Skip to content

Commit

Permalink
feat(ts): add dimension support (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
rap2hpoutre committed Jul 11, 2022
1 parent 2c661bd commit 417e914
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
37 changes: 35 additions & 2 deletions custom.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
// / <reference types="next" />
// / <reference types="next/types/global" />

interface Dimensions {
dimension1?: string;
dimension2?: string;
dimension3?: string;
dimension4?: string;
dimension5?: string;
dimension6?: string;
dimension7?: string;
dimension8?: string;
dimension9?: string;
dimension10?: string;
}

interface Window {
_paq?: null | (string | string[] | number | number[])[][];
_paq?:
| (
| Dimensions
| number[]
| string[]
| number
| string
| null
| undefined
)[][]
| null;
}
declare namespace NodeJS {
interface Global {
_paq?: null | (string | string[] | number | number[])[][];
_paq?:
| (
| Dimensions
| number[]
| string[]
| number
| string
| null
| undefined
)[][]
| null;
}
}
16 changes: 16 additions & 0 deletions src/__tests__/__snapshots__/matomo.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ Array [
]
`;

exports[`push should append dimensions data to window._paq 1`] = `
Array [
Array [
"trackEvent",
"kikoo",
"lol",
null,
null,
Object {
"dimension1": "ok",
"dimension2": "foobar",
},
],
]
`;

exports[`router.routeChangeComplete event should track route as search in /recherche 1`] = `
Array [
Array [
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/matomo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ describe("push", () => {
push(["trackEvent", "kikoo", "lol"]);
expect(window._paq).toMatchSnapshot();
});

test("should append dimensions data to window._paq", () => {
init({ siteId: "42", url: "YO" });
window._paq = [];
push([
"trackEvent",
"kikoo",
"lol",
null,
null,
{ dimension1: "ok", dimension2: "foobar" },
]);
expect(window._paq).toMatchSnapshot();
});
});

describe("router.routeChangeStart event", () => {
Expand Down
25 changes: 24 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,31 @@ interface InitSettings {
excludeUrlsPatterns?: RegExp[];
}

interface Dimensions {
dimension1?: string;
dimension2?: string;
dimension3?: string;
dimension4?: string;
dimension5?: string;
dimension6?: string;
dimension7?: string;
dimension8?: string;
dimension9?: string;
dimension10?: string;
}

// to push custom events
export function push(args: (number[] | string[] | number | string)[]): void {
export function push(
args: (
| Dimensions
| number[]
| string[]
| number
| string
| null
| undefined
)[]
): void {
if (!window._paq) {
window._paq = [];
}
Expand Down

0 comments on commit 417e914

Please sign in to comment.