Google Tag Manager is a very robust (and awesome) tool to use if you’re managing many different types of pixels and code for a web property. If like me, you have multiple vendors and ad partners it may not make sense to fire all your pixels during all the same conversions events. Maybe you want to fire a pixel only on specific traffic that came from a specific channel.
For most people, UTM parameters work well to segment traffic from different channels, but those UTM’s fall of once a user bounces around your website.
Example, if someone came to my site with utm_source=facebook, I can see that in the Page URL (i’m using Google Tag Manager’s preview function to see this).
But as they bounce around my site, from blog to a specific blog post, we can no longer see the UTM of that user.
In order to retain that UTM variable, we must use some code to capture, extract and save it in the browser.
With sessionStorage, the variable is saved long enough to fulfill any conversion tracking and is erased once the user closes their tab or browser. To accomplish this, let’s use Google Tag Manager.
1. First, create a new custom html tag. Name it “UTM Source Capture” and paste the following code. Make sure you click “fire on all pages”. Then save the tag.
var url = window.location.href;
var keyToFind = 'utm_source';
if(url.indexOf(keyToFind + '=') > -1)
var valueFound = url.substr(url.indexOf(keyToFind + '=') + keyToFind.length + 1).split('&');
3. Set up your custom trigger. Choose “Page View” then select your previously made variable and have it equal to the specific channel source you want to fire your pixel on. Then, choose page path and add the page you want your conversion pixel to fire (example, on your thank you page). Name it “Facebook – Thank you”.
4. Now you’re ready sync up your pixel. Go to “Tags”, select your tag and have it fire on the trigger we just made.
5. Publish and you’re done! Be sure to use Google Tag Manager’s preview function to troubleshoot and confirm it is firing correctly. You can also go to your console, and in “Resources”, locate session storage and you’ll be able to see the saved key and value (hopefully matching what you just made!).
Also check out How To Set Device Type in Google Tag Manager.