Skip to content

Commit

Permalink
feat: allow disabling of cookie usage (#111)
Browse files Browse the repository at this point in the history
* #82: added disableCookies config parameter and conditionally push disableCookies flag

* #82: added tests

* #82: added to readme

---------

Co-authored-by: Christian Hoffmann <[email protected]>
  • Loading branch information
Christian1984 and Christian Hoffmann authored Sep 6, 2023
1 parent 89bde8b commit 3d50f80
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
39 changes: 31 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ init({
});
```

### Disable cookies :

To disable cookies (for better GDPR compliance) set the `disableCookies` flag to `true`.

```js
init({
url: MATOMO_URL,
siteId: MATOMO_SITE_ID,
disableCookies: true,
});
```

### Track additional events :

```js
Expand All @@ -81,17 +93,28 @@ The function has three optional callback properties that allow for custom behavi

```
init
✓ should create a js tag and initialize (16 ms)
✓ should NOT create events when url is not provided (19 ms)
✓ should create a js tag and initialize (7 ms)
✓ should NOT create events when url is not provided (9 ms)
push
✓ should append data to window._paq (2 ms)
✓ should append data to window._paq (1 ms)
✓ should append dimensions data to window._paq (1 ms)
onInitialization
✓ should work if the surcharge of the operator (1 ms)
router.routeChangeStart event
✓ should setReferrerUrl and setCustomUrl on route change start (1 ms)
✓ should use previousPath as referer on consecutive route change (1 ms)
✓ should work if the surcharge of the operator (3 ms)
router.routeChangeComplete event
✓ should trackPageView with correct title on route change (5 ms)
✓ should use previousPath as referer on consecutive route change (10 ms)
✓ should trackPageView with correct title on route change (3 ms)
✓ should use previousPath as referer on consecutive route change (2 ms)
✓ should track route as search in /recherche (1 ms)
✓ should track route as search in /search (2 ms)
✓ should work if the surcharge of the operator (2 ms)
excludeUrlsPatterns
✓ should excluded login.php and token variables (7 ms)
✓ should exclude initial page tracking (4 ms)
✓ should track initial page if not excluded (3 ms)
✓ should excluded login.php and token variables (2 ms)
✓ should exclude initial page tracking (3 ms)
✓ should track initial page if not excluded (2 ms)
disableCookies
✓ should NOT append disableCookies to window._paq by default (1 ms)
✓ should append disableCookies to window._paq (1 ms)
```
14 changes: 14 additions & 0 deletions src/__tests__/matomo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,17 @@ describe("excludeUrlsPatterns", () => {
});

// todo: should track pageview on next router routeChangeComplete

describe("disableCookies", () => {
test("should NOT append disableCookies to window._paq by default", () => {
init({ disableCookies: false, siteId: "42", url: "YO" });
expect(window._paq).not.toEqual(
expect.arrayContaining([["disableCookies"]])
);
});

test("should append disableCookies to window._paq", () => {
init({ disableCookies: true, siteId: "42", url: "YO" });
expect(window._paq).toEqual(expect.arrayContaining([["disableCookies"]]));
});
});
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface InitSettings {
jsTrackerFile?: string;
phpTrackerFile?: string;
excludeUrlsPatterns?: RegExp[];
disableCookies?: boolean;
onRouteChangeStart?: (path: string) => void;
onRouteChangeComplete?: (path: string) => void;
onInitialization?: () => void;
Expand Down Expand Up @@ -64,6 +65,7 @@ export function init({
jsTrackerFile = "matomo.js",
phpTrackerFile = "matomo.php",
excludeUrlsPatterns = [],
disableCookies = false,
onRouteChangeStart = undefined,
onRouteChangeComplete = undefined,
onInitialization = undefined,
Expand All @@ -89,6 +91,10 @@ export function init({
push(["trackPageView"]);
}

if (disableCookies) {
push(["disableCookies"]);
}

push(["enableLinkTracking"]);
push(["setTrackerUrl", `${url}/${phpTrackerFile}`]);
push(["setSiteId", siteId]);
Expand Down

0 comments on commit 3d50f80

Please sign in to comment.