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)
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With