Correctly add dataLayer information (dataLayer vs ga('ec:...') - google-analytics

I want to implement Google dataLayer for a client, and after reading docs, articles, and trying out browser extensions, I am still a bit confused.
I saw 2 versions of it being used:
dataLayer.push('ecommerce': {
'detail': {/* data */},
'impressions': [{/* data */}]
});
and
ga('ec:addImpression', {'id':'%s', 'name':'%s', 'category':'%s', 'brand':'%s', 'position':%s, list: '%s'})
ga('send', 'pageview');
Is there a preferred version, or do both work ? Also, for the second option, do I need to use ga('send', 'event', '......') each time after using the ga('ec:something') function ?

First one is the dataLayer as used in Google Tag Manager.
The second is not a dataLayer at all, these are direct calls to the Google Analytics tracking code (which would need to be loaded for this to work).
So these are two different things, if you really intend to implement a dataLayer to use with a tag management solution you need the first solution.
And not to be unkind, but if you can't tell the difference you should start by reading Google's really extensive documentation. From your question it is not even clear what you intend to do, since dataLayer is not a Google product, it's just a variable that feeds into Google's tag management solution.

Related

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

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'
});

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.

Can Adobe analytics variable be passed to a Google analytics custom dimension?

Does anyone know if it's possible to capture the variables from an Adobe Analytics implementation and pass them to GTM so that they can be used as custom dimensions in Google Analytics?
I was wondering if there was some javascript that i could implement in GTM that would extract the neccessary props i want from the adobe data layer, but im not a developer so im not even sure if this is possible.
Thank you!
This looks to me like exactly the guide you are looking for:
http://blogs.perficient.com/adobe/2017/04/12/custom-adobe-analytics-tracking-using-google-tag-manager/
I don't have any personal experience with Adobe Analytics so I cannot help you further than that. However I can tell you what you are trying to achieve is most definitely possible.
This is definitely possible and not hard to pull off. There is even a google analytics tool than can be used for adobe dtm.
Just doing it manually through code, make sure that GA is already present on the page, and fire this code in a third party script after you AA variables are set.
ga('set', 'dimension1', value1);
ga('send', 'pageview');
You can set multiple dimensions at once:
ga('ehiglobal.set', {
'dimension2': value2,
'dimension3': value3,
'dimension4': value4
});
You can also pass specific page names on the hit:
ga('send', 'pageview', location.pathname);
You can also send an event hit if you already have page views set up:
ga('send', 'event', 'category', 'action', {
'dimension5': value5
});
**Note: You need to send the data through a page view or a hit. Just setting the data wont do anything, so make sure you actually have one of the send 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.

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