Google Analytics first uses _gaq [object Array] . Passing an array to a function is performed in JavaScript passing the object, therefore by reference.
( Edit: As indicated in the answers, the link is passed by value. For more details, see https://stackoverflow.com/a/2128328) . The value passed in JavaScript.)
The code below uses jQuery to wait for the DOM to load, and then attach a change event that sends a virtual page to Google Analytics after the user changes the <input/> field.
var _gaq = _gaq || []; _gaq.push(['_setAccount', _gAAccount]); _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); })(); var Tracking = { trackInputs: function ($, gaObject) { var inputs = $('#signUp').find('input'); inputs.bind('change', function () { gaObject.push(['_trackPageview', '/virtual/input']); console.log(gaObject);
However, as can be seen from the comment above, the trackInputs() method prints the "source" array. Usually, what the Google Analytics script does, it changes the _gaq array to the _gaq object and changes the push prototype for the object so that it requests the Google Analytics servers after the new call is transferred to the object.
Why is this change not passed by reference to trackInputs() ?
I understand that a loaded Google Analytics script will (or will?) Appear after Tracking.trackInputs() defined, so the browser may not understand that it is now [object object] , but will persist in being original [object Array] . But then this is not a link, is it?
(Using the _gaq object globally (without passing it to the method at all), the problem will be solved, but I want to understand why this does not work.)
David Andersson
source share