How do I use Google Analytics and Google Tag Manager 2 at a local address?

I am relatively new to GTM and have experimented with a new interface that will be completely replaced by the original on April 1st: https://support.google.com/tagmanager/answer/4605576 .

However, I ran into some issues that caused Google Analytics to log page views when testing at a local address. To do this, there is a solution using the original GTM format described here: Tracking the local host in Google Analytics in Google Tag Manager , so I don’t want to ask a repeating question. However, in the new Google Tag Manager design, it’s no longer possible to set the cookie domain to “none”, so how can I test Google Analytics and GTM from a local address now?

+3
source share
3 answers

If you go to “fields for installation”, click “add new field” and start typing “field name” in the input field, the auto-execution function will suggest the corresponding field names (that is, if you start to enter “coo”, it will offer all related to cookies, including the cookie domain).

“Behind the scenes” GTM has always used GA's “set margins” mechanism, now it has become apparent in the interface. But that matters for how GA tracking works, so just "set the field" → "cookieDomain" to "none" and everything will work as before.

+4
source

Instead of changing your tags to add / remove cookieDomain when moving between localhost and yourdomain.com, I do the following to automatically set cookieDomain based on hostname.

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

Code (debugging stuff is how I execute all my custom JS variables, this is not required):

function() { var result = {{Page Hostname}} == 'localhost' ? 'none' : 'auto'; if ({{Debug Mode}}) { console.warn('XYZ-JS-CookieDomain', result); } return result; } 
  1. Customize the tag.
    • Advanced Settings> Fields for Installation
    • Field Name: cookieDomain
    • Value: {{XYZ-JS-CookieDomain}}

Now, when you start cookieDomain from localhost, it will be set to "none", and in another place it will be "auto".

+4
source

I think now you do not need to do anything. By default, cookieDomain is set to auto. enter image description here

+1
source

All Articles