Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to put Google Analytics code in an external JS file?

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));  try  {     var pageTracker = _gat._getTracker("UA-XXXXXXX-1");     pageTracker._trackPageview(); } catch(err) {} 

Would it be possible to call this script from an external JS file? I wanted to to something like:

<script type="text/javascript" src="googleanalytics.js" ></script> 

and put one of these on each of my HTML pages.
The code I have above will be inside googleanalytics.js
Google's instructions was to put the code in each page. The problem with that is it makes it harder to change the tracking code. (We use different tracking codes for our DEV and PROD pages).
I've tried it out and it doesn't seem to be working.
Is there something wrong with doing that? Or is there something else causing the problem?

Important FYI Please note that we are using IE6 and 8 browsers (yes, I know, no need to tell me)

like image 598
Nikko Avatar asked Jul 15 '09 12:07

Nikko


People also ask

How do I integrate Google Analytics into JavaScript?

Install the Google tag Replace GA_TRACKING_ID with the ID of the Google Analytics property to which you want to send data. You need only one snippet per page. This snippet loads the gtag. js library, establishes GA_TRACKING_ID as the default Google Analytics property ID, and sends a pageview hit to Google Analytics.

Where should I put Google Analytics code?

You can add the Analytics tag directly to the HTML of each page on your site, or indirectly using a tag management system such as Google Tag Manager.

Can I put Google Analytics code in body?

You can put it anywhere you want on page, and ll run anywhere on the page whether it's in the head or the body. But according to Google support (Add the tracking code directly to your site) it is recommended to it in the head tag, paste it immediately before the closing </head> tag.

Where do I paste Google Analytics code in HTML?

Add Analytics to Your Site Find the <head> tag on each of your web pages and add the code you copied in the previous section. The code will look like this, but with your own tracking ID substituted.


2 Answers

Yes this is possible. If it is not working then there is something else going on.

Just a thought, Google Analytics is usually about a day behind in reporting so when you make changes it will take some time before you know it is working. What I like to do is hit a page that does not get traffic very often to assure me that I have my tracking set up correctly.

Also, you might try making the link an absolute link in your <script tag. It might just be looking in the wrong place for the analytics code.

like image 96
Jason Avatar answered Sep 23 '22 11:09

Jason


Can you not use your server-side language to output the code at the bottom of each page? Have a function such as output_ga() and call that. That way you can change it in one place.

like image 28
Draemon Avatar answered Sep 20 '22 11:09

Draemon