Analytics
5.2K members online now
5.2K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
 
Guide Me
star_border
Reply

Storing ClientID on a cuestom dimension

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi everyone,

What do you think of the following code to store ClientID on a custom dimension?

<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','//www.google-analytics.com/analytics.js','ga');
 
ga('create', 'UA-XXXXXXX-2', 'example.com');
 ga(function(tracker) {
 ga('send', 'pageview', {
    'dimension1' :  tracker.get('clientId')
 });
 });
</script>

 

Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author gmr1970
June 2016

Re: Storing ClientID on a cuestom dimension

[ Edited ]
Explorer ✭ ☆ ☆
# 2
Explorer ✭ ☆ ☆

Hi gabriel,
on your function you have directly the tracker so you can use the send call directly:

ga('create', 'UA-XXXXXXX-2', 'example.com');
ga(function(tracker) {
     tracker.send('pageview', {'dimension1' : tracker.get('clientId') });
});

here you find documentation

https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits#using_a_named_...

 Fil

View solution in original post

Marked as Best Answer.
Solution
Accepted by topic author gmr1970
June 2016

Re: Storing ClientID on a cuestom dimension

[ Edited ]
Explorer ✭ ☆ ☆
# 2
Explorer ✭ ☆ ☆

Hi gabriel,
on your function you have directly the tracker so you can use the send call directly:

ga('create', 'UA-XXXXXXX-2', 'example.com');
ga(function(tracker) {
     tracker.send('pageview', {'dimension1' : tracker.get('clientId') });
});

here you find documentation

https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits#using_a_named_...

 Fil

Re: Storing ClientID on a cuestom dimension

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Thanks for your prompt answer, @filtro!

I've tried to implement this solution, but I couldn't make it work:
http://www.simoahava.com/analytics/improve-data-collection-with-four-custom-dimensions/#2

I'm not a developer myself, so I have two questions....
1. Pros and cons of using the version that I posted here versus the one posted on the link above.

As a result of trying to implement the solution posted on the article, I'm storing "false" on my custom dimension. I have a WordPress based site and I’ve implemented GTM using this plugin:
https://es.wordpress.org/plugins/duracelltomi-google-tag-manager/

2. Do you have any ideas what could be happening?

Thanks in advance!

Re: Storing ClientID on a cuestom dimension

Explorer ✭ ☆ ☆
# 4
Explorer ✭ ☆ ☆

Hi Gabriel,
the solution that you are posting to be used on GTM need to use a custom html tag, I don't know how you are using it, but looking at the link on ahava blog, I suppose that you are using different custom html tag to recreate what simo is explaining.

If this what you are doing you will be in the situation where you have different tracker with the same name on your page and this is creating an exception that block Google Analytics code.

What you have to do is:
create a custom Javascript variable that retrieve the clientID from the current tracker

 

function() {
  try {
    var trackers = ga.getAll();
    var i, len;
    for (i = 0, len = trackers.length; i < len; i += 1) {
      if (trackers[i].get('trackingId') === {{GA Tracking Code}}) {
        return trackers[i].get('clientId');
      }
    }
  } catch(e) {}  
  return 'false';
}

{{GA Tracking Code}} is a GTM variable that contain the property id.

 

This is the solution that resolve the issue to get the correct clientId

 

Re: Storing ClientID on a cuestom dimension

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

Hi again @filtro

If I understood correctly what you've just said, I guess that Mr. Ahava's solution wouldn't work if I don't setup also this:
http://www.simoahava.com/analytics/access-the-tracker-object-in-your-page-view-tag/

If that's the case, my question remains the same...

What are the advantages of doing so much stuff on GTM, when you can do the same using your line??

I'm referring to this line:

ga(function(tracker) {
tracker.send('pageview', {'dimension1' : tracker.get('clientId') });
});

Re: Storing ClientID on a cuestom dimension

Explorer ✭ ☆ ☆
# 6
Explorer ✭ ☆ ☆
The code it's just using what ahave describe on this second article you posted.

The advantage of GTM is not implement this code, but using GTM permit you to personalize all the implementation and add other tracking much faster. GTM is never an advantage for a single implementation, but it help you when you want to modify and improve you implementation with new element.

Fil

Re: Storing ClientID on a cuestom dimension

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭
Thanks a lot!

Re: Storing ClientID on a cuestom dimension

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭

Sorry, just to make things clear...

 

Is this code correct?

 

<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-XXXXX-X', 'auto');
  ga('require', 'displayfeatures');
  ga(function(tracker) {
     tracker.send('pageview', {'dimension1' : tracker.get('clientId') });
});
  ga('send', 'pageview');

</script>

Re: Storing ClientID on a cuestom dimension

Explorer ✭ ☆ ☆
# 9
Explorer ✭ ☆ ☆
It should work correctly

Re: Storing ClientID on a cuestom dimension

Visitor ✭ ✭ ✭
# 10
Visitor ✭ ✭ ✭
Hi again @filtro,

I've copied and pasted the above code on a Custom HTML tag on Google Tag Manager, but Google Tag Assistant is telling me: "Same web property ID is tracked twice."

I've looked at the code, and there's just one instance of the GTM code snippet.

When I look at the code that I posted above, I see that ga send pagewiew appears two times.

Is this what's causing the issue?