16

I am setting up a Custom Dimension in my Google Analytics Tracking Code, however I am seeing a strange error in the Chrome Console with the Google Analytics Debugger switched on.

This is my code which is fired on every page. I am reporting to a regional account as well as a global/rollup account and I have created two trackers to achieve this.

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-85521872-1', 'auto', 'crmpiccoglobal');
    ga('create', 'UA-85521872-3', 'auto', 'crmpiccoregion');

    ga('set', 'dimension1', 'premium');

    ga('crmpiccoglobal.send', 'pageview');
    ga('crmpiccoregion.send', 'pageview');
</script>

In the console I see this:

Running command: ga("set", "dimension1", "premium")

analytics_debug.js:10 Command ignored. Unknown target: undefined

I have created the Custom Dimension in GA under each property I want to access it in.

Community
  • 1
  • 1
crmpicco
  • 16,605
  • 26
  • 134
  • 210

2 Answers2

27

You need to use your tracker names in the "set" command, else GA will apply the command to the default tracker t0 (which does not exist in your example):

  ga('crmpiccoglobal.set', 'dimension1', 'premium');
  ga('crmpiccoregion.set', 'dimension1', 'premium');

  ga('crmpiccoglobal.send', 'pageview');
  ga('crmpiccoregion.send', 'pageview');
crmpicco
  • 16,605
  • 26
  • 134
  • 210
Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62
  • OFC, thanks. I was treating it like the `create` thinking I had to provide the tracker name at the end. – crmpicco Oct 12 '16 at 04:57
  • 6
    In my case, i use GTM to insert analytics. It sets the name as gtm1 – Maxwell s.c Oct 26 '17 at 11:29
  • 1
    @User, See the comment above yours, if you use Google Tag Manager, the `ga('create')` is run with the name gtm1 (in my case). So for example for event tracking i use `ga('gtm1.send' {....more data....} );` Use a Google analytics debug chrome extension, this helped me alot. – Bjorn Jul 09 '18 at 13:15
9

If you use Google Tag Manager to load your Google Analytics and you don't know what tracker is being created, use this:

sendGa(name : string, data: any) {
    (<any>window).ga(() => {
        const trackers = (<any>window).ga.getAll();
        const firstTracker = trackers[0];
        const trackerName = firstTracker.get('name');
        (<any>window).ga(trackerName + '.' + name, data);
    });
}

docs: https://developers.google.com/analytics/devguides/collection/analyticsjs/accessing-trackers

CularBytes
  • 9,924
  • 8
  • 76
  • 101
  • Awesome mate! I guess you are using typescript? But not Eslint :) Strange syntax :-) You could use `const` instead of `var` for those and concatenate with ` ${trackerName}.name ` – OZZIE May 28 '19 at 08:56
  • 1
    what are the name and data in this case? I am using GA via GTag Manager – Pardeep Jain Jul 18 '19 at 11:45
  • I named it `name` and `data` as it seems most logical when looking at GA's documentation, but eventually it is just a collection of parameters that you would normally sent to `ga(x,y,z)`. You could also add another function that takes and sends 3 arguments and do something like: `sendGa2('send', "pageview", "shipment");` – CularBytes Oct 30 '19 at 08:35
  • 3
    You can get the tracker name with `firstTracker.get('name')` https://developers.google.com/analytics/devguides/collection/analyticsjs/accessing-trackers#getting_data_stored_on_a_tracker – LuJaks Jan 07 '20 at 13:55