How can I redact personal-identification info (PII) in a URL when using gtag.js? - google-analytics

I'm using the Google global site tag (gtag.js) to manage my tags, and I use it for Google Analytics.
Sometimes there is an email address in URLs on my site, and I need to remove that from the code.
I know there are clever ways to get this done when using Google Tag Manager. But I'm not using GTM, I'm using gtag.js. (This simple page explains the difference.)
How can I check the current page's URL for email addresses and fix that, before the URL is tracked by gtag.js?
(I know how to do the regex etc... this is more asking where in the flow can I grab the URL and 'rewrite' it as needed before the Analytics pageview event posts?)
I looked through the gtag.js API reference without much luck. Also looked at the docs for how Analytics implements gtag.js, still no luck.

Indeed Google's documentation can be hard to explore sometime since they describe stuff only once for better maintenance and consistency, but at the end you could be missing a working summary ...
I would suggest to use this https://developers.google.com/analytics/devguides/collection/gtagjs/pages
gtag('config', 'GA_MEASUREMENT_ID', {
'page_path': '/pagepath?email=redacted#example.com'
});

Related

What is gtag and why do I have to add that separately from google tag manager?

I find Google documentation around Google Tag Manager (GTM) terrible at helping me figure out which bits go where.
As I understand, GTM requires that you put a <script> snippet on your pages which is supposed to bring in other code snippets, as could be configured by a non-technical user.
I'm a technical user, though. Perhaps that's the problem! I also find it problematic that Google use the word "tag" to refer to either an HTML element tag, like <script>, or their own proprietary use of the word to mean calling a function ("triggering a tag") in another script, also unhelpfully referred to as a tag.
They also have "gtag" which is what - a helper? something that enables you to send general analytics events through the GTM API? The docs simply say:
The global site tag (gtag.js) is a JavaScript tagging framework and API that allows you to send event data to Google Analytics, Google Ads, and Google Marketing Platform.
... but we could already send analytics? What does this add?
For example, I wish to send an e-commerce Purchase event.
I've found that to do this I needed to add a new snippet of code with two <script> tags to the header on the site (thought GTM meant I didn't need to do this?) that sources gtag.js, then I'm able to call the following at the appropriate place in my javascript:
gtag('event', 'purchase', { value: 1.23, transaction_id: 'test' });
Or without it (although this does not seem to work):
ga('require', 'ec');
ga('ec:addProduct', {name: 'test product', price: 1.23})
ga('ec:setAction', 'purchase', { id: 'test_id_1', revenue: 1.23 })
So my question is: when would you use gtag() over ga(), and why can't GTM install gtag?
When would you use gtag() over ga()?
Use gtag if you want to send data to supported Google products other than Google Analytics. As you pointed out, "The global site tag (gtag.js) is a JavaScript tagging framework and API that allows you to send event data to Google Analytics, Google Ads, and Google Marketing Platform.", whereas ga only works for Google Analytics. But (see below), you might decide to never use gtag nor ga and always use GTM.
Why can't GTM install gtag?
It could (you could have a GTM tag inserting some gtag code) but it's beside the point as they are meant to be used as 2 different solutions:
gtag is a purely programmatic tracking tool for sending data and only works with 3 Google products (so far - Analytics, Ads, Marketing Platform - more maybe added in the future), it's made to provide basic out-of-the-box tracking with a simple copy/paste + small lines of code (if needed for customization).
GTM is a tag manager: it can work programmatically BUT requires a minimal configuration of the container via the GTM UI (a default container won't send data anywhere), and can send data to whatever products you want (just need to setup the corresponding tags in GTM), while having a bunch of other features
A few questions to help you choose:
Am I sending data to other tools than Google Analytics/Ads/Marketing platform?
Do I want to use some the extra features GTM offers (UI, version control, templates, debug, environments etc...)?
Is there some tracking that would be heavy to implement via pure custom JS (eg scroll tracking) which GTM can facilitate with its built-in listeners (eg scroll tracking)?
If YES to any of the above, then use GTM
I personally never use gtag, I always replace it with GTM because it's considerably more powerful than gtag.
What Google is doing is progressively replacing all their default snippets with gtag so they only have 1 unified API to maintain and it's an easy copy/paste for users (bear in mind most users aren't tech savy and just need to paste the snippets in into their CMS). Forcing people to use GTM would be too much of a friction as out-of-the-box GTM simply doesn't track anything and people would need to learn & configure GTM, too much work vs a simple copy/paste.
Note: The built-in events don't use category, label, and value. Take care to use the correct keys when sending these events.

Adding GTM to a website with GA

I have a website that has been using GA for some time. Now someone wants me to add it to theirs GTM and replace my snippet with that GTM tag.
I don't know how to use GTM and don't really want to dig into that :/
My website was working just fine... Is there some easy way to make GTM just a simple middle man that looks at my domain and just throw everything directly to GA?
I manage to make it so GA gets info on the website traffic like active users etc. however that website has scripts that fires events to GA.
After googling a while i think this is because GTM adds some random names to the trackers and my code calls ga('send', ... ) directly :/
I know i can make a custom tag in GTM but they want it to be UA tag -_-.
Is there any way to set a default name for my trigger in GTM settings? Or some other solution?
atm. i have a code with gtm tag only and I'd rather avoid changing my web code if possible.
Edit
Ok, could someone explain to me how to achieve this:
I have this code:
var a = $('meta[property="a"]').attr('content');
var b = $('meta[property="b"]').attr('content');
ga('send','event',a,event,b);
where event is one of several possible strings of for example 'event_1','event_2' or'event_3'
and my GA has 3 goals that have action = 'event_1' etc.
How do i replace this with GTM and dataLayer?
This thread has 2 questions :
1.- Migrate a hardcode implementation of Google Universal into Tag Manager is not so simple as copy and replace the Universal Main Snipper for the GTM Code.
Look for this google guide to migrate. Has more or less the steps needed and the one to take in consideration during the migration.
https://developers.google.com/tag-manager/devguide#migration
If you goes for GTM, it's higly recomended to remove your ga() function on the page, this will stop working and you javascript too. Basically this mean, remove all your Google Analytics of the domains and install GTM and configure the corrects tags. Try to avoid things like paste the Google Analytics code inside a custom HTML tag, it's a very bad practice, but is see that a lot. Plan your migration
2.- Regarding the event you have to do :
Create a tag of universal analytics events and activate when you pushes a GTM event, them manage this values via the dataLayer
Let this link for more information:
https://support.google.com/analytics/answer/6164470?hl=en
var a = $('meta[property="a"]').attr('content');
var b = $('meta[property="b"]').attr('content');
dataLayer.push({'event': 'ga_event' , 'cat' : a , 'act' : b})
Try to involve more yourself in the GTM and Universal's World before ask, i'm not trying to be an asshole, but this question involves so many things that can be solved just looking the documentation, and somany thing to examplain in a single post.

Automatic checking of GTM Tags

Is there is a tool or a solution that automatically checks if the GTM (Google Tag Manager) tags are working properly on a page?
I don't need it to do anything else except retrieve the bag of tags and let me know which any URLs where there is a problem with a tag.
I can only find manual validation checking and I would need to implement a solution for a large number of tags so automation would be most helpful.
There are a few paid services that I can think of:
Observepoint
Tag Inspector
I'm sure there are others, but those are the ones I've used.
You can also use GTM's error tracking to log client-side JS error. Doesn't really check to see what tags are firing, but will let you know when your JavaScript is having problems.
Good blog post here: Using Google Tag Manager to log JavaScript errors in Google Analytics

How to track urls without sessions and encrypted url in google analytics in a decent way?

General setting
I have a website, which uses regular and encrypted urls. Now I want to track the pageviews or all pages the same way.
I have regular URLs like this:
/library.dll?page=page12&arg1=0&arg2=some&session_id=7892734
and special pages like this:
/library.dll?page=specialpage&arg1=0&arg2=some&session_id=7892734&id=page13
aswell as encrypted URLs like this, which are also containing the session id:
/library.dll?page=encrypted&args=gYZEI7lnRAQLzVXdtdbcral8.cOoc6NDtMUGY2yep9wO3JM
So the interesting niformation is always the page, which is in this examples page12, page13 and page14 (where page14 is also part of the encrypted string).
Clarification
I can change the HTML and JS code only. I have no access to the Google Analytics interfaces at all. This will be administrated by multiple customers.
The GA code will be integrated within a template using a customer-specific code and their unique tracking id.
Problem description
I need to track the page argument, because this is basically the interesting part of the url. When tracking other url parameters I cannot accumulate the pageviews for a certain page, because Google Analytics shows them as separate pages.
In addition I don't see any way to track the pageviews with encrypted URLs, because I cannot set a generic name for them unless there is a way to utilize the method ga('send', 'pageview');
Solution idea
I read about overwriting the pageview attributes like this:
ga('send', 'pageview', '/my-overridden-page?id=1');
in the article on page tracking #Google Developers
Utilizing (event) triggers is in my opinion a pretty bad idea.
The question itself
Is there any smarter way to track this information? Is extracting the page-information and overwriting the pageview attributes the best way to do this?
I just started using GA and have kind of no idea how to do this any other way.
You could use filters (custom advanced filters) to rewrite the request url inside the google admin interface (admin->views->filters). This has the advantage that you do not need to change your site/application code.
However using filters will require multi-step-filters with heavy use of regular expressions, and you would have to test this in a "staging" view first (because a wrong filter will permanently mess up your data).
Passing a custom url to the pageview tracking is pretty straightforward and can be tested immediately via the real time view. In my opinion this is indeed the smartest way to do this.

Ember.js and Google Analytics

I wanted to get a definitive answer on here for later reference now that we have a stable Ember RC. A combination of the top 2 search results for emberjs google analytics reveals that this is a good way to do track route changes:
App.ApplicationController = Ember.Controller.extend
routeChanged: ( ->
return unless window._gaq
Em.run.next ->
page = if window.location.hash.length > 0 then window.location.hash.substring(1) else window.location.pathname
_gaq.push(['_trackPage', page])
).observes('currentPath')
but then I also see results for using Event Tracking for single page web applications.
I haven't tested the code above yet, it takes a few hours to propagate changes to the GA dashboard. Update: This doesn't show up under the Content category on my Google Analytics dashboard. Neither under "Pages" or "Events".
If anyone has advice or if there's something I'm missing somewhere let me know. I can also PR a guide for the website based on the answers here.
Alex DiLiberto gave a really nice talk about a robust & scalable way of adding Google Analytics to an ember app in his EmberConf 2014 talk here.
- Slides
- GitHub
App.ApplicationRoute = Ember.Route.extend({
actions: {
didTransition: function() {
Ember.run.once(this, function() {
ga('send', 'pageview', this.router.get('url'));
});
}
}
});
The talk was aiming to be independent of which analytics library was used.
There is also now an official Ember Cookbook on implementing Google Analytics here.
I would use _trackPageview for things that have routable URLs and _trackEvent for things that don't.
In the Event Tracking link when they refer to "Embedded AJAX page elements". They're not meaning SPA's, but rather those cases when the URL stays the same, but some event that you wish to track happens within the page (in the case via AJAX).
There may be other cases where it makes sense to use _trackEvent, but for route transitions I'd use _trackPageview.
Using routeChanged() is not a good way to track dynamic segments such as /category/food /category/something since it's going to be fired only once. I wrote an article about this here: http://www.randomshouting.com/2013/05/04/Ember-and-Google-Analytics.html. I also consulted with the guys behind Ember and confirmed that this is indeed the proper way to track url changes for Google Analytics.
Most of these answers are outdated. You should be using a mixin and adding it to your Router to listen for the didTransition event and fire your pageview there. That way it's handled for all routes. There are several addons out there, including one I wrote called ember tracker which gives you pageviews and event tracking out-of-the-box.
You can see how I did it here. It's fairly straight forward.

Resources