Skip to content

Commit

Permalink
Respect localStorage.plausible_ignore
Browse files Browse the repository at this point in the history
This change ensures no events are sent if `localStorage.plausible_ignore` is set to `"true"`. This behaviour is consistent with the standalone script.
  • Loading branch information
Windvis committed Oct 14, 2021
1 parent daea747 commit e9867d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,11 @@ const cleanup = enableAutoOutboundTracking()
cleanup()
```

### Opt out and exclude yourself from the analytics

Since plausible-tracker is bundled with your application code, using an ad-blocker to exclude your visits isn't an option. Fortunately Plausible has an alternative for this scenario: plausible-tracker will not send events if `localStorage.plausible_ignore` is set to `"true"`.

More information about this method can be found in the [Plausible documentation](https://plausible.io/docs/excluding-localstorage).

## Reference documentation
For the full method and type documentation, check out the [reference documentation](https://plausible-tracker.netlify.app).
12 changes: 12 additions & 0 deletions src/lib/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ describe('sendEvent', () => {
sendEvent('myEvent', { ...defaultData, trackLocalhost: true });
expect(xmr).toHaveBeenCalled();
});
test('does not send if "plausible_ignore" is set to "true" in localStorage', () => {
window.localStorage.setItem('plausible_ignore', 'true');
expect(xmr).not.toHaveBeenCalled();
sendEvent('myEvent', defaultData);
expect(xmr).not.toHaveBeenCalled();

window.localStorage.setItem('plausible_ignore', 'something-not-true');
sendEvent('myEvent', defaultData);
expect(xmr).toHaveBeenCalled();

window.localStorage.removeItem('plausible_ignore');
});
test('calls callback', () => {
expect(xmr).not.toHaveBeenCalled();
const callback = jest.fn();
Expand Down
8 changes: 8 additions & 0 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export function sendEvent(
);
}

const shouldIgnoreCurrentBrowser =
localStorage.getItem('plausible_ignore') === 'true';
if (shouldIgnoreCurrentBrowser) {
return console.warn(
'[Plausible] Ignoring event because "plausible_ignore" is set to "true" in localStorage'
);
}

const payload: EventPayload = {
n: eventName,
u: data.url,
Expand Down

0 comments on commit e9867d2

Please sign in to comment.