@MichaelRoosz opened this Pull Request on October 17th 2018 Contributor

this pr adds the function getGlobalVisitorId() to the tracker js, which returns the visitorId stored in the 3rd party cookie (_pk_uid)

@MichaelRoosz commented on October 17th 2018 Contributor

Note: this function will only work correctly, when the Matomo third party cookie is bound to the same domain, for example:
www.example.com -> Content
matomo.example.com -> Matomo
cookie domain: example.com

This could be improved by adding an API call which returns the value (visitorId) of the third party cookie. But I am unsure whether this is a good idea or not. If you are interested, I can adjust my pr.

@MichaelRoosz commented on October 18th 2018 Contributor

If this gets accepted, I can add getGlobalVisitorId() also to matomo-php-tracker, if you are intersted.

@tsteur commented on September 1st 2020 Member

@MichaelHeerklotz sorry for the late reply. Are you still needing this? As it's quite edge case with third party cookies and only works when Matomo is also installed on the same domain I'm thinking it might be better to use tracker.getCookie('_pk_uid') and do a custom implementation in your code?

@tsteur commented on September 3rd 2020 Member

@MichaelHeerklotz I'll close this PR for now as mentioned ideally you would use tracker.getCookie('_pk_uid'). Let me know if that won't work and I'll be happy to reopen and see what we can do. Generally globalVisitorId might be bit confusing and too edge case. We have now also documented how to build a custom JS tracker see https://developer.matomo.org/guides/enrich-js-tracker . Meaning this could be even solved in a plugin.

@MichaelRoosz commented on September 3rd 2020 Contributor

Hello @tsteur, if we want to add it, I think we should get the 3rd party / global visitor id from an api call to the Matomo installation. If this is something you would accept, I can create a pr.

@tsteur commented on September 4th 2020 Member

@MichaelHeerklotz we'd suggest creating a custom plugin for this to not "bloat" the default tracker. This should be possible using https://developer.matomo.org/guides/enrich-js-tracker

eg you could do something like this in a tracker.js file:

(function () {

    function init() {
        function enrichTracker(tracker) {
            tracker.getThirdPartyId = function (callbackToBeCalledWhenDataFetched) {
            ...
            }
        }
        if (windowAlias.Piwik.initialized) {
            var asyncTrackers = Piwik.getAsyncTrackers();
            asyncTrackers.forEach(enrichTracker);
            Piwik.on('TrackerSetup', enrichTracker);

        } else {
            Piwik.on('TrackerSetup', enrichTracker);
        }
    }

    if ('object' === typeof window.Piwik) {
        init();
    } else {
        // tracker might not be loaded yet
        if ('object' !== typeof window.piwikPluginAsyncInit) {
            window.piwikPluginAsyncInit = [];
        }

        window.piwikPluginAsyncInit.push(init);
    }

})();

and then it works to do eg tracker.getThirdPartyId(callback)

This Pull Request was closed on September 3rd 2020
Powered by GitHub Issue Mirror