How can you scrub PII from a URL when using GA via GTM?

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.

Answers

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

Posted on by Max