How to prevent google analytics from getting traffic from localhost? - javascript

I have an issue.
I use google analytics in a small project, and I develop on localhost. Of course I call my site again and again on my local machine, but this pullutes my analytics stats.
What can I do?

Thats quit simple.
you have the possibility to filter it in analytics, but this option
jungle in analytics is a bit confusing in my opinion.
you can just do it in your javascript:
if (window.location.hostname != 'localhost' && window.location.hostname != '127.0.0.1') {
//put your analytics here
}

Instead of hard-coding your google analytics key inside the rendered HTML, I suggest to put it in a configuration file.
It's typical to have several config files - at least one for the production app and one for a developer setup. Then in the developer settings you can disable GA altogether or just put a different key (for a testing site, so that you can e.g. check if your event tracking works properly).
Another option is to use the Google Analytics debug version and disabling the network part, so that you can preview the hits in console without actually registering them:
ga('set', 'sendHitTask', null);
I'd rather avoid checking for localhost host in Javascript. What if another developer uses localhost:8000? What if you deploy to a staging server? Config files are more reliable and flexible.

Related

Error when using G-Suite Developer console with Google Sites embedded API

I've written some code that retrieves some data from google sheets then updates some content on my google sites. However, while the script works (when run on localhost) I encounter the
"details": "Not a valid origin for the client: https://966655698-atari-embeds.googleusercontent.com has not been whitelisted for client ID MY-ID. Please go to https://console.developers.google.com/ and whitelist this origin for your project's client ID."
However, I enabled this for localhost, cleared my caches. The problem is the 'https://966655698-atari-embeds'. Each time the google site loads it generates a new random number sequence. Does anyone know how to workaround this? The google site uses embedded html which I believe is why the initialization failed.
I have tried to white-list https://googleusercontent.com which didn't work (I didn't think it would because the domain changes) but I'm honestly incredibly stumped.
Google hosts all user content using their somedomain.googleusercontent.com. I do not know for certain, but I'm almost sure that to save space they dynamically host their content, meaning that when the embedded html does not need to be actively hosted, it isn't. I had to find a way to host from a site that would always send the request. For me, I found that github pages was the answer.
I found this on adobe's website which somewhat explains what googleusercontent does. https://helpx.adobe.com/analytics/kb/googleusercontentcom-instances.html
To set up github pages this link will explain how to do so https://guides.github.com/features/pages/
You can add this to the developer Google console relatively easily and any connection will submit from your username.github.io. (I believe it also uses https protocol). It also allows me to implement directly using git version control and implements nicely with WebStorm.

How to use google adsense on local host ? Also do not have a google adsense account

I've been searching the internet for the last 2 days and everywhere I got was:
Add this line in the script , `google_adtest = "on";
But nobody is saying how to get the code without registering the domain. I have a google adsense account but it is not registered for a website, it is registered for google admobs.
I believe google_adtest is now redundant and from a time when it was not necessary to whitelist domains. However, I suppose you could white-list any arbitrary domain and then use that ad code for a local host setup. What is your aim? If it is to test ad serving, then you might be better suited deploying to a staging version on your live domain; hidden from real-world users, but still reflecting a production environment.

Google Analytics Instant Tester

Given that it takes around 24 hours for Google Analytics data to update, this makes it hard to see how data rolls up and is displayed within Google Analytics.
Is there some sort of instant tester, or fast turn around application that allows me to quickly set up custom dimensions/metric within my app and see how they appear?
Options for real time analytics:
Google Realtime Analytics - Built into Google Analytics. To use Google Analytics Realtime, login to your google analytics account and select your web property. Then, on the left hand side click Real-Time->Overview.
GoAccess - If you have access to your web server log files this will give you a real-time view of traffic and does not require anything more than a console access. The really nice things about GoAccess is that it does not rely on any 3rd part services and has the ability to run in real-time or generate reports. To use GoAccess, first install it on your server using either the package manager or in a local directory using the official Git. Then, if you are running a standard Apache configuration just run the executable with:
# goaccess -f /var/log/apache2/your-website-access.log -a
If you are running a non-standard Apache log configuration (or another web server entirely), then you have to give GoAccess a description of your log file. This can be done in the ~/.goaccessrc file. Refer to the GoAccess documentation for specific descriptors in generating a string that describes your log file lines.
There are also a host of other SAS options like Clicky, GoSquared, or piwik (which is open source).

HTML/Javascript Offline Tracking Localhost Without Analytics

What I am looking to accomplish is when a website is hosted locally on the localhost if it would be possible to track the clicks/navigation kind of like google analytics does but a very simplified version of having time stamps, and counts using primarily HTML5 and Javascript. I haven't found much information doing something like this with localstorage either. Primary reason is to keep tracking information for what information would be accessed without being online/needing a internet connection.
If I remember correctly one of the issues of localhost, is the inability to have scripts change files on the localhost directly(AJAX) and would require to be sent to a server. Preferred if it did not require server side languages such as php, etc.. Since how this would be used would not always have mamap/wamap installed.
Any help would be great, or a direction.

Google Analytics to track FireFox extension use

I'm developing a Firefox extension and would like to track its use with google analytics, but I can't get it working.
I've tried manually calling a function from ga.js, but that didn't work for some reason. No error was produced, but neither was any data collected.
My last attempt was to have a website that just holds the tracking javascript and then load it within the extension in an iframe with the URL configured so it contains meaningful data. This way the analytics are getting connected when I visit said webpage with a browser, but not in an extension. I've tried putting some visible javascript on the site and have confirmed the site's javascript is executing. This method also works with other trackers, but I don't like their output and would prefer Google Analytics.
Any ideas what else I could try to accomplish this?
The solution is to use Remy Sharp's mini library for tracking bookmarklets and extensions with Google Analytics. Works like a charm.
Usage is as simple as:
gaTrack('UA-123456', 'yoursite.com', '/js/script.js');
Note that, since it doesn't use cookies, there's no differentiation between pageviews and visits, or for that matter, between visits and visitors. But, the rest of the functionality is fairly reliable.
Depending on what you want to track you may not need Google Analytics. Mozilla's addon.mozilla.org portal already provides comprehensive tracking and usage statistics for addons.
To check if Mozilla provides what you need go to the Statistics Dashboard and choose the statistics for one of the publicly available addons.
Here is a small library to proxy the requests through an iframe hosted on another server: https://github.com/yelloroadie/google_analytics_proxy
This gets around the bug in the add-on sdk that causes ga.js to die (https://bugzilla.mozilla.org/show_bug.cgi?id=785914).
This method allows full use of google analytics, unlike the limited use found in the library by Remy Sharp.
I don't think this is possible. Firefox extensions don't allow you to load pages from other servers. So the only way I can think of is to have an invisible iframe load up the code. The pings to Google's servers need to be from a domain belonging to you. So I guess your own servers have to serve up pages every time a user loads the extension, which just kills your server and defeats the purpose of Google doing all the work!! Please post if you have found a way around it. Chrome extensions can be tracked easily!
For using analytics in the main/background script you might want to use this solution:
https://stackoverflow.com/a/17430194/193017
Citing part of the answer:
I would suggest you take a look at the new Measurement Protocol in Universal Analytics:
https://developers.google.com/analytics/devguides/collection/protocol/v1/
This allows you to use XHR POST to simply send GA events directly.
This will coexist much better with Firefox extensions.
The code would look something like this:

Categories