I'm trying to introduce Google Analytics in a sort of desktop app that all loading files are done through file:// not http or localhost (because most of the time it works offline). Any attempt to track app uses with Google Analytics or Google Tag Manager wasn't work.
For example, I've used:
ga('create', 'UA-XXXXX-Y', 'auto');
ga('set', 'checkProtocolTask', null);
ga('set', 'checkStorageTask', null);
ga('send', 'pageview');
As explained here (adding ga('set', 'checkStorageTask', null);
) with no results.
Has anybody deal with this situation?
I run a very quick test. ga('set', 'checkStorageTask', null); did nothing for me, instead I found it necessary to set storage to none on tracker creation (which makes sense, since you cannot set cookies with the file protocol). This also means that you probably won't have session tracking, since each hit generates a new ID.
ga('set', 'checkProtocolTask', null) seems necessary - else the debugger complains (naturally) that file is not a valid protocol.
After that data was sucessfully sent, but did not show up in the realtime view. I suspected that maybe the reporting engine does not like the file protocol and set the "location" field with a correct protocol. So I ended up with:
ga('create', 'UA-XXXXXX-5' , {'storage':'none'});
ga('set', 'checkProtocolTask', null);
ga('send', 'pageview' , {'location' : document.location.href.replace('file','http') });
and that displayed in the realtime reports. This was a real quick test, so you need to verify this independently. Notice that you do not need to set a cookie domain (the "auto" argument in your code example) since you cannot set a cookie in any case (there is no domain to set the cookie to).
Also if you work offline for most of the time GA will not work (you need to load the analytics.js file and you need sent calls to the tracking server), but then you are probably aware of that.
Related
I am implementing google analytics for the first time. Here are the steps I took.
I went to analytics.google.com and created an account. Then I created a "property". After that I created a "Data Stream" for my website and got a "Measurement ID which starts with a G-XXXXXXX. Then I implemented the following code in the header (before any other css or js calls)
<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'G-XXXXXXX', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->
When I look at the network tab, I can see that the call was made:
Request URL: https://www.google-analytics.com/j/collect?v=1&_v=j96&a=316116739&t=pageview&_s=1&dl=https://mywebsite.com&ul=en-gb&de=UTF-8&dt=My Website Title&sd=24-bit&sr=1920x1080&vp=1920x577&je=0&_u=ABC~&jid=1234&gjid=1234&cid=1234.1234&tid=G-XXXXXXX&_gid=1234.1234&_r=1&_slc=1&z=1234
Method: Post
Status Code: 200
However when I look at google analytics, it says "Data collection isn't active for your website".
I waited 24 hours to ensure it is not some delay problem but there is still no data and there have been visitors on the website, at the very least myself.
Is there a step I missed or maybe it could be a configuration issue? I haven't been able to find any clues as to why it might not be working. Any help is most appreciated.
Note: The values above in Request URL have been modified to remove any sensitive information. All the parameters are there, just values have been changed to ABC and 1234 as its not necessary to see those.
If you want to track with GA4 (G-xxxxxxxx)
You need to use gtag.js for it.
ga.js is not support for GA4
Here is the document
I have a special situation where the sites visitors can access the page from a certain domain but no others. So HTML and assets are no problem as long as they are stored on the server. Google Analytics on the other hand requires a download of analytics.js from Googles servers, which is impossible.
So I'm looking for a way to proxy this. The webserver itself has internet access and could relay the trafic. To report to Google about my page view, a single pixel GIF is downloaded from Google, described here: https://developers.google.com/analytics/resources/concepts/gaConceptsTrackingOverview
I think it would be kind of easy to get all the parameters in the GIF and use the measurement protocol to report to Google from the server - but the hard bit is to get all this info to the server. To download analytics.js and modify it to go to my own server seems to me as a hack that ain't future proof at all. To just get the current page from the user to the server is not a big deal, but we would like to get the user id, browser version and everything you get with Analytics.
How would you do it? Do you find a solution for this?
Update: Google has since released server-side GTM, which allows you to proxy requests and scripts through a custom domain. In most use cases I can imagine, this would be the much superior solution to a dyi proxy.
As pointed out in my comment the utm.gif is no longer used. Google Analytics has completely switched to the Measurement Protocol and data is now sent to the Endpoint for the Measurement Protocol at google-analytics.com/collect. Actually this still return a transparent pixel since calling an image with parameters is a probate way of transmitting informations across domain boundaries.
Now, you could just the Measurement Protocol to implement your own Google Analytics tracker.
To quote myself:
Each calls includes at least the ID of the account you want to send
data to, a client id that allows to group interactions into sessions
(so it should be unique per visitor, but it must not identify a user
personally), an interaction type (pageview, event, timing etc., some
interactions types require additional parameters) and the version of
the protocol you are using (at the moment there is only one version).
So the most basic example to record a pageview would look like this:
www.google-analytics.com/collect/v=1&tid=UA-XXXXY&cid=555&t=pageview&dp=%2Fmypage
You probably would want to add the users IP (will be anonymized automatically) and the user agent.
However it sounds like you prefer to use the standard Analytics code to collect the data and relay the tracking call via your own server. While I haven't used the following in production I don't see any reason why it wouldn't work.
First you need the analytics.js file. Self-hosting the file is discouraged, but the given reason is that the code is updated sometimes by Google and if you host it yourself you might miss the updates. This can be remedied by setting up a cron job that downloads the file regularly to your server so you always have a current version.
Next you'd adapt the GA bootstrap function to load the code from your own server:
(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.myserver.com/analytics.js','ga');
Now you have the code, but the tracking call will still be sent to the Analytics Server (i.e. in your case it won't be sent at all). So you need to re-route the call via your server.
To make this possible the Google (Universal) Analytics Code has a feature called "tasks". Tasks are functions within the tracking code in which the tracking call is being assembled.
It is possible to modify tasks by using the "set" function of the tracker object, using the taskname as parameter and passing a function that overwrites/overloads the task function.
The following is pretty much the example from the Google documentation (except I omitted the part where data is still being sent to Google - you don't need this at this point):
ga('create', 'UA-XXXXX-Y', 'auto');
ga(function(tracker) {
tracker.set('sendHitTask', function(model) {
var payLoad = model.get('hitPayload');
var gifRequest = new XMLHttpRequest();
var gifPath = "/__ua.gif";
gifRequest.open('get', gifPath + '?' + payLoad, true);
gifRequest.send();
});
});
ga('send', 'pageview');
Now this sends the data to a file called __ua.gif at your own server (if you need to send data cross-domain you can simply do a var ua = new Image; ua.src = gifPath + '?' + payLoad to create an image request).
The model parameter to the sendHitTask-function contains (apart from a lot of overhead) the payload, that is the assembled query string that contains the analytics data. You can then make your _ua.gif a script that proxies the request to the google-analytics.com/collect.
At this point the user agent will be your script and the IP adress will be that of your server, so you need to include &uip (User IP override) and &ua (User agent override) parameters ( https://groups.google.com/forum/#!msg/google-analytics-measurement-protocol/8TAp7_I1uTk/KNjI5IGwT58J) to get geo and technical information.
If you are feeling more adventurous you can override the buildHitTask instead and try and add the additional parameters there (more hassle probably since you'd need to get the IP address from somewhere).
For additional parameter see the reference for analytics.js and the Measurement Protocol.
I released a new version of my website today and with the old version, I was around 90 visitors an hour, and after the update, it dropped to around 20 visitors.
I don't know what went wrong. Here's the stats:
As you can see, a drop occurred between 3pm and 4pm and drop to finally get to 0 since then.
I added an httpS connection, but the non www and http all redirects to https://www automatically. The tracker is the same, I only added
ga('set', 'forceSSL', true);
ga('set', 'appVersion', '2');
ga('send', 'pageview');
There is only one page (it's a basic app), so there is no other page that could have the tracker removed.
Now, I'm wondering something, when I start the tracker, I send this info :
ga('create', 'UA-XXXXX-X', 'voilanorbert.com');
shouldn't it be ga('create', 'UA-XXXXX-X', 'www.voilanorbert.com');? (with "www.") Is this the issue?
Big Update: I decided to create a new tracker in Google to see if this was a reason, and yes it is! A few minutes after I changed it to the new tracker, the data went to 1 visitors per hour, to 18 in real time! There is clearly a problem in Google Analytics!
Your tracking code was flawed. This does not imply Google Analytics does not work.
The appVersion parameter is reserved for mobile apps and not for the web collection API.
forceSSL is kind of obsolete now
When in doubt, stick to the regular tracking code or use Google Tag Manager.
I have added the Google Dynamic Re Marketing Tag to my website by use of my GTM account (test#gmail.com) successfully; it was shown with minor warnings in Google Tag Assistant as well. But, when I later switched to another GTM account (original#gmail.com) with the same settings it shows an error: “No HTTP response detected”. How this can happen when both the GTM accounts have the same data ? Any help appreciated.
Thanks!
This means clearly the code is not getting executed, could be syntax error, or extra parameters...etc. In my scenario I was sending transaction as part of Ecommerce,
(ref : https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce )
ga('require', 'ec', 'commerce.js'); ///HTTP RESPONSE NOT DETECTED..error.
ga('require', 'ec'); ///SOLVED THE ISSUE
so on similar grounds, check the syntax!
I've been noticing this error on Chrome's console for a while now:
I modified Google's script so that it logs the error, because it uses try{} catch{}, and this is what I got:
I haven't noticed considerable changes in the stats, it's always in ups and downs.
Also, this isn't only on my sites, but fricking everywhere. I haven't found bug reports or anything like that.
If I go to http://www.google-analytics.com/ga.js on the browser, it loads normally.
Does anyone have a clue of what causes this?
It was a problem with AdBlock. I disabled it and now it loads it normally.
yagudaev suggests (read answers below) that in order to keep AdBlock from blocking Google Analytics, you need to edit the snippet provided and explicitly use https:// instead of the protocol-relative URL by default. This means changing
'//www.google-analytics.com/analytics.js'
into
'https://www.google-analytics.com/analytics.js'
Example:
<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-XXXXX-XX', 'auto');
ga('send', 'pageview');
</script>
It could also be your hosts file, here's mine:
$ grep -ni "google-analytics.com" /etc/hosts
6203:# 127.0.0.1 ssl.google-analytics.com #[disabled = Firefox issues]
6204:127.0.0.1 www.google-analytics.com #[Google Analytics]
If it's an offline app (ie, you've defined a cache manifest) be sure to allow the network request.
See HTML5 Appcache causing problems with Google Analytics
The reason you are running into problems is because AdBlock will block this script if and only if it does not go through https. Notice the error you get it contains an http: protocol reference.
All you need to do is change the snippet to force it to go through an ssl connection by adding an explicit protocol instead of the protocol relative url that is the default.
<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-XXXXX-XX', 'auto');
ga('send', 'pageview');
</script>
This error is commonly caused due to one of the extensions installed within Chrome.
There are a few ways to debug and solve an ERR_BLOCKED_BY_CLIENT message.
Disable the extension.
Whitelist the domain.
Debug the issue.
I would recommend to find more detail at How to Solve ERR_BLOCKED_BY_CLIENT
2019 update
This has become very widespread now.
Solutions
Ask people to unblock your website, (bad idea from personal experience)
Host google analytics script locally (bad idea) because google says so HERE
Referencing the JavaScript file from Google's servers (i.e.,
https://www.googletagmanager.com/gtag/js) ensures that you get access
to new features and product updates as they become available, giving
you the most accurate data in your reports.
Use Server side analytics. This is what people are doing nowadays. If you are on node.js, use a library such as analytics or universal-analytics
I've noticed same thing on my browser some time ago.
Did you sing in to chrome using your Google account maybe? Or did you choose in any way to opt-out from collecting data on Google Analytics ?
Maybe Google remembers that option and uses it on Chrome when you are singed in..
BTW. I can normally open http://www.google-analytics.com/ga.js in browser, it just doesn't work when automatically loaded.
Ensure Fiddler (or similar proxy) is not active.