Using Google Analytics without a server or localhost? - google-analytics

I have spent quite a lot of time researching ways to use GA on a locally run file, but not using http://localhost:(some_port).
Every method I use never returns any regular data. This is the closest I have got to receiving anything:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-47519364-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', 'true']);
_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/u/ga_debug.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Anyone got this working in this fashion before?

Thanks for enlightening my eyes with the option of the '_debug.js' script.
I am using the analytics module and had the same problem.
So I used the debug script and got the following error when trying to send an event:
Unallowed document protocol. Aborting hit.
I googled it and found this thread exlaining how to bypass it:
https://productforums.google.com/forum/#!topic/analytics/KNz8TimivXo
This was the answer:
ga('create', 'UA-**********-6', {'cookieDomain': 'none'});
ga('set', 'checkProtocolTask', function(){ /* nothing */ });
ga('send', 'pageview');

Related

how to check which version of Google Analytic code is working?

I am working on a website, and i want to do record event event on the website. for that i have searched on Google and found Event Tracking. but i how to check whether i am using ga.js or analytic.js and which is better to use and why.
My google Analytic code is:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '[TRACKING_ID]']);
_gaq.push(['_setDomainName',document.domain]);
_gaq.push(['_setAllowLinker',true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
please help me.
Universal Analytics does not use the .push syntax. Instead, it has a function ga().
You apear to not be using universal analtyics you should check this link on how to work with Event Tracking: Event Tracking - Web Tracking (ga.js)
This link should help you understand the diffrence: About Universal Analtyics

google analytics setup, not tracking

I'm trying to implement google analytics on a page but it refuses to track anything.
the public domain is on www.publicdomain.com, when entering the page its redirected to: www.publicdomain.com/sub1/sub2/index.php
this website is hosted on another domain:
www.privatedomain.com/publicdomain/sub1/sub2/index.php
This is the GA code I'm using inside the body tag of the index.php file:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9999999-9']);
_gaq.push(['_setDomainName', 'publicdomain.com']);
_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>
Any ideas of what could be wrong?
Here are some things to check:
Are you using the correct UA number? (i.e. not 999999-99)
Are the GA cookies being created? (__utm[abcz])
Is the __utm.gif request being sent back to Google? If not, is your browser blocking it? (i.e. is Do Not Track enabled?)

shorten Google Analytics code

Can the following line in Google Analytics code:
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
be shortened to
ga.src = '//stats.g.doubleclick.net/dc.js';
?
The full script looks like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_setDomainName', 'example.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
It's called a Protocol Relative URL and using one to shorten the Google Analytics code can cause some problems with IE6.
From Paul Irish # Google on using Protocol-Relative URLs:
There is an edgecase bug in IE6 that causes a dialog to blow up… under some security settings from the non-‘ssl’ subdomain. So feel free to take 40 bytes off your GA snippet if you don’t care about IE6.. otherwise you’re gonna need that ternary operator. :)
If you do so it won't work if you load this file using the file: protocol to test your server for example. Besides that small problem it should work just fine.

Uncaught ReferenceError: _gaq is not defined (Google Analytics)

The following message appears when viewing a site page in the chrome debug log.
Uncaught ReferenceError: _gaq is not defined
The page itself is supposed to track an object using the onload event handler and fire a _trackEvent for Google Analytics.
My best guess is that perhaps the ga.js file doesn't load in time and therefore the onload _trackEvent triggered is not caught. the async snippet is being used before the </body> close and the object is positioned in the middle <body>.
(some other posts have referenced jQuery position too but, this might be a red herring)
any assistance greatly appreciated.
From https://developers.google.com/analytics/devguides/collection/gajs/ this code replaces your existing "traditional snippet" with the "latest, asynchronous version, you should remove the existing tracking snippet first."
<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>
To see your events come in (to know if this is working) look for "Events" under the "Real-Time" menu option on the left side of the "Reporting" page.
Regarding the position of the async snippet, the GA help page says -
Paste this snippet into your website template page so that it appears
before the closing </head> tag.
My first thought was that JS should be loaded at the bottom of the page to improve page speed. However, the GA async tracking snippet should be loaded in the head, as it will not load the ga.js immediately, and it won't block page execution.(It does this by dynamically adding the script tag to the DOM, which puts it at the back of the queue.)
If, for some reason, you can't move the async snippet to the head, you can define _gaq yourself, like this-
<button onclick="var _gaq = _gaq || []; _gaq.push(['_trackEvent', 'button3', 'clicked'])"/><button>
Had the same problem. You have to define the _gaq array. Just add this after your Google Analytics script in the header:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);
You can use the last version of analytics.js instead of ga.js
ga.js is a legacy library. If you are starting a new implementation we
recommend you use the latest version of this library, analytics.js.
For exisiting implementations, learn how to migrate from ga.js to
analytics.js.
Here is an example:
ga('send', {
hitType: 'event',
eventCategory: 'Video',
eventAction: 'play',
eventLabel: 'cats.mp4'
});
Change the Tracking Code to:
<script type="text/javascript">
var gaq;
var _gaq = gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'yourdomain.com']);
_gaq.push(['_setAllowLinker', true]);
_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>

Google Analytics Custom Variables not working

I am trying to log the username of users who are using my system. Unfortunately, I couldn't track by using custom variables. I could track the page visit, but custom variable tracking is not working. Could you please help me? Thanks.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxx-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setCustomVar',1,'UserName','JohnPaul',2]);
_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>
The code is correct and it does track the users now. I just used the similar codes as above and it works now. But I needed to wait 2 days to track the custom variables and it appears in my reports now.
For more than one user, you'll have to use PHP to output user names.
_gaq.push(['_setCustomVar', 1, 'UserName', '<?php get_usernames(); ?>',2]);
***Substitute "get_usernames" with the function for your site that retrieves the usernames.

Resources