I am investigating a way to include a JavaScript file within some script tags. I have a large JavaScript file of Google Analytics Tracking Code and I need to include it at a certain point in my code. This is my code below:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456789']);
<script type="text/javascript" src="/scripts/search_engines.js"></script>
_gaq.push(['_trackPageview']);
</script>
I need to have search_engines.js
before the _trackPageview
, but the syntax is clearly invalid.
search_engines.js
has 290 lines of this:
_gaq.push(
['_addOrganic','advancedsearch2.virginmedia.com','SearchQuery',true],
['_addOrganic','alltheweb','q',true],
['_addOrganic','ananzi','qt',true],
I was looking a jQuery's addScript, but that doesn't appear to offer what i'm looking for.
Is there a way to include the JavaScript file within my script tags?
Load your script over AJAX and run the dependent code in a callback. This is trivial with jQuery:
$.getScript('/scripts/search_engines.js').done(function() {
_gaq.push(['_trackPageview']);
});
Or, just put the whole GA code in one JS include.