Why are prototypes not passed by reference to a method?

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); // Outputs: // [Array[2], Array[1], Array[2]] // ie _setAccount, _trackPageview, // and _trackPageview calls. }); } }; jQuery(document).ready(function($) { Tracking.trackInputs($, _gaq); }); // ... DOM begins below 

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.)

+7
source share
4 answers

Google Analytics first uses _gaq [object Array]. Passing an array to a function is in JavaScript passing the object in this way by reference.

The original presumption is incorrect. Javascript always uses a pass by value. Go by value. PASS BY VALUE.

you pass an object reference by value . The link is transmitted by VALUE. At the end of the day, you have many links for the same main array , because every time the link is passed by value , you have a new copy of the link, but the object never changes.

Typically, what the Google Analytics script does is change the _gaq to the _gaq object and change the push prototype for the object to request the Google Analytics servers after the new call is transferred to the object.

Where does he do it?

However, as can be seen from the comment above, the trackInputs () method prints the original array.

Of course yes.

 var _gaq = _gaq || []; _gaq.push(['_setAccount', _gAAccount]); _gaq.push(['_trackPageview']); 

identify and massage and put things in it.

 jQuery(document).ready(function($) { Tracking.trackInputs($, _gaq); }); 

pass this array by value to the method

 var Tracking = { trackInputs: function ($, gaObject) { ... inputs.bind('change', function () { gaObject.push(['_trackPageview', '/virtual/input']); ... }); } }; 

create a closure around gaObject, which is a reference to the original array and use it in the change handler. This is not the same link as _gaq , its copy of this link, but it points to the same array.

+3
source
 jQuery(document).ready(function($) { Tracking.trackInputs($, _gaq); }); 

Since the DOM is ready before loading GA, the _gaq reference still points to a regular array, which is then saved with the local gaObject symbol inside the trackInputs() function.

 trackInputs: function ($, gaObject) { 

After loading GA, the global _gaq character _gaq replaced by the tracker, but gaObject still points to the old character.

You can use the GA ready function to call your trackInputs() function instead of using $(document).ready(...) :

 _gaq.push(function() { // when this runs, the tracker would have initialized; Tracking.trackInputs($, _gaq); }); 
+5
source

At the risk of saying something stupid, since I looked at your script for about 20 minutes, and nothing obvious jumps at me ... Is it possible that it works as intended, but your test - the data is discounted in Google Analytics, since your Is your own IP address listed in the profile filters ?

0
source
 jQuery(document).ready(function() { _gaq.push(function() { Tracking.trackInputs($, _gaq); }); }); 
0
source

All Articles