0

I'm trying to understand how Google Analytics works and one thing I started to wonder is why they don't replace

  _gaq.push(['_setAccount', 'UA-XXXXX-Y']);
  _gaq.push(['_trackPageview']);

with _gaq.push(['_setAccount', 'UA-XXXXX-Y','_trackPageview']);

I mean, why two (rather than one or three) lines for three array elements? What am I misunderstanding?

3
  • 1
    It would be: _gaq.push(['_setAccount', 'UA-XXXXX-Y'], ['_trackPageview']);
    – elclanrs
    Sep 27, 2013 at 5:58
  • Then _qaq is an array consisting of arrays, right?
    – RubenGeert
    Sep 27, 2013 at 6:03
  • Initially _gaq is an array, but when the ga.js code loads, it's replaced by an object with a push method like an array. The array that's pushed onto _gaq contains a function name & arguments.
    – mike
    Sep 27, 2013 at 13:55

1 Answer 1

2

Yes, you can replace the 2 push statement with a single one and it will provide the same functionality

_gaq.push(['_setAccount', 'UA-XXXXX-X'], ['_trackPageview']);

The 2 push statements do different roles:

1,The first once sets the web property ID for the tracking object. This means all further push statements to the same queue will be sent to the same GA property (tracking ID).

2,The second push tracks a pageview using the above tracking object and is typically the first GA functionality required when you load analytics in any webpage. Now you can send further tracking data to your GA account with the already created tracking object and you just need to invoke the corresponding push statements like below:

-event tracking

<button onclick="_gaq.push(['_trackEvent', 'button3', 'clicked'])"/><button>
  • ecommerce tracking, etc

    _gaq.push(['_addTrans', transactionId, affiliation, total, tax, shipping, city, state, country]);

1
  • thanks for the explanation. But there MUST be some reason why the guys at Google don't just put it on one line like you did, right?
    – RubenGeert
    Sep 28, 2013 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.