How to use Google Analytics and Google Tag Manager 2 on a local address?

…衆ロ難τιáo~ 提交于 2019-11-30 22:35:48

If you go to "fields to set", click on "add new field" and start typing into the "field name" input box the autosuggest function will suggest applicable field names (i.e. if you start typing "coo" it will suggest everything related to cookies, including the cookie domain).

"Behind the scenes" GTM has always used the "set fields" mechanism of GA, this has now been made explicit in the interface. But this does make a difference to the way GA tracking works, so simply "set field"->"cookieDomain" to "none" and everything will work like before.

Rather than modifying your tags to add/remove cookieDomain when moving between localhost and yourdomain.com I do the following to set cookieDomain automatically based on hostname.

  1. Create a custom JS variable that will be used to set the value of cookieDomain.
    1. Variable Name: XYZ-JS-CookieDomain (or whatever your naming convention is)
    2. Type: Custom JavaScript

Code (the debug stuff is how I do all my custom JS variables, it's not required):

function() {
    var result = {{Page Hostname}} == 'localhost'
                 ? 'none'
                 : 'auto';

    if ({{Debug Mode}}) {
        console.warn('XYZ-JS-CookieDomain', result);
    }

    return result;
 }
  1. Configure the Tag.
    1. More Settings > Fields To Set
    2. Field Name: cookieDomain
    3. Value: {{XYZ-JS-CookieDomain}}

Now when you run from localhost cookieDomain will be set to "none" and anywhere else it'll be "auto".

I guess you don't need to do any of these things now. cookieDomain is set to "auto" by default.

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