Can you use an include file for the Google Analytics code?
I mov开发者_运维知识库ed my analytics code to a javascript include file and now it does not seem to be tracking. Can anyone shed any light on this?
Contents of analytics.js
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-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);
})();
Include code
<script src="js/analytics.js" type="text/javascript"></script>
Yes, you can include the analytics code in an external JavaScript file. How are you determining that it is not working? Depending on the browser you are using, you should be able to view the DOM source and see if the <script>
was appended to the document.
You should see something along the lines of:
<SCRIPT type="text/javascript" src="http://www.google-analytics.com/ga.js" async></SCRIPT>
I usually add the GA tracking code in the header or footer file and then I include (header, footer) file on the page.
When you page loads, you can verify the GA code by looking the source of the page. (Right click)
精彩评论