Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#24): Set userInfo once #26

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions src/MatomoTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class MatomoTracker {

this.trackerUrl = trackerUrl ?? `${normalizedUrlBase}matomo.php`;
this.siteId = siteId;
this.userInfo = {};

if (userId) {
this.userId = userId;
Expand All @@ -70,7 +71,8 @@ class MatomoTracker {
* @see https://developer.matomo.org/api-reference/tracking-api#optional-user-info for optional data used for tracking different user info
*/
trackAppStart({ userInfo = {} } = {}) {
return this.trackAction({ name: 'App / start', userInfo });
this.updateUserInfo(userInfo);
return this.trackAction({ name: 'App / start' });
}

/**
Expand All @@ -96,11 +98,10 @@ class MatomoTracker {
if (!name) {
throw new Error('Error: The "name" parameter is required for tracking a screen view.');
}

return this.trackAction({ name: `Screen / ${name}`, userInfo });
this.updateUserInfo(userInfo);
return this.trackAction({ name: `Screen / ${name}` });
}


/**
* Tracks a custom action.
*
Expand All @@ -126,8 +127,9 @@ class MatomoTracker {
if (!name) {
throw new Error('Error: The "name" parameter is required for tracking an action.');
}
this.updateUserInfo(userInfo);

return this.track({ action_name: name, ...userInfo });
return this.track({ action_name: name });
}

/**
Expand Down Expand Up @@ -162,14 +164,14 @@ class MatomoTracker {
if (!action) {
throw new Error('Error: The "action" parameter is required for tracking an event.');
}
this.updateUserInfo(userInfo);

return this.track({
e_c: category,
e_a: action,
e_n: name,
e_v: value,
mtm_campaign: campaign,
...userInfo
});
}

Expand Down Expand Up @@ -200,8 +202,13 @@ class MatomoTracker {
if (!keyword) {
throw new Error('Error: The "keyword" parameter is required for tracking a site search.');
}
this.updateUserInfo(userInfo);

return this.track({ search: keyword, search_cat: category, search_count: count, ...userInfo });
return this.track({
search: keyword,
search_cat: category,
search_count: count,
});
}

/**
Expand Down Expand Up @@ -229,11 +236,10 @@ class MatomoTracker {
if (!link) {
throw new Error('Error: The "link" parameter is required for tracking a link click.');
}

return this.track({ link, url: link, ...userInfo });
this.updateUserInfo(userInfo);
return this.track({ link, url: link });
}


/**
* Tracks file downloads.
*
Expand All @@ -259,10 +265,24 @@ class MatomoTracker {
if (!download) {
throw new Error('Error: The "download" parameter is required for tracking a file download.');
}
this.updateUserInfo(userInfo);

return this.track({ download, url: download, ...userInfo });
return this.track({ download, url: download });
}

/**
* This method is used to update the user information for tracking for entire instance.
*/
updateUserInfo(newUserInfo) {
this.userInfo = { ...this.userInfo, ...newUserInfo };
}

/**
* This method is used to remove the user information for tracking for entire instance.
*/
removeUserInfo() {
this.userInfo = {};
}

/**
* Sends the tracking data to Matomo.
Expand Down Expand Up @@ -290,9 +310,10 @@ class MatomoTracker {
idsite: this.siteId,
rec: 1,
apiv: 1,
...(this.userId ? { uid: this.userId }: {}),
...(this.userId ? { uid: this.userId } : {}),
send_image: 0,
...data
...data,
...this.userInfo,
}).toString()
};

Expand Down
4 changes: 3 additions & 1 deletion src/useMatomo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const useMatomo = () => {
trackEvent: (params) => instance.trackEvent && instance.trackEvent(params),
trackSiteSearch: (params) => instance.trackSiteSearch && instance.trackSiteSearch(params),
trackLink: (params) => instance.trackLink && instance.trackLink(params),
trackDownload: (params) => instance.trackDownload && instance.trackDownload(params)
trackDownload: (params) => instance.trackDownload && instance.trackDownload(params),
updateUserInfo: (params) => instance.updateUserInfo && instance.updateUserInfo(params),
removeUserInfo: () => instance.removeUserInfo && instance.removeUserInfo(),
}),
[instance]
);
Expand Down