Can I get attribution info from _paq?


(Olle Härstedt) #1

Can I use

_paq.push('getAttributionInfo')

or sim to get attribution info from the _paq API?


(Lukas Winkler) #2

Hi,

I think the best way to call a JS method is like this

_paq.push([function () {
    var attributionInfo = this.getAttributionInfo();
    console.info(attributionInfo)
}]);

(Olle Härstedt) #3

Thank you! How do you know this.getAttributionInfo() works? Couldn’t find it in the docs about _paq.


(Lukas Winkler) #4

Hi,

You can find getAttributionInfo in the JS-tracking-client docs:

https://developer.matomo.org/api-reference/tracking-javascript#configuration-of-the-tracker-object

But I agree that this website needs a better search (which is already partly build)


(Olle Härstedt) #5

Oh! So using this in _paq gets access to the JS API? Good to know. ^^


(Lukas Winkler) #6

You could also use Piwik.getAsyncTracker().getAttributionInfo(), but it has one large disadvantage, which is solved by the _paq syntax:

Matomo loads asynchronous, so it is possible that your code runs before the piwik.js has even finished loading and would fail therefor. If you use _paq the function will be saved and run once Matomo has finished loading.


(Olle Härstedt) #7

Right now I use setTimeout to wait until Piwik is done. Is there another way, like, register a callback function instead?


(Lukas Winkler) #8

I think what I posted above should be about what you need:

_paq.push([function () {
    console.info("Matomo has finished loading")
}]);