Rename "_paq" possible?

Hey there,
I got a JS which can be embedded from foreign Websites to show Panoramaic Images. I also track those includes with Matomo … Problem is some Sites also have Matomo included within their Page, others not … so I’m colliding with “_paq”, “u”, …
Is there any possibility to rename “_paq” to something unique on my side to be safe?
thx, bye from Austria
Andreas Schnederle-Wagner

2 Likes

Hi,

That’s a great question. I think renaming _paq won’t be enough as you’ll then load the piwik.js twice with are still using the same internal variables.

I think the only solution is detecting if Matomo is already initialised on this page and if it is, add your Matomo URL to the existing tracker (and hope that they are using a similar piwik.js as you are).

https://developer.matomo.org/guides/tracking-javascript-guide#collect-your-analytics-data-into-two-or-more-piwik-servers

1 Like

Hey @Lukas ,
I’m already trying to detect it Matomo is loaded/initialized on the Page where my Script is included.
It’s working on Pages where their Matomo implementation is initialized before my JS … but when they add Matomo at the very end of the Page it won’t work … as my implementation is loaded first - no Matomo initialized … so I initialize everything - and further down their Matomo implementation which doesn’t know anything about my Script crashes into action …

if(typeof _paq === 'undefined' || !Array.isArray(_paq)){
*initialize everything*
} else {
var rb_u="//xxx/";
_paq.push(['addTracker', rb_u+'piwik.php', xxx]);
}

So I would need to 100% decouple my Matomo implementation to be sure to never collide with anything on their side … mhhh …

1 Like

Maybe it would help wrapping your code into a $(document).ready()?

1 Like

I don’t think so, as Matomo isn’t finished loading on document ready (rather it starts loading then)

1 Like

Well, the task is, that it needs to run after the other matomo code that gets loaded at the end of the document. So this nasty hack could work:

$(document).ready(function() { // run the function after dom is ready
  setTimeout(function() { // use timeout 0 to add the inner function to the end of the current execution stack
    if(typeof _paq === 'undefined' || !Array.isArray(_paq)){
      *initialize everything*
    } else {
    var rb_u="//xxx/";
      _paq.push(['addTracker', rb_u+'piwik.php', xxx]);
    }, 0);
  }
}

If that doesn’t work you can still try it with a bigger timeout, but than running into problem that an action might be triggered before your code is executed.

While it is quite a hack, I think it could work as it doesn’t depend on the piwik.js loading.

Now that I think about it, what I have written above is wrong and document ready should also work. I didn’t notice you checked for _paq and not for the piwik script or window.Piwik.

1 Like

with checking the presence of the “_paq” array I was trying to avoid the probs of async loading of the pwiik.js / creation of window.Piwik Object … :wink:

But “$(document).ready(function() {})” won’t work as I don’t include jquery within my Script … had id at first - but there were also problems with double loading of jquery / different jquery versions / … (also included jquery only if it’s not already present - but ppl are very creative when and how to include jquery into their page … lol)

might this maybe work?

function ready(fn) {
  if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
    fn(); //Function which got your code with setTimeout() in it
  } else {
    document.addEventListener('DOMContentLoaded', fn);
  }
}

or would the setTimeout() “trick” to get it load last not work here? thinking

2 Likes

Could work, as it simple tries to mirror what jquery is doing behind the scenes.

1 Like

I will try to do some test cases this week to see if it works as expected … thank you both for helping me on this! :wink:
Also made a Feature suggestion for renaming “_paq” … guess there are some scenarios where one needs to completely decouple 2 different Piwik Instances … ?!?
–> Multiple decoupled Matomo instances (rename ‘_paq’, ‘window.Piwik’, ...)

1 Like

Off-topic:
@futureweb, as you are integrating Matomo into “foreign” websites you need to be really carefully with data privacy.
So you’ll at the very least need to provide a “Datenschutzerklärung” and an Opt-Out iFrame inside your panorama to be compliant with GDPR.

2 Likes

@Lukas - thx for pointing that out … new DSGVO is a big issue those days and it’s already talking point in our company how to best solve DSGVO compliance with those Pano-Pic-Stats and 3rd-Party integration …

1 Like

This may help you:
https://github.com/matomo-org/matomo/issues/12600

1 Like

thx again - will read through the Links within this post! :wink:

1 Like