jonkoops / matomo-tracker

Stand alone library for using matamo tracking in frontend projects
Mozilla Public License 2.0
142 stars 62 forks source link

Use functions for getting information #180

Open veermetri05 opened 4 years ago

veermetri05 commented 4 years ago

Is your feature request related to a problem? Please describe. A Yes I want to fetch user's visitorId . It is not possible every visitor on my website is logged in. If the user is logged in then I am able to set the userId on initialization. But I want to get the visitorId of the current user. This can be achieved by passing a function as described in the documentation.

var visitor_id;
_paq.push([ function() { visitor_id = this.getVisitorId(); }]); 

But I didn't find a way to do that with the @datapunk/matomo-tracker-react

So I am requesting a feature to add passing functions to the Configuration of the Tracker Object (as mentioned on the documentation)

Describe the solution you'd like A Be able to pass functions or getting visitorId directly

Describe alternatives you've considered A I found out this plugin. I haven't tried any alternative methods that have found information here.

Additional context I haven't worked with the javascript tracker by matomo I thought this plugin has all features. I want to fetch the visitorId to track the user's behavior (page he visits, time spent on the website, etc). Then based on the data we will recommend articles that the user will like to read.

Resources https://developer.matomo.org/api-reference/tracking-javascript https://developer.matomo.org/guides/tracking-javascript-guide

Package Used https://github.com/Amsterdam/matomo-tracker/tree/master/packages/react

jonkoops commented 4 years ago

Hi @veermetri05, this is certainly something we would want to include in a newer version. I am adding this to our backlog.

jutunen commented 2 years ago

It is quite straightforward to expand the hook to get broader tracking API support.

For example, here is how to add hasRememberedConsent feature to the hook.

Just add following function to useMatomo.js:


const hasRememberedConsent = useCallback(() => {
        let rval;
        // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
        instance === null || instance === void 0 ? void 0 : instance.pushInstruction(function() { rval = this.hasRememberedConsent(); });
        return rval;
    }, [instance]);

    return {
        trackEvent,
        trackEvents,
        trackPageView,
        trackSiteSearch,
        trackLink,
        enableLinkTracking,
        pushInstruction,
        hasRememberedConsent
    };

Also remember to add the new function to the returned object.

How to use the new function in React client:

const { trackPageView, hasRememberedConsent } = useMatomo();

if( hasRememberedConsent() === false ) console.log("...");