google analytics counts double visits for on single page - javascript

I am using google analytics for the first time
here is the code which I have added
<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');
console.log("executed");
ga('create', 'UA-46680625-1', 'wfihotspotnet.in');
ga('send', 'pageview');
</script>
I also want to add custom variables to google analytics script,
I got the code from one source , which is.
<script type='text/javascript'>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-46680625-1']);
// set custom variables
_gaq.push(["_setCustomVar", 1, "Member", "yes", 1]);
_gaq.push(["_setCustomVar", 3, "Topic", "JavaScript", 3]);
// track page view
//_gaq.push(['_trackPageview']);
console.log("executed");
(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>
I thought its because I have added these two scripts together, but after removing 2nd script also it shows same result 2 vsits/sometimes 3.
is there any way to add custom variables and google analytics code together in single code.

You are using new analytics.js code with old ga.js code. Are you attempting to migrate to universal analytics? If so, use their migration.

Related

What is causing the conflict between these two Google Analytics tracking scripts?

I am getting a ton of self-referred traffic on my website. I believe it is due to these two tracking codes.. I am not sure that I need both, but want to be sure if I do or not.
We want sub-domain tracking to work (first script), and obviously the regular GA tracking info as well (2nd script is the out-of-the-box 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-5402569-1', 'auto',{‘allowLinker’: true });
ga(‘require’, ‘linker’);ga(‘linker:autoLink’, ['goaupair.com’,‘community.goaupair.com’,'knowledge.goaupair.com']);
ga(‘send’, ‘pageview’);
</script>`
`<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5402569-1']);
_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>`
You do not need both (in fact you will get most likely double counting of pageviews). The first script is the current version (Universal Analytics). The second script is previous version of the GA tracking script.
You should remove the second version altogether. As far as subdomain tracking is concerned you do not need any special tracking for that - just make sure that your domain is added to the referral exclusion list in the property settings, and that the cookieDomain is set to "auto" when you create the tracker (which it is, so simply lose the the both lines for the linker).
Linking between domains is only necessary if you want to do cross domain tracking between two different domains. The point if cross domain tracking is to transfer the client id that is stored in a cookie, and since your domain and subdomain share their cookies anyway you do not need special configuration.
The only thing that's important is the "auto" setting for the cookie domain, else visitors entering via the subdomain will get a new clientid (and thus be counted as new visitors) when they enter the main domain.

Google Analytics - Event pushing not working

I inserted Google Analytics with the following code snipped.
<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-xyz-1', 'xyz.com');
ga('send', 'pageview');
</script>
Now I want to track a specific page in my ajax application. The following link
describes how to push events to google analytics:
https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
If I try to execute the following command, I just get the error saying "_gaq" is not defined.
_gaq.push(['_trackPageview', 'myPage']);
Am I doing something wrong here or are the docs are outdated?
Edit: So there seems to be a new API. What would be the equivalent to:
_gaq.push(['_trackPageview', 'myPage']);
Would this be the same command in the new API?
ga('send', 'pageview', 'myPage);
You're mixing the new analytics.js with the old async ga.js. Use analytics.js (Universal Analytics) event tracking documentation instead.
This is actually the code that samples the variable is not defined _gaq, reviewing the Analitycs page I see that the code is as follows:
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);
})();
Check the block of code and make sure that all is well implemented
Jan, I believe the pageview request is already in the snippet you mentioned:
ga('send', 'pageview');
Just make sure you the line above this -- you need to make sure that domain and Analytics account ID is specified:
ga('create', 'UA-123456-1', 'yourdomain.com');
Looking at your code, it seems that you are using new Universal Analytics library, so _gaq.push commands won't work anymore. When you refer to documentation, always double check you looking at Analytics.js section (link).

Is Google Rendering Bad Tracking Codes?

I've gotten a lot of response saying that my Google tracking code was excessive in many ways, and I suspect that perhaps not all features are working. (There's certain metrics that aren't showing data: "Paid Traffic" and "Campaigns" for example)
Is it possible that Google is rendering less than useful tracking codes?
Should I return to the old format of tracking codes, since I know that they work well?
Could it be the placement on my page? Google recommends before </head> yet everyone on here recommends before </body>.
What Google rendered for me: (Current)
<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-40498113-1', 'caseykidd.com');
ga('send', 'pageview');
</script>
My old Tracking Code: (Circa One Year Ago)
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-28036048-1']);
_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 "new" tracking code is analytics.js, which "is part of Universal Analytics, which is currently in public beta".
Granted you can probably get the Google Analytics pages to give you the beta code, I would probably stick with the tried and true ga.js on production websites until Google pulls it out of beta.
The new tracking code has async enabled by default, so in most recent browsers it won't stall the page load even if there is a problem loading Google Analytics. That is why they recommend you put the ga.js tracking code before </body> instead of </head>.

google analytics javascript security - property ID public

I've read the following question: Security on Google Analytics and other client-side analytics tools?
But I need an even simpler answer.
Per the google analytics site, I've put the following code in the header of my web site:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'propertyID']);
_gaq.push(['_setDomainName', 'brokersqueeze.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>
While perhaps obvious to most readers here, I realized all this information (like the PropertyID) is available via "inspect element" in the browser.
Have I put the GA snippet properly? Or is there a way to us GA such that the info is hidden from the public? It seems like answers for the question above suggest that while the information is public, it's a risk everyone just lives with. Do I have that right?
Yes, you've done it right. That risk exists with every client side analytics tool. If they send spoofed requests to GA, you can try filtering the results in reports. Even if you obfuscate the ID in the javascript code, they can capture the http request and get that ID.

Why isn't Google Analytics script loading in some browsers?

I use default generated GA tracking code for my page:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXX-1']);
_gaq.push(['_setDomainName', 'xxx.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>
This code seems to work in most cases. However, the _utm.gif is not requested from some browsers, (namely, I noticed it in Chromium on Ubuntu and Firefox on Fedora). Actually, it appears that the ga.js script is not even loaded.
If I use the GA Debugger or locally saved script ( ga.src = 'ga.js'; ), the tracking works as expected even on mentioned browsers. Other tested OS-browser configurations works.
EDIT: As suggested in comments, I've tried to remove protocol resolution (i.e.: ga.src = 'http://www.google-analytics.com/ga.js') and scripts other then ga.js (to rule out conflicts). It didn't change the behavior in any way.
Any ideas what causes the problem and how to avoid it?
I tested the issue again today and everything appears to be working as it should.. I have no idea why..

Categories

Resources