Which gtag MEASUREMENT_ID to use - google-analytics

I'm a back end developer, sometimes a little fronted, but not a SEO related expert at all.
Now I have to make a full website from top to bottom and I'm stuck with the Google stuff.
First I created a Google Analytics profile, I got the srcipt that I needed to hardcode to all my site.
<script async src="https://www.googletagmanager.com/gtag/js?id=G-MEASUREMENT_ID></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-MEASUREMENT_ID');
</script>
However, a little while later, I started to use Google Ads and it recommended to connect my Analytics page to the Ads page, so I connected them and I got another code that I needed to insert.
The code snippet is almost the same, except the two MEASUREMENT_ID, because now I got a UA-MEASUREMENT_ID.
So, do I have to use both snippets, or just one? If so, which MEASUREMENT_ID should I use?
Or am I screwed up something?
Thanks all the help

The code with UA- (Universal Analytics) and the one with G- (Google Analytics 4) belong to two completely different tracking systems in Google Analytics. If until now you have used UA- you should continue to do so and in parallel track with the new one G-. So you will have two tracking codes at the same time. This is also Google's suggestion not to lose the data history in UA-.

Related

Google Analytics stopped gathering data when migrated to gtag.js

I have an angular.js webapp and I´m using Google Analytics. On 16/11 I migrated my angular webapp from the old universal google-analytics.js script to the new gtag.js. Next day, I stopped to getting analytics information. See the screenshot.
Some considerations:
I just migrated the js in the webapp.
I didn´t change any configuration in Google Analytics.
With the purpose to filter spam or ghost visits in GA, since a long time ago I had a filter that I "expected" to allow analytics only from my site, like:
I have another view without any filter that I have noticed that seems to work. So for some reason the root cause could be the filter.
Also, I have noticed the real-time reports are not working in the filtered view, however, they are working in the not filtered view.
For testing purposes, I have just deleted the filter and the real-time reports are working.
As before the migration, I was gathering analytics, I think there are maybe different problems:
- It looks like the filter is (and was before the migration) preventing the real-time reports to work.
- Apart from the real-time reports, the analytics were working before the migration with the filter, so is there anything that needs to be changed, updated in analytics in order the gtag.js works. Or maybe gtag.js is working in a different way that makes analytics not work with that filter?
- Should I delete the filter in any case? At the moment, I have deleted it and I will observe what happens with the analytics next days. However, if I delete the filter the spam analytics will come back. Maybe there is another filter that has to be applied.
UPDATE:
I attach the Gtag.js script in the website:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-property-1"></script>
<script>
var gaEnv;
switch (window.location.hostname) {
case 'www.domain.com':
gaEnv = 'UA-property-1'; // production
break;
case 'www.test.domain.com':
gaEnv = 'UA-property-3'; // test
break;
default:
gaEnv = 'UA-property-2'; // development
}
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
// Config for analytics
gtag('config', gaEnv, { 'send_page_view': false });
// Config for Adwords
gtag('config', 'AW-property');
</script>
As I use Angularjs I hace developed a service to track page views, like:
service.trackPageView = function (url) {
gtag('config', trackingId, {'page_location': url});
/* Old code for google-analytics.js. deprecated with gtag.js
* ga('set', 'page', url);
* ga('send', 'pageview', url); */
};
UPDATE 2: I have installed Google Tag Assistant. It looks like it´s gathering the data and tracking the pageviews. The only strange thing that I see is the alert: This hit is missing the hostname in the URI.
UPDATE 3: As I suspected, when I delete the "include only" my hostname filter in the analytics view, the data is gathered again as you can see in the pic.
So, it´s pretty clear that gtag.js is sendind data from the app to Analytics to the right property.
Now, the only point is that I had that filter that I have deleted to filter the ghost analytics spam. So, I assume it will be back soon. Again to the starting point.
My concern is if it´s maybe related to the info that I send to analytics or maybe the filter was wrong. So, I think I will need help to setup the filter working properly with this gtag.js library.

Cross Domain Tracking with new gtag.js framework

We have a few questions related to the new gtag google analytics tracking code in relation to the cross-domain bi-directional auto linker. In there documentation, they mention you can use one line of code across all sites with unknown origin using the following code,
Documentation for Cross Site
To simplify bi-directional cross-domain tracking further, you can list
all possible domains you want to track in the domains property of the
linker parameter of property's config for each domain, allowing you to
use the same snippet of code on every domain:
On example-1.com, update the property's config:
gtag('config', 'GA_TRACKING_ID_1', {
'linker': {
'domains': ['example-1.com', 'example-2.com']
}
});
On example-2.com, update the property's config:
gtag('config', 'GA_TRACKING_ID_2', {
'linker': {
'domains': ['example-1.com', 'example-2.com']
}
});
There are some inconsistencies in this code, for one, it shows the example-1 and example-2 as using different GA_TRACKING_ID's while all the other examples on the page show the same GA TRACKING ID being used across snippets. We need to use the same piece of code, not multiple pieces. We currently are looking to use this method with ONE gtag code across all sites, not multiple gtag snippets as shown. If you reference the other examples in the link above, you will see they also use one gtag.
We have a situation where we require access to a 3rd site for cross-domain tracking, but the company will not be able to immediately give us access. We do not want to make multiple requests to website #2, so in the meantime can we add all three websites into our cross-domain linking? Will it cause a conflict to add a third website that is not actively tracking basically, but eventually will be added into the mix
Google's old documentation mentions the ability to add more than three domains, does the new gtag framework still have this ability and will it work bidirectionally.
I would assume the proper method of adding bi-directional tracking would be to add it as followed to all sites
gtag('config', 'GA_TRACKING_ID_1', {
'linker': {
'domains': ['example-1.com', 'example-2.com', 'example-3.com']
}
});
Just had a similar dilemma reading through the docs and implementing the solution.
A better step by step overview, that includes a link to the docs you have referenced in your question, is this Cross domain tracking with gtag.js
Code that worked in my case, and that I will mention in my answer bellow, was implemented for a situation where I needed to implement Bi-directional cross-domain tracking across multiple domains. Some of the domains that also needed to be tracked where actually sub-domains (subdomain.example.com), for them you just need to include the root domain (example.com) in the linker parameter domains property array.
Example:
gtag('config', 'GA_TRACKING_ID', {
'linker': {
'domains': ['example.com', 'second-example.com', 'third-example.com']
}
});
And the complete code, including the script src tag if someone needs it:
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', 'GA_TRACKING_ID', {
'linker': {
'domains': ['example.com', 'second-example.com', 'third-example.com']
}
});
</script>
To answer your questions:
You need/have to use one Tracking ID (also known as Property ID). That example snippet confused me as well. But you need to use the same Tracking ID as it gathers data to a single property, and that's what you want with cross-domain tracking. More about Properties here.
I don't see a problem with adding the third website domain to the domains array. The gtag tracking code gets activated only when there is a user on the website where the code is implemented. So in your case, Google Analytics just won't be able to track the traffic on the third domain until you add the appropriate code to it. The tracking on the first two websites where the code IS implemented won't be affected by this.
Yes it will work. Working example displayed above this list.
Yes, that is correct. But you've also included that extra numbering after the Tracking ID in your example that got us both confused initially :)
Of course this isn't all you need to do in order to setup a correct cross-domain tracking. As mentioned in the document referenced at the top of my answer you will also need to:
Create a copy of the reporting views and add a custom filter if you want to get full domain names in your reports. It's easier to differentiate the visits between domains this way (example: you may have an about-us.html page on every domain).
It's important that you add the tracked domains to the Referral Exclusion List. If you don't do this, cross-domain tracking will not work. It will register a single visitor going from the second-example.com to example.com as a new session.
Extra tip from the docs, that proved really helpful:
For quick testing of the above setup, you can use a Chrome extension developed by Google called "Google Tag Assistant".
It saved me a lot of time in debugging the issues.
Especially useful when working on live sites with a lot of traffic. About Tag Assistant Recordings

Google Universal Analytics Enhanced Link Atrribution

I'm using UA on our company's in-house software to help understand how our users use it, and part of that is learning what they click when they click and so on.
Enhanced Link Attribution seems to be the best choice for this, but per the Developer Docs:
Tag your page for enhanced link attribution
In order to implement this additional tagging for enhanced link
attribution, you have to use the asynchronous version of the Analytics
tracking code.
The problem I'm seeing is that currently, I'm using Universal Analytics which uses analytics.js whereas the Asynchronous version of GA uses ga.js. So now I'm confused because the option is available in my property settings in the Admin section in our GA account.
Univeral Analytics
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-CODE-HERE', 'SITE_URL');
ga('send', 'pageview');
</script>
Asynchronous Code
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Since the two versions of GA are not compatible, can I still use Enhanced Link Attribution? If so, what steps would I take? I can't seem to find the answers in the Google Analytics docs related to ELA with UA.
Edit
Is it at all possible or advised to use both versions of Google Analytics on the same page/site/property? Assuming I set up another GA property for the standard version and use both JS snippets on the site?
To answer the original question:
No, enhanced link attribution is not yet supported by Universal Analytics. Though this and many other features will be rolled out soon enough. Universal Analytics is still very beta, but it's been established that this is the future for google analytics.
Yes, the new code is asynchronous just like the old code, and I really couldn't imagine a situation where you would want to turn this off. Asynchronous loading in this case means that when the analytics javascript fires, your web page continues loading regardless of whether the javascript has finished loading or not. Before the asynchronous snippet update, it was best practice for the analytics code to be loaded in the footer to prevent the entire page from hanging due to the script not being asynchronous in nature. Though this was changed because on long/slow pages, the user would often interact with the website before the footer/javascript had a chance to load, and in turn caused major discrepancies in the data.
Wikipedia:
In computer programming, asynchronous events are those occurring
independently of the main program flow. Asynchronous actions are
actions executed in a non-blocking scheme, allowing the main program
flow to continue processing.
I also wouldn't suggest changing the object name as Concept Rat suggests, as I believe that would only apply if you were implementing multiple "universal analytics" trackers to different web properties within the same snippet.
https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#snippet:
Renaming the Global Object
In some cases the ga variable name might already be used by an
existing object on your page. To avoid overriding your existing
object, you can rename the ga function, for example to __gaTracker. To
do this, simply replace the ga parameter in the snippet above:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new
Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','__gaTracker');
Then you can use __gaTracker instead of ga when calling commands:
__gaTracker('create', 'UA-XXXX-Y');
__gaTracker('send', 'pageview');
If renaming the variable were necessary to load both snippets, I don't believe Google would say this:
https://developers.google.com/analytics/devguides/collection/analyticsjs/:
The analytics.js snippet is part of Universal Analytics, which is
currently in public beta. New users should use analytics.js. Existing
ga.js users should create a new web property for analytics.js and dual
tag their site. It is perfectly safe to include both ga.js and
analytics.js snippets on the same page.
Also please note, if you want to try out universal analytics you should run it concurrently with your existing implementation, as they should eventually release a migration tool to remain backwards compatible allowing you to keep your existing data. To be perfectly clear:
You should only fully implement universal analytics if you're are creating a brand new account with no existing data in place.
I've been researching this same problem. As of November 2012, Google said the following in response to a support question: "In-Page Analytics support for analytics.js is not yet implemented. This is one of the features we'll be working on and introducing later in the beta. Other features not currently supported include Remarketing and AdSense reporting." As you know, Enhanced Link Attribution is a feature of In-Page Analytics.
I have not found any new references to this issue since that post, so I can only assume they Universal Analytics is still not ready for prime time. If you can, I would try using Asynchronous Code until Universal Analytics is working properly.
FYI: Universal Analytics is out of Beta testing and Enhanced Link Attribution is now supported:
https://support.google.com/analytics/answer/2558867?hl=en
For your reference the Universal Analytics is, or does support, asyncronous mode. You can see it in the Universal JS code third line down under the "script" tag "a.async=1;".
You can also run both the standard GA code and Universal at the same time. Just setup a separate property for the Universal code and make sure to change the object name "ga" that you see on the fourth line "//www.google-analytics.com/analytics.js','ga')" to say "gau" (just has to be unique on the page for scripts). Then use "gau(" instead of "ga(" for setting things, etc. Remember this is just for the Universal code not the standard GA.
Once you've done this you can continue to track things using the standard GA and have the Universal GA logging underneath the new property. Once you're happy with things you can switch to using only the Universal one.

google analytics - multiple trackers on one page (traditional and asynchronous)

I'm writing a widget that will be embedded in other people's websites. I'm using Google Analytics to track all the people that visit all instances of my script on the embedding websites. I understand that the new asynchronous tracking code resolves this problem so that there's no interference with those websites' own Google Analytics accounts.
However, what if my analytics tracking code uses the new asynchronous tracker, and the embedding website is using the legacy (traditional) tracking code? Would there be any interference between these two blocks of code?
My tracker code:
<script>
...
_gaq.push(
['myTracker._setAccount', 'UA-XXXXX-2'], // naming the tracker 'myTracker' to be different from any default tracker already defined
['myTracker._trackPageview']
);
...
</script>
Embedding website's code:
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-1");
pageTracker._trackPageview();
} catch(err) {}</script>
Can pageTracker and myTracker co-exist and register analytics information to their own separate accounts?
As far as official advice goes this is not a supported scenario:
http://www.google.com/support/forum/p/Google+Analytics/thread?tid=3490dc16fec867c5&hl=en
And there are pitfalls:
http://www.lunametrics.com/blog/2009/02/26/pitfalls-tracking-multiple-accounts-ga/
When you say embedded in peoples websites if you mean that your widget lives inside an iframe then you dont need to worry.
If the code is going to be loose in the page then you should try to use a different variable name to the default one so it doesn't clash. This means change the var _gaq = ... to something else and then renaming it throughout your code.
http://www.google.com/support/forum/p/Google+Analytics/thread?tid=5c7a6e5751bf5789&hl=en
I'm not entirely clear on your setup but if it is being inserted into many domains you are going to need to set up multiple GA accounts and use different account numbers per user.
If this is for clients that you have access to then you might consider simply adding an extra user to the report so that you can view all the accounts from one central account:
http://www.google.com/support/analytics/bin/answer.py?hl=en_US&answer=55500&utm_id=ad

Google Analytics - async tracking with two accounts

I'm currently testing GAs new async code snippet using two different tracking codes on the same page;
_gaq.push(
['_setAccount', 'UA-XXXXXXXX-1'],
['_trackPageview'],
['b._setAccount', 'UA-XXXXXXXX-2'],
['b._trackPageview']
);
Although both codes work, I've noticed that they present inconsistent results. Now, we aren't talking huge differences here, only 1 or 2 visits / day every now and then. However, this site is tiny and 1 or 2 visits equates to a 15% difference in figures. Now, the final site has much more traffic, but my concerns are;
will this inconsistancy scale with traffic?
assuming not, is a slight variation in recorded stats an accepted norm?
You can avoid the conflicting cookies by setting a different domain for google analytics.
<script type="text/javascript">
//<![CDATA[
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-NNNN-1']);
// primary profile
_gaq.push(['_setDomainName', 'www.domain.com']);
_gaq.push(['_trackPageview']);
_gaq.push(function() {
// create the second async tracker
_gaq._createAsyncTracker('UA-NNNN-2', 'blogTracker');
});
// secondary profile (this is the default domain setup if not specified)
_gaq.push(['blogTracker._setDomainName', 'domain.com']);
_gaq.push(['blogTracker._trackPageview']);
//]]>
</script>
This will keep the cookies separate.
Note: I am using this setup to track events in a second profile to keep my bounce rate numbers accurate. The second profile tracking code is only used on my blog, thus, is not a complete profile on purpose.
Are they from different accounts ?
If so check follow statement from GA website
Multiple Analytics Accounts on a Given
Page Some users want to track the same
page or set of pages in multiple
Analytics Accounts. Analytics is
designed to work effectively with a
single account-to-web-property
relationship. If you have multiple
accounts tracking the same web
property (e.g. page or sets of pages),
both accounts will read from and set
the same set of cookies. This set up
is generally not recommended.
Another thing to consider with tracking in multiple accounts is that any events in the page will be sent to whichever account was set last in the _setAccount call. I spent months wondering why my events weren't showing up under my main account and then realized we had an extra set of tracking code appearing after the main tracking code with a call to _setAccount for the affiliate account. My event tracking code appeared to be working fine, but events never showed up in my account. Moving the affiliate code to before our main tracking code solved the problem.

Resources