Skip to content
This repository was archived by the owner on Jun 20, 2025. It is now read-only.

Commit 96df037

Browse files
authored
Merge pull request #47 from caalberts/master
Add a public API to set user id. thx @caalberts
2 parents eb54dea + 383cc6e commit 96df037

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ Adds a page view from a history [Location](https://github.com/rackt/history/blob
116116
Pushes the specified args to the Piwik tracker the same way as you're using the `_paq.push(args);` directly. You can use this method as the low-level api to call methods from the [Piwik API](http://developer.piwik.org/api-reference/tracking-javascript#list-of-all-methods-available-in-the-tracking-api), [track events](http://piwik.org/docs/event-tracking/#tracking-events) or call [custom functions](http://developer.piwik.org/guides/tracking-javascript-guide).
117117

118118

119+
### setUserId (userId)
120+
121+
Sets the specified user id to the Piwik tracker after `PiwikReactRouter` has been initialized. It wraps around the `'setUserId'` method of Piwik [User ID](https://developer.piwik.org/guides/tracking-javascript-guide#user-id)
122+
123+
119124
### trackError (error, [eventName])
120125

121126
Tracks the given error as a new [Piwik Event](http://piwik.org/docs/event-tracking/#tracking-events) for the given event name. If you don't specify any name here it will fallback to `JavaScript Error`.

index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var apiShim = {
88
_isShim: true,
99
track: function () {},
1010
push: function (args) {},
11+
setUserId: function (userId) {},
1112
trackError: function (e) {},
1213
connectToHistory: function (history, modifier) { return history; },
1314
disconnectFromHistory: function () {}
@@ -79,6 +80,16 @@ var PiwikTracker = function(opts) {
7980
window._paq.push(args);
8081
};
8182

83+
/**
84+
* Sets a user ID to the piwik tracker.
85+
* This method can be used after PiwikReactRouter is instantiated, for example after a user has logged in
86+
*
87+
* @see https://developer.piwik.org/guides/tracking-javascript-guide#user-id
88+
*/
89+
var setUserId = function setUserId (userId) {
90+
window._paq.push(['setUserId', userId])
91+
};
92+
8293
/**
8394
* Tracks occurring javascript errors as a `JavaScript Error` piwik event.
8495
*
@@ -159,7 +170,7 @@ var PiwikTracker = function(opts) {
159170
push(['setTrackerUrl', u+opts.serverTrackerName]);
160171

161172
if (opts.userId) {
162-
push(['setUserId', opts.userId]);
173+
setUserId(opts.userId);
163174
}
164175

165176
if (opts.enableLinkTracking) {
@@ -177,6 +188,7 @@ var PiwikTracker = function(opts) {
177188
_isShim: false,
178189
track: track,
179190
push: push,
191+
setUserId, setUserId,
180192
trackError: opts.trackErrorHandler,
181193
connectToHistory: connectToHistory,
182194
disconnectFromHistory: disconnectFromHistory

test/client.tests.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('piwik-react-router client tests', function () {
5555
]);
5656
});
5757

58-
it ('should correctly push the userId', () => {
58+
it ('should correctly push the userId on instantiation', () => {
5959
const piwikReactRouter = testUtils.requireNoCache('../')({
6060
url: 'foo.bar',
6161
siteId: 1,
@@ -209,6 +209,25 @@ describe('piwik-react-router client tests', function () {
209209

210210
});
211211

212+
describe('setUserId', () => {
213+
it('should add user id to _paq', () => {
214+
const piwikReactRouter = testUtils.requireNoCache('../')({
215+
url: 'foo.bar',
216+
siteId: 1
217+
});
218+
219+
const userId = '[email protected]';
220+
piwikReactRouter.setUserId(userId);
221+
222+
assert.includeDeepMembers(window._paq, [
223+
[
224+
'setUserId',
225+
226+
]
227+
]);
228+
});
229+
});
230+
212231
//it ('should correctly warn')
213232

214233
describe ('javascript error', () => {

test/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const requireNoCache = function (filePath, opts) {
1313
const API_METHOD_NAMES = [
1414
'track',
1515
'push',
16+
'setUserId',
1617
'trackError',
1718
'connectToHistory',
1819
'disconnectFromHistory'

0 commit comments

Comments
 (0)