0

It's supposed to be done, and in plain Google Universal Analytics, it should be a simple matter of setting the location:

ga('set', 'location', sanitisedLocation);

But in GTM, of course, there is no place in the code snippet where the ga variable has been created, but the pageview has not been triggered.

So where would I add such code? I'm guessing it might be via the dataLayer but wouldn't know how, exactly.


Edit: I'd be happy to do this by editing the Pageview tag in the GTM UI, but I don't know where to override the location with a custom variable.

Walf
  • 8,535
  • 2
  • 44
  • 59

1 Answers1

1

You have 2 options:

  • Custom HTML tag: you can use an HTML tag to insert your own Google Analytics code inside <script> elements.
  • Fields to Set: you can use the Fields to Set option (Variables -> Google Analytics Settings -> More Settings -> Fields to Set) to set the location and other Google Analytics parameters. You will need to create a variable (eg {{sanitisedLocation}} as Custom JavaScript to return the value, and use that for your field:

enter image description here

Max
  • 12,794
  • 30
  • 90
  • 142
  • I figured something like that but left my edit unsubmitted for a while whilst investigating it. I used this for the custom script: `function() { var l = window.location; return l.protocol + '//' + l.host + l.pathname + l.search.replace(/([?&])((private_param_1|private_param_2)=[^&]*([&])?)+/gi, function(m, qa, x, n, a) { if (qa == '?') { if (!a) { return ''; } return qa; } return a || ''; }); }` where `private_param_*` are the sensitive query string parameters. I called it `Page URL Safe` so it's listed next to the built-in `Page URL`. – Walf May 09 '18 at 07:16
  • Indeed very good practice to follow the existing GTM naming convention so you don't end up with variables all over the place with obscure names you won't understand later. Looks like you have a bright GTM future ahead of you :) – Max May 09 '18 at 10:33
  • In the end I applied the same scrubbing to make "Safe" versions of `Page Path` and `Referrer` built-ins, then also override the `page` and `referrer` settings of analytics, and replaced all relevant uses of the built-ins with the scrubbed versions. – Walf Aug 15 '18 at 08:32