Jump to content

Recommended Posts

BACKGROUND:  Earlier I posted a script from Matomo that serves as a counter for page hits and various other bits of information gleaned from a user's behavior on a webpage that bears the script.  Although I now understand what the code does, I would like to understand its purpose.  In particular, I would like to understand the probable reason for assigning to a variable another variable with the same name.  The full code is given as:

<!-- Matomo -->
<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//{$PIWIK_URL}/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', {$IDSITE}]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

The code of interest is

var _paq = _paq || [];

I find this piece of code problematic for three reasons:

  1. It assumes that this variable could already exist, but I have no idea where it could come from.
  2. It it does already exist what happens to the pre-existent with the same name.
  3. How does it know which value to choose?  Is there a latent selection mechanism that tells the assignment operator to choose the second value when the first is undefined?

Please advise.

Roddy

 

 

 

 

 

Edited by iwato

Share this post


Link to post
Share on other sites

The OR operator in Javascript returns the leftmost operand that evaluates to a boolean true value. In the following code, the variable x gets set to 5 because 0 is false.

var x = 0 || 5;

When you assign a variable to itself nothing changes:

var x = x;

When a variable does not exist, its value is undefined which evaluates to boolean false. In the following code the variable x gets set to 5 because an undefined variable is false.

var x = x || 5;

In conclusion, the code var x = x || []; will do nothing when x already exists but will assign an empty array to x if it does not already exist.

The reason that people creating embeddable scripts do this is to prevent the program from breaking when somebody embeds their code more than once in the same document.

  • Thanks 1

Share this post


Link to post
Share on other sites

Revisiting the Matomo insert

<!-- Matomo -->
<script type="text/javascript">
    var _paq = _paq || [];
    _paq.push(['trackPageView']);
    _paq.push(['enableLinkTracking']);
    (function() {
        var u="//{$PIWIK_URL}/";
        _paq.push(['setTrackerUrl', u+'piwik.php']);
        _paq.push(['setSiteId', {$IDSITE}]);
        var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
        g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
    })();
</script>
<!-- End Matomo Code -->

If what you said is true,  then the following statement would be true would it not?

STATEMENT:  Were the same script to be entered twice on the same page, then the contents of the first completed _paq array would be passed onto the _paq array of the second entry and everything would be counted twice. 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×