Rename "_paq" possible?


(Andreas Schnederle-Wagner) #1

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


Multiple decoupled Matomo instances (rename '_paq', 'window.Piwik', ...)
(Lukas Winkler) #2

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


(Andreas Schnederle-Wagner) #3

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 …


(Fabian Dellwing) #4

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


(Lukas Winkler) #5

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


(Fabian Dellwing) #6

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.


(Lukas Winkler) #7

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.


(Andreas Schnederle-Wagner) #8

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


(Fabian Dellwing) #9

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


(Andreas Schnederle-Wagner) #10

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


(Lukas Winkler) #11

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.


(Andreas Schnederle-Wagner) #12

@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 …


(Lukas Winkler) #13

This may help you:


(Andreas Schnederle-Wagner) #14

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