Skip to content

Commit

Permalink
feat(request): adds support for custom event properties
Browse files Browse the repository at this point in the history
Adds support for custom event properties through the EventOption 'props' value.

fix #2
  • Loading branch information
Maronato committed Nov 8, 2020
1 parent 51a4690 commit 2e4b8eb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ trackPageview({

The second parameter is an object with [some options](https://plausible-tracker.netlify.app/globals.html#eventoptions) similar to the ones provided by the [official Plausible script](https://docs.plausible.io/custom-event-goals).

The only supported option at the moment is `callback` – a function that is called once the event is logged successfully.

```ts
import Plausible from 'plausible-tracker'

Expand Down Expand Up @@ -177,6 +175,12 @@ const { trackEvent } = Plausible()
trackEvent('signup')
```

Custom props can be provided using the third parameter:
```ts
// Tracks the 'Download' goal and provides a 'method' property.
trackEvent('signup', {}, { props: { method: 'HTTP' } })
```

As with [`trackPageview`](#tracking-page-views), you may also provide override values and a callback as the second and third parameters respectively:

```ts
Expand All @@ -186,11 +190,16 @@ const { trackEvent } = Plausible({
trackLocalhost: false,
})

// Tracks the 'signup' goal with a different referrer and a callback
// Tracks the 'signup' goal with a different referrer, a callback and props
trackEvent(
'signup',
{ trackLocalhost: true },
{ callback: () => console.log('done') }
{
callback: () => console.log('done'),
props: {
variation: 'button A'
}
}
);
```

Expand Down
17 changes: 17 additions & 0 deletions src/lib/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,21 @@ describe('sendEvent', () => {
xhrMockClass.ready(4);
expect(callback).toHaveBeenCalled();
});
test('sends props', () => {
expect(xmr).not.toHaveBeenCalled();
const props = { variation1: 'A', variation2: 'B' };
sendEvent('myEvent', defaultData, { props });

const payload = {
n: 'myEvent',
u: defaultData.url,
d: defaultData.domain,
r: defaultData.referrer,
w: defaultData.deviceWidth,
h: 0,
p: JSON.stringify(props)
};

expect(xhrMockClass.send).toHaveBeenCalledWith(JSON.stringify(payload));
});
});
10 changes: 10 additions & 0 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ type EventPayload = {
readonly r: Document['referrer'] | null;
readonly w: Window['innerWidth'];
readonly h: 1 | 0;
readonly p?: string;
};

// eslint-disable-next-line functional/no-mixed-type
export type EventOptions = {
/**
* Callback called when the event is successfully sent.
*/
readonly callback?: () => void;
/**
* Properties to be bound to the event.
*/
readonly props?: { readonly [propName: string]: string }
};

/**
Expand Down Expand Up @@ -46,6 +55,7 @@ export function sendEvent(
r: data.referrer,
w: data.deviceWidth,
h: data.hashMode ? 1 : 0,
p: options && options.props ? JSON.stringify(options.props) : undefined
};

const req = new XMLHttpRequest();
Expand Down
4 changes: 4 additions & 0 deletions src/lib/tracker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ describe('tracker', () => {

const getEventOptions: () => Required<requestModule.EventOptions> = () => ({
callback: jest.fn(),
props: {
variation1: 'A',
variation2: 'B'
}
});

test('inits with default config', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/lib/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ export type PlausibleOptions = PlausibleInitOptions & PlausibleEventData;
*
* // Tracks the 'signup' goal
* trackEvent('signup')
*
* // Tracks the 'Download' goal passing a 'method' property.
* trackEvent('Download', {}, { props: { method: 'HTTP' } })
* ```
*
* @param eventName - Name of the event to track
* @param eventData - Optional event data to send. Defaults to the current page's data merged with the default options provided earlier.
* @param options - Event options. The only supported option at the moment is `callback` – a function that is called once the event is logged successfully.
* @param options - Event options.
*/
type TrackEvent = (
eventName: string,
Expand All @@ -90,7 +93,7 @@ type TrackEvent = (
* ```
*
* @param eventData - Optional event data to send. Defaults to the current page's data merged with the default options provided earlier.
* @param options - Event options. The only supported option at the moment is `callback` – a function that is called once the event is logged successfully.
* @param options - Event options.
*/
type TrackPageview = (
eventData?: PlausibleOptions,
Expand Down

0 comments on commit 2e4b8eb

Please sign in to comment.