Is there a way to create a google tag manager rule ( via a macro ) which identifies the traffic source type? ( Like organic ?). Would like to create a tag which only fires if the source of traffic is organic. Possible?
2 Answers
Easiest way would be to extract the source (utmcsr) from the Google Analytics (__utmz) Cookie and fire an tag Manager event based on the value. Then create a rule based on that event.
I don't think it's possible solely from the tag manager interface.
(Updated to add) I've used the following code in the past and guess it'll still work (I'm afraid I can't give proper credit, I pinched that from some website).
/**
Reads the Google utmz Cookie and returns he values as an array
utmcsr = utm_source
utmccn = utm_campaign
utmcmd = utm_medium
utmctr = utm_term
utmcct = utm_content */
function parseGACookie() {
var values = {};
var cookie = readCookie("__utmz");
if (cookie) {
var z = cookie.split('.');
if (z.length >= 4) {
var y = z[4].split('|');
for (i=0; i<y.length; i++) {
var pair = y[i].split("=");
values[pair[0]] = pair[1];
}
}
}
return values;
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
ga = parseGACookie();
if(ga['utmcsr'] == "cpc") {
alert("Paid advertising");
}
-
Agreed. I am not sure how to extract the utmcsr value in the dataLayer. Care to provide an example of how this will look like? thx .. Feb 25, 2013 at 16:04
-
Interesting. I have been messing with something similar today - also found on aother site, but cant get it to work well. Having trouble outputing the utmcmd value into a dataLayer variable ( and also having trouble figuring out how to post the code here! ) Feb 26, 2013 at 2:31
If your traffic arrives from tagged URLs (like from a campaign that you manually tag with the default GTM url tags) you can do this simply through the GTM interface.
- Create a URL type macro with the Query component type that reads out the URL tag, like this: https://www.evernote.com/shard/s56/sh/c1d43612-66ed-4972-aca3-4aa59c040322/347f19fb9561d11143e33a25d6fe080c
- Create a rule that fires when this macro equals your tag (e.g. medium=cpc or utm_campaign=yourcampaign)
- Create a tag that sends a GA event based on this rule
I hope this helps.