Google Tag Manager Rule to Identify Traffic Source

三世轮回 提交于 2019-12-23 04:32:26

问题


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?


回答1:


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");
}



回答2:


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.



来源:https://stackoverflow.com/questions/14983545/google-tag-manager-rule-to-identify-traffic-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!