ga.js on server where my site is hosted - javascript

I use the next code from Google for Analytics:
<script type='text/javascript'>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '############']);
_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>
And I use minify library for merging all JS files into the one. And I want to include ga.js to it. Can I use ga.js from my server and how?

Sure you could use it from your server. Just download it and include it locally instead. It's even already minified. The thing is, you probably don't want to do that. Such a file is usually distributed from a CDN. There are a couple of advantages to this:
If a lot of sites include it from there, chances are that your users will already have it in their cache and will not need to download it
Those CDNs have distributed servers around the world. It will automatically download it from the closest source to your users, which is better than what you can guarantee with your server.
You will also miss out on possible new features being added.
You can read Google's word on that.
In short, juts leave it on GA's servers.

Related

Google Analytic inside iframe

I have an iframe that hosting a third party page (that I build). In that page, I have a Google analytic as below. However, Google analytic is not tracking all the entries...
The third paty page redirects users to different pages depending on different times of the day. Is there anything extra we need to do to implement Google Analytic in an iframe?
(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-47306399-1', 'serviceportal.com.au');
ga('send', 'pageview');
If the page within the <iframe /> element is on a different domain than the parent page, then you'll run into problems (particularly with older versions of Internet Explorer). The issue is that 3rd party cookies are blocked when there isn't a correct P3P header for every external asset loaded on the page within the <iframe /> (including the HTML document itself). This will effectively break your tracking code since it relies on 3rd party cookies (i.e. cookies created for domains controlled by Google or whatever other tracking provider you use).
If this is your issue, I've found two fixes.
Output proper P3P headers for all assets loaded inside the IFRAME so browsers don't block 3rd party cookies.
Use JS to Communicate from the inner-page to the parent-page and then use that communication to trigger tracking pixels from the parent-page.
My guess is that there are other ways to do this as well, this is what has worked for me.
Also, note that you can do as little as change the protocol and the pages will be thought of as on different domains by the browser, so it's generally a good idea to load the IFRAME with a URL starting in // rather than http:// or https:// so that the protocols of the parent-page and the inner-page will match. This is just if they are on the same domain to start with.
Another note... Please DO NOT just copy/paste any old P3P header value into a production environment just because it makes the "evil eye" go away in IE. Specific P3P header "codes" equate to real privacy policy statements.
One of the SEO gurus at my workplace was able to provide me with the solution tips
It was a fact of misamtached server..
The correct code ended up being
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-47544250-1']);
_gaq.push(['_setDomainName', 'vserver.com.au']);
_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);
})();

How can I make sure _gaq.push() works good?

Our client asked us to implement the following Google Analytic code:
_gaq.push(['SX0._setCustomVar', 4, 'xxxx', "yyyy", 2]);
So I've made this, and now I'm looking for a way - how can I check that everything is correct (without actually building GA report)? I haven't did this before - so please sorry if the answer is obvious.
Thanks.
You'll need to define the _gaq variable and load in the analytics script from googles server. After that, you should be able to call the 'push' method.
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-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);
})();
// tracks hit for /some/page/
_gaq.push(['_trackPageview', '/some/page']);
After that's done, load up the page in your browser, open up the console, and look in the network tab. Try loading a few pages, and verify that it's asynchronously pushing data to googles servers.
Google uses a .gif image for tracking, so you might try filtering by image and to find request for a file called __utm.gif
If you're still having trouble finding it, you might try the tag debugger plugin by ObservePoint for Chrome or the Official Tag assistant plugin by Google
Use the Analytics debugger extension for Chrome. Another useful extension (although it will not help you with custom vars) is the Google Tag Assistant (which will also check other Google tags such as adwords conversion tags and the like).

Tracking Multiple Domain Names Under One Google Analytics Account

I've read the docs here about 100 times and I can't figure this out.
I have a platform that basically creates spoofed domain names on top of one backend:
mysite.com
yourface.com
example.com
Since all domains point to the same root index file, I would like to have one Google Analytics tracking code that I can put in the footer that will track activity on all these domains.
Right now, I am using the following, and it only tracks activity on mysite.com (not the other ones):
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_setDomainName', 'mysite.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_getLinkerURL','mysite']);
_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>
I know I can set up separate profiles but that defeats the point of needing aggregate data.
Any ideas?
The culprit is probably this
_gaq.push(['_setDomainName', 'mysite.com']);
because that limits the GA cookie to the mysite.com domain. If you drop the setDomainName call alltogether it should track all domains where the code is deployed (and you'll need to filter by hostname if you want to see data for one particular domain).
That is, if I understood you correctly and your domains use the same backend but are independent on the frontend; if there is traffic between the domains you'll need to set up cross domain tracking to track correctly (else user sessions will end when a user visits another domain).
From the Google Analytics dashboard, click on Admin at the right to get to the Account Administration page. Then click on the name of your account, and you'll be taken to a list of things you're using Analytics to track. Then click on the New Property button to add the next website to your account. Each website gets its own javascript code that must be generated by Google Analytics before you install it in your website, so you have to go through this process for each website you want to track.

Do i need a running website over internet in order to access Google analytics Data using Google API

I am new to Google analytics API. I want to access my Google Analytics Data using its API.I have a local website which is deployed on IIS over a LAN.Using this setup can i access google analytics data by using its API or i need a website running over the internet.If yes please suggest how can it be done using javascript.
Google Analytics works by sending information to Google servers whenever someone visits your HTML page (doesn't need to be on the web). However you need access to the internet to send data to the Google servers. You can track any "website" if you make an account on Google Analytics and simply add the code below to whichever page you wish to track (before the </head> tag, as Google says):
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'TrackingID']);
_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 TrackingID must be replaced by the tracking ID for your Google Analytics account.

Can't push javascript variable into array

I want to push a javascript variable into an array. Specifically, I want to push a Google Analytics account ID (UA-XXXXXXXX-X) into the Google Analytics javascript tracking code in the footer of my site.
(function($) {
var jsvars = {"columns":"1","mobilemenu":"1","googleanalytics":"UA-XXXXXXXX-X"};
var googleanalytics = jsvars.googleanalytics;
/* hit run to to confirm the googleanalytics variable is available */
alert (googleanalytics);
var _gaq = _gaq || [];
/* I want to push the googleanalytics variable into the array */
/* When I view source, it does not show as UA-XXXXXXXX-X, it just shows as googleanalytics */
_gaq.push(['_setAccount', googleanalytics]);
_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);
})();
})(jQuery);
http://jsfiddle.net/robflate/2MnM7/
Thanks.
Extending Pointy's comment to an answer, you can't use your browser's "view source" function to see what you're looking for. "View Source" will show the JavaScript as you wrote it. It won't show you what any of the values are at runtime, etc. For this, you'll want a decent JavaScript debugger - now built-in to all major web browsers. (Or use Firebug for Firefox.)
If you set a debugger breakpoint on this line, you'll be able to see that googleanalytics is UA-XXXXXXXX-X. This is similar to what you already demonstrated yourself with your alert line.
This is not really an improvement on the answer, but some tips for debugging Google Analytics. The original poster mentioned that he was using chrome, which is a good thing. In chrome you can first of all pull up the JavaScript debug console using the key combo CTRL-SHIFT-J. For Google Analytics specific information, you can also download the extension for GA debugging built for Chrome. This extension allows you to see pretty much everything you would want to know about your Google interactions in the debug console.
Also, fiddler2 is a great resource for debugging JavaScript. The second link below has a great video tutorial on how to use fiddler to give you a great deal of control over the http interactions, especially when using the autoresponder function to supply local copies of .js files that would normally be loaded from your web server.
A couple of links that helped me:
http://www.webanalyticsworld.net/2012/01/basics-of-debugging-google-analytics-code-ga-chrome-debugger-and-other-tools.html
http://www.webanalyticsworld.net/2012/02/debugging-google-analytics-code-ii-a-tutorial-video-on-fiddler%E2%80%99s-inspector-and-autoresponder-functions.html

Categories