I want to push a javascript variable into an array. Specifically, I want to push a Google Analytics account ID (UA-XXXXXXXX-X) into the Google Analytics javascript tracking code in the footer of my site.
(function($) {
var jsvars = {"columns":"1","mobilemenu":"1","googleanalytics":"UA-XXXXXXXX-X"};
var googleanalytics = jsvars.googleanalytics;
/* hit run to to confirm the googleanalytics variable is available */
alert (googleanalytics);
var _gaq = _gaq || [];
/* I want to push the googleanalytics variable into the array */
/* When I view source, it does not show as UA-XXXXXXXX-X, it just shows as googleanalytics */
_gaq.push(['_setAccount', googleanalytics]);
_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);
})();
})(jQuery);
http://jsfiddle.net/robflate/2MnM7/
Thanks.
Extending Pointy’s comment to an answer, you can’t use your browser’s “view source” function to see what you’re looking for. “View Source” will show the JavaScript as you wrote it. It won’t show you what any of the values are at runtime, etc. For this, you’ll want a decent JavaScript debugger – now built-in to all major web browsers. (Or use Firebug for Firefox.)
If you set a debugger breakpoint on this line, you’ll be able to see that
googleanalyticsisUA-XXXXXXXX-X. This is similar to what you already demonstrated yourself with youralertline.