Reusing Google Analytics Campaign Information With Google Tag Manager
There are many situations where it would be helpful to have the source, medium, and other campaign information for a user’s session. For example, we may want to capture the source / medium of a session when a user submits a Contact Us form to store inside of Salesforce.
This used to be very easy to do; in Classic Analytics, campaign information would be stored in the __utmz cookie, right on the user’s browser.
Universal Analytics moved campaign processing from the user’s browser to Google’s servers, eliminating the __utmz cookie (and easy access to that data). So how can we retrieve that information with Universal Analytics?
The proper way to do it is to store the users Client ID in a 3rd-party system and a Custom Dimension, then run a periodic script to retrieve the data via the Reporting API and sync it between systems. However, for many clients that kind of implementation is out of reach.
A few years ago we wrote a UTMZ Cookie emulator recipe that we’re releasing to the public today. The script it contains will do its best to faithfully recreate the __utmz cookie of old, and will inherit values from any existing __utmz cookie and apply the same rules that ga.js would to values therein.
Import The Recipe
For instructions on how to import the recipe file, check out my colleague Jim Gianoglio’s evergreen post on using the Import feature of Google Tag Manager. Once you’ve got it imported, proceed.
Use the Variables
The recipe will import the following Variables into your container:
- JS – GA Source
- JS – GA Medium
- JS – GA Campaign
- JS – GA Content
- JS – GA Keyword
These correspond to dimensions in Google Analytics. We can use these Variables to use for a condition in a Trigger or to pass values to a Tag.
In a Trigger
Let’s set up a Trigger that fires when a user visits from a search engine. We’ll create a Trigger that fires on Pageview when the value of the JS – GA Medium Variable is “organic”. Here’s how to set that up:
We can use this concept with any of our campaign variables, too; want to fire a modal for users visiting from a specific campaign? Use the JS – GA Campaign Variable.
In a Tag
We also might want to provide these values to Tags that fire in our container. If the Tag is a “pre-built” templated Tag, we typically just need to add the Variable to a field in the template. To pass along our data to a Custom HTML Tag, we need to review the vendor’s documentation and determine where the value belongs.
Hopefully you’re excited about some of the things you might try with this recipe, but before you get too jazzed, there are some caveats. Because the processing happens server-side, there are some situations our recipe can’t account for:
- If you’re using cross-domain tracking, our recipe will probably say the session was a self-referral from that sister domain.
- If you’re using the referral exclusion list extensively, our recipe will attribute sessions to domains that the exclusion list would have set to (direct) / (none) instead.
- If you have any kind of filter black magic fiddling with the source / medium values, our script will miss those, too.
- If you’re using Google AdWords or Google DoubleClick and autotagging is turned on, you will see the source/medium, but you won’t receive campaign information as that’s joined inside the interface.
- If the user is coming directly and has an existing campaign from before the recipe was added, the recipe will show them as (direct) / (none) whereas the reports will show the previous campaign.
Questions? Issues? Let us know in the comments.