Viewing page locally locks up some browsers - google-analytics

I just started replacing my legacy Google Analytics tracking code with the newer "universal" code, and have run into a problem viewing pages locally. I'm not talking about using localhost - I'm talking about simply opening a page like C:\My Documents\whatever\index.html. No problem in FF28. Chrome34 chokes for a while, but recovers. IE11 locks up completely, and it's Task Manager Time.
The reason is clear - when viewing the page locally, it's trying to load the script from file://www.google-analytics.com/analytics.js, which doesn't exist.
Does anyone know how to get around this? I do cross-browser testing before uploading my stuff anywhere - I really need to be able to view the pages locally.
I've found that replacing one chunk of the code snippet appears to work, but I don't know what bad effects it might have. Replacing this:
(window,document,'script','//www.google-analytics.com/analytics.js','ga')
With:
(window,document,'script','http://www.google-analytics.com/analytics.js','ga')
Seems to work, but...?

It appears that my approach will work, but only as long as I'm certain that I will always be serving my pages with the http: protocol, and never with https:
The "book" solution appears to be this:
})(window,document,'script',('https:' == document.location.protocol ? 'https://' : 'http://') +'www.google-analytics.com/analytics.js','ga');

Another workaround to avoid the "local lockup" problem:
if(location.protocol!=='file:') {
(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-999999-9');
ga('send', 'pageview');
}

Related

Solving configuration mismatch between Google Analytics and Google Optimize?

So I'm using Google Analytics, Google Optimize, and Google Tag Manager. I also feel that I may be losing my mind.
For performance reasons, we don't want to add the google optimize script through GTM. We want to hard-code it. But every time I try that, the installation doesn't work. Specifically, when I click "Run Diagnostics" on the google optimize setup page, I get an error that says
All Analytics tracking code on the page must have the same tracker configuration settings. [...] The following tracker configuration properties do not have the same configurations: alwaysSendReferrer.
So I go into my handy Analytics debugger, find the alwaysSendReferrer property, and note that it's set to true. Great. I edit the config object that i'm passing to the google optimize init script, and I add alwaysSendReferrer: true. So far so good. Except... I get the same error.
I thought maybe something was wrong with our analytics setup, so I got rid of the hardcoded google optimize script and instead included it via the Google Optimize GTM tag -- which, unexpectedly, worked perfectly. I thought "Okay i'll just find the script that GTM is adding and copy/paste it into the codebase" but of course it's minified within an inch of its life, and untangling that javascript from the rest of the tags would be a whole other endeavor.
I have access to a {{Default UA Settings}} variable in GTM, and google's documentation would really like me to just pass that object to the official "Google Optimize" tag in GTM. That works (I've tested it), but as mentioned, we don't want to do that.
If anyone knows what the Google Optimize config object should look like, that would help me massively.
Right now mine looks like this (with identifying details removed)
dataLayer.push(['config', 'UA-12345', {
'optimize_id': 'GTM-55555',
'linker': { 'domains': ['mywebsite.com <http://mywebsite.com>'] },
'alwaysSendReferrer': true
});
I've tried false, "true", 1, and basically everything else I can think of (I'm quite confident that Google Analytics has this property set to true on our site) -- my only guess is that maybe I'm setting the value in the wrong place and google optimize isn't seeing it. Help?
I know the feeling of losing your mind trying to read through Google documentation!
This is the code I use on pages I wish to run Optimize experiments on:
<head>
...
<!-- Google Tag Manager --> <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-PRMQXXX');</script> <!-- End Google Tag Manager -->
<!-- Page-hiding snippet (recommended) -->
<style>.async-hide { opacity: 0 !important} </style>
<script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
(a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
})(window,document.documentElement,'async-hide','dataLayer',4000,
{'GTM-KL7XXXX':true});</script>
<!-- Modified Analytics tracking code with Optimize plugin -->
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-99999999-1', 'auto');
ga('require', 'GTM-KL7XXXX');
ga('send', 'pageview');
</script>
...
</head>
Where:
GTM-PRMQXXX is the Google Tag Manager container id
GTM-KL7XXXX is the Google Optimize container id, and
UA-99999999-1 is my Google Analytics id
Note that you need to remove your usual GA script once you add the scripts above.
I hope this helps you!

Google translate has stopped working in Chrome

Google translate has stopped working in Chrome but still working in Firefox. The code is below and it was working for long time before now:
<script>
function googleSectionalElementInit() {
new google.translate.SectionalElement({
sectionalNodeClassName: 'can-translate',
controlNodeClassName: 'translation-control',
background: '#ffffcc'
}, 'google_sectional_element');
}
var url = top.location.href,
pos = url.search(/\/\w\w(\W|$)/),
lang = url.substring(pos+1,pos+3),
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = false;
ga.src = 'https://translate.google.com/translate_a/element.js?cb=googleSectionalElementInit&ug=section&hl='+lang;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
</script>
At the moment here is error:
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://translate.googleapis.com/translate_a/l?client=te&alpha=true&hl=en&cb=_callbacks____0jtqeel7c with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.
If it go to the proposed link, here is suggestion:
Make sure these resources are served with a correct "Content-Type" response header from the list below, as well as a "X-Content-Type-Options: nosniff" response header.
Also there is message at https://translate.google.com/intl/en/about/website/
We no longer provide new access to Google Translate's Website Translator. This change does not affect existing use of the Website Translator.
This has affect in Chrome only, code above is Google only and I've no idea where and how to add "X-Content-Type-Options: nosniff".
UPD: there are both of two necessary headers.
Does Google blocked itself and does anyone know how to fix it?
UPD1: Google translate error began to appear and in Firefox 66.0.1 too. It is
The resource from “https://translate.googleapis.com/translate_a/l?client=te&alpha=true&hl=en&cb=_callbacks____0jttrta1j” was blocked due to MIME type (“application/json”) mismatch (X-Content-Type-Options: nosniff).
It looks like some glitch of translation service, after one page refresh it works, after another - server returns incorrect data, json instead of javascript or something like this
At this point, if you manually delete a cookie with the name "NID" for the host .translate.googleapis.com, you can process the page(CORB error disappears)
By the way, repeated page calls also sometimes remove this error. But the solution to the problem is as strange as its occurrence.
Please open a bug via https://crbug.com/new and try to provide as many details as possible:
Did the issue work in Chrome 72 and stopped working in Chrome 73?
Is there a repro page that shows the problem? What is the expected VS observed behavior?
Does the issue repro without any Chrome extensions present?
Does the broken page use AppCache?
I've encountered this issue on multiple sites, it first started on Chrome on my Pixel 2.
The widget does appear on the first load sometimes, if not a reload is all that's required, not even a hard reload. My guess would be that Google are trying to phase the widget out.
The widget does still work without any issues on all other browsers.

Change name of counter Universal Analytics

Help me please.
I have name for my counter Universal Analytics (analytics.js) - clientTracker, but now I want to change it.
Everithing will be Ok with it, or some statistic can be ruined?
Old and new statistic will be counted in counter correct?
Old name (I want to change it):
ga('create', 'UA-XXXXX-Z', 'auto', 'clientTracker')
New name:
ga('create', 'UA-XXXXX-Z', 'auto', 'nonStat')
What you're referring to is the tracker name (GA documentation). The only consideration when changing the tracker name is this: you need to make sure to update every ga command that you want to continue to track.
So, if you change:
ga('create', 'UA-XXXXX-Z', 'auto', 'clientTracker')
to:
ga('create', 'UA-XXXXX-Z', 'auto', 'nonStat')
...you'll also need to change:
ga('clientTracker.send', 'pageview')
to:
ga('nonStat.send', 'pageview')
...and all other instances of:
ga('clientTracker.send', ...
to:
ga('nonStat.send', ...
If yours is more advanced than the typical setup, there may also be other references, such as: ga.getByName('clientTracker'). Make sure these are updated too.
Lastly, check all the platform and third-party integrations you have on your website that might integrate with GA (e.g. a CRM) and make sure they don't rely on the existing tracker name. (A simple way is to search all code sources for the old tracker name using Chrome DevTools on multiple pages of the site, including all form pages.) Any references need to be updated.
That's it; just make sure you replace all references to the old tracker name. Once you change the tracker name in the create command, any other commands still using the old tracker name will stop working.
But first ...
Think about why you want to change the tracker name. Typically the only reason to have a custom tracker name is to have multiple trackers on the page. If you have only one tracker, chances are changing the tracker name is extra effort for no benefit.

In ga('create' 'ua-' 'auto'), what does 'auto' stand for?

As per the doc , 'auto' in ga('create' 'ua-' 'auto') is described as below.
opt_configObject – An optional object containing configuration field/value pairs.
But, I am not sure, I completely get this.
What is the difference between ga('create' 'ua-' 'auto') and say I give ga('create' 'ua-' 'mysite.com') ?
I agree the method signature and the myriad of valid combination can make it confusing.
ga('create', 'UA-XXXXX-Y', 'auto');
is the same as
ga('create', 'UA-XXXXX-Y', {
cookieDomain: 'auto'
});
More info on using 'auto' for cookieDomain can be found here:
https://developers.google.com/analytics/devguides/collection/analyticsjs/domains#auto
To answer your questions, there's no difference between specifying 'auto' and 'mysite.com' if your site's domain name is 'mysite.com', but there would be a difference if your site were hosted at 'subdomain.mysite.com'.
I had a similar issue awhile back where a client had all of his domains say:
ga('create' 'ua-' 'mysite.com')
That's basically an old method of specifying the cookie to a domain. Might as well change when google changes correct?
I changed them to 'auto' because it simplifies subdomain tracking.
See the official youtube video.
Google YouTube Video

Analytics Code Not Tracking after 24 hours

I've installed - and I believe I installed right - Google Analytics code yesterday. But still getting "not tracking" message and 0 stats. Can anyone check if there is something wrong?
I don't know if it's allowed to give urls. So, the website is my display name dot com if you want to check.
Here is my head code:
<head>
<meta charset="utf-8">
<title>Babality - Oyunlar, Hileler, Combolar</title>
<meta name="keywords" content="flash oyun, unity oyunları, 3d oyunlar, flash oyunlar, tam ekran oyun, oyun hileleri, oyun comboları, oyun komboları">
<meta name="description" content="domain.com; kaliteli ve eğlenceli oyunların, popüler oyunlara ait hilelerin ve comboların bulunduğu bir web sitesidir.">
<link href="http://domain.com/tyle.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://domain.com/jss.js"></script>
<!-- analytics script -->
<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-55572105-1', 'auto');
ga('require', 'displayfeatures');
ga('send', 'pageview');
</script></head>
I've tried some websites that checks if anaytics code works. Every site I tried saw my analytics code working.
I've tried GA Debug extension on Chrome. Can't find any errors.
I can see my code in my pages
I've tried Google Tag Assistant extension on Chrome. It found a Google tag. It was Analytics. And 0 errors.
Bottomline, everything - even Google extension - says nothing wrong. But still not tracking.
EDIT: Did not change anything, did not do anything. Code works now. Seems sometimes 24 hours is not enough. For me, code worked after 30 hours.
After you have installed the tracking code, you will need to wait at least 24 hours for stats to show up in your standard reports. What you can do is to check in Real-time reports to see if you see hits for you own simultaneous visits. If you see data there, then you should only need to wait the aforementioned 24 hours.

Resources