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

五迷三道 提交于 2019-12-09 00:39:29

问题


I'm relatively new to GTM and have been experimenting with the new interface which will be fully replacing the original on 1 April: https://support.google.com/tagmanager/answer/4605576.

However, I have encountered some issues getting Google Analytics to register pageviews when testing on a local address. There is a solution for this using the original GTM layout outlined here: Track localhost on Analytics in Google Tag Manager, so I don't wish to ask a duplicate question. However, in the new Google Tag Manager design there is no longer an option to set Cookie Domain to 'none', so how could I now test Google Analytics and GTM from a local address?


回答1:


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.




回答2:


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".




回答3:


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



来源:https://stackoverflow.com/questions/29201241/how-to-use-google-analytics-and-google-tag-manager-2-on-a-local-address

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