Update 6/9/2014: Jump to the end of this post for a solution that obviates the need for two tags.
A week ago, I migrated a site to use Universal Analytics (UA) from classic GA. As part of the migration, I wanted to enable the new User ID functionality which lets you pass a User ID for authenticated (signed in) with each GA hit. This gives Google Analytics a common identifier to the ‘connect the dots’ across visits where cookies aren’t shared. That way when a user visits from multiple machines and mobile devices, you have visibility into their behavior through the new Cross Device Measurement reports in Google Analytics.
The site being upgraded to UA was using Google Tag Manager which makes migrations of this nature a breeze. I disabled the classic Google Analytics tag and added the new Universal Analtyics tag. To enable the User ID feature, you need to add a row in the ‘Fields to Set’ section.
The value field is populated with my {{uid}} macro which is the customer identifier that is passed to the data layer when a user is authenticated. What I didn’t realize is that when the user isn’t signed in, the {{uid}} is empty, but the &uid field is still passed to the Google Analytics hit. This caused a significant drop in pageviews, because for some reason when the UID field is empty, Google Analytics isn’t tracking them.
The workaround is to create two versions of your Universal Analytics tag to handle authenticated and non-authenticated scenarios. For the authenticated users, you want a firing rule similar to this:
This rule uses RegEx to test that the {{uid}} is a valid GUID (the customer identifier on this particular site) and fires on all pages.
For the non-authenticated (no User ID to populate), modify your Universal Analytics tag so the ‘Fields to Set’ is not being populated with the ‘&uid’ field. The firing rule for this tag will simply be the ‘All Pages’ rule. Now add a blocking rule using the rule created above (All Authenticated Pageviews) that only fires when the {{uid}} macro is a valid GUID.
Now you have two versions of your Universal Analytics tag. One will fire when users are authenticated and pass the User ID. The other tag will fire for anonymous users and not populate the User ID field. This will resolve the issue of pageviews not being properly recorded due to the empty User ID field.
Don’t forget to do this same workaround for any event or ecommerce tags you have implemented in Google Tag Manager.
As a final note, I have reached out to the Google Analytics team asking if this is the intended behavior as I suspect others will encounter this same issue. I will update this post based on their response.
Update 6/9/2014: Thanks to Simo Ahava, there is a solution that eliminates the need for two using two tags. For this version, you’ll only need one Universal GA tag, one rule (All Pages) and two macros ({{uid}} and {{Get User ID}}. In the Universal GA Tag fields to set, you’ll use a JavaScript macro {{Get User ID}} that will do a check to see if the User ID value is empty or not. If it is, it simply returns which sets the {{Get User ID}} macro to undefined. Google Analytics does not pass undefined values as part of the hit, so you avoid the problem I encountered previously. Here’s what the JavaScript macro looks like:
Happy Measuring!
Questions or comments? Get in touch with me on Google+ or leave a comment below.
Comments on this entry are closed.