Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  178] [ 1]  / answers: 1 / hits: 26053  / 14 Years ago, wed, october 6, 2010, 12:00:00

New Google Analytics code looks like one below:



<script type=text/javascript>

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-0000000-00']);
_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>


How to move the whole new Google Analytics Asynchronous Tracking Code into an external JavaScript file?



I'm asking especially about var _gaq = _gaq || []; [...] part because I know it's possible to move the rest e.g.



index.html



<script type=text/javascript>

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-0000000-00']);
_gaq.push(['_trackPageview']);

</script>
<script src=include.js type=text/javascript></script>


include.js



function includeGA()
{
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);
}

$(document).ready(function()
{
includeGA();
});


I've already tried to place the var _gaq = _gaq || []; [...] code into various locations but nothing worked.


More From » jquery

 Answers
111

This appears to be a similar question to this one: Using Google Analytics asynchronous code from external JS file



It seems pushing the code into an external file removes the benefit of the new asynchronus code.


[#95404] Saturday, October 2, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaleefridad

Total Points: 399
Total Questions: 97
Total Answers: 114

Location: North Korea
Member since Fri, Nov 4, 2022
2 Years ago
;