matomo-tracker-for-react 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +194 -0
  3. package/es/MatomoContext.d.ts +5 -0
  4. package/es/MatomoContext.d.ts.map +1 -0
  5. package/es/MatomoContext.js +4 -0
  6. package/es/MatomoContext.js.map +1 -0
  7. package/es/MatomoProvider.d.ts +5 -0
  8. package/es/MatomoProvider.d.ts.map +1 -0
  9. package/es/MatomoProvider.js +94 -0
  10. package/es/MatomoProvider.js.map +1 -0
  11. package/es/MatomoTracker.d.ts +49 -0
  12. package/es/MatomoTracker.d.ts.map +1 -0
  13. package/es/MatomoTracker.js +330 -0
  14. package/es/MatomoTracker.js.map +1 -0
  15. package/es/constants.d.ts +11 -0
  16. package/es/constants.d.ts.map +1 -0
  17. package/es/constants.js +13 -0
  18. package/es/constants.js.map +1 -0
  19. package/es/index.d.ts +15 -0
  20. package/es/index.d.ts.map +1 -0
  21. package/es/index.js +6 -0
  22. package/es/index.js.map +1 -0
  23. package/es/tracker-types.d.ts +76 -0
  24. package/es/tracker-types.d.ts.map +1 -0
  25. package/es/tracker-types.js +2 -0
  26. package/es/tracker-types.js.map +1 -0
  27. package/es/types.d.ts +19 -0
  28. package/es/types.d.ts.map +1 -0
  29. package/es/types.js +3 -0
  30. package/es/types.js.map +1 -0
  31. package/es/useMatomo.d.ts +4 -0
  32. package/es/useMatomo.d.ts.map +1 -0
  33. package/es/useMatomo.js +30 -0
  34. package/es/useMatomo.js.map +1 -0
  35. package/lib/MatomoContext.d.ts +5 -0
  36. package/lib/MatomoContext.d.ts.map +1 -0
  37. package/lib/MatomoContext.js +6 -0
  38. package/lib/MatomoContext.js.map +1 -0
  39. package/lib/MatomoProvider.d.ts +5 -0
  40. package/lib/MatomoProvider.d.ts.map +1 -0
  41. package/lib/MatomoProvider.js +122 -0
  42. package/lib/MatomoProvider.js.map +1 -0
  43. package/lib/MatomoTracker.d.ts +49 -0
  44. package/lib/MatomoTracker.d.ts.map +1 -0
  45. package/lib/MatomoTracker.js +332 -0
  46. package/lib/MatomoTracker.js.map +1 -0
  47. package/lib/constants.d.ts +11 -0
  48. package/lib/constants.d.ts.map +1 -0
  49. package/lib/constants.js +16 -0
  50. package/lib/constants.js.map +1 -0
  51. package/lib/index.d.ts +15 -0
  52. package/lib/index.d.ts.map +1 -0
  53. package/lib/index.js +29 -0
  54. package/lib/index.js.map +1 -0
  55. package/lib/tracker-types.d.ts +76 -0
  56. package/lib/tracker-types.d.ts.map +1 -0
  57. package/lib/tracker-types.js +3 -0
  58. package/lib/tracker-types.js.map +1 -0
  59. package/lib/types.d.ts +19 -0
  60. package/lib/types.d.ts.map +1 -0
  61. package/lib/types.js +19 -0
  62. package/lib/types.js.map +1 -0
  63. package/lib/useMatomo.d.ts +4 -0
  64. package/lib/useMatomo.d.ts.map +1 -0
  65. package/lib/useMatomo.js +35 -0
  66. package/lib/useMatomo.js.map +1 -0
  67. package/package.json +58 -0
  68. package/src/MatomoContext.tsx +6 -0
  69. package/src/MatomoProvider.tsx +93 -0
  70. package/src/MatomoTracker.ts +415 -0
  71. package/src/constants.ts +12 -0
  72. package/src/index.ts +20 -0
  73. package/src/tracker-types.ts +89 -0
  74. package/src/types.ts +29 -0
  75. package/src/useMatomo.ts +55 -0
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=tracker-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracker-types.js","sourceRoot":"","sources":["../src/tracker-types.ts"],"names":[],"mappings":""}
package/es/types.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import React from "react";
2
+ export * from "./tracker-types";
3
+ export interface MatomoProviderProps {
4
+ children: React.ReactNode;
5
+ urlBase: string;
6
+ siteId: string | number;
7
+ trackCookies?: boolean;
8
+ disabled?: boolean;
9
+ }
10
+ export interface MatomoInstance {
11
+ trackEvent: (category: string, action: string, name?: string, value?: number) => void;
12
+ trackPageView: (customTitle?: string) => void;
13
+ trackGoal: (goalId: number | string, revenue?: number) => void;
14
+ setUserId: (userId: string) => void;
15
+ trackLink: (url: string, linkType: "link" | "download") => void;
16
+ pushInstruction: (instruction: any[]) => void;
17
+ }
18
+ export type UseMatomo = MatomoInstance;
19
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,cAAc,iBAAiB,CAAC;AAEhC,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,cAAc;IAC7B,UAAU,EAAE,CACV,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,MAAM,EACb,KAAK,CAAC,EAAE,MAAM,KACX,IAAI,CAAC;IACV,aAAa,EAAE,CAAC,WAAW,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9C,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/D,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IACpC,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,UAAU,KAAK,IAAI,CAAC;IAChE,eAAe,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;CAC/C;AAGD,MAAM,MAAM,SAAS,GAAG,cAAc,CAAC"}
package/es/types.js ADDED
@@ -0,0 +1,3 @@
1
+ // Re-export tracker types for convenience if needed by consumers of the React package
2
+ export * from "./tracker-types";
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,sFAAsF;AACtF,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { UseMatomo } from "./types";
2
+ declare function useMatomo(): UseMatomo;
3
+ export default useMatomo;
4
+ //# sourceMappingURL=useMatomo.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useMatomo.d.ts","sourceRoot":"","sources":["../src/useMatomo.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEpC,iBAAS,SAAS,IAAI,SAAS,CAgD9B;AAED,eAAe,SAAS,CAAC"}
@@ -0,0 +1,30 @@
1
+ import { useContext, useCallback } from "react";
2
+ import MatomoContext from "./MatomoContext";
3
+ function useMatomo() {
4
+ var context = useContext(MatomoContext);
5
+ if (context === null) {
6
+ throw new Error("useMatomo must be used within a MatomoProvider");
7
+ }
8
+ var trackPageView = useCallback(function (customTitle) { return context.trackPageView(customTitle); }, [context]);
9
+ var trackEvent = useCallback(function (category, action, name, value) {
10
+ return context.trackEvent(category, action, name, value);
11
+ }, [context]);
12
+ var trackGoal = useCallback(function (goalId, revenue) {
13
+ return context.trackGoal(goalId, revenue);
14
+ }, [context]);
15
+ var setUserId = useCallback(function (userId) { return context.setUserId(userId); }, [context]);
16
+ var trackLink = useCallback(function (url, linkType) {
17
+ return context.trackLink(url, linkType);
18
+ }, [context]);
19
+ var pushInstruction = useCallback(function (instruction) { return context.pushInstruction(instruction); }, [context]);
20
+ return {
21
+ trackEvent: trackEvent,
22
+ trackPageView: trackPageView,
23
+ trackGoal: trackGoal,
24
+ setUserId: setUserId,
25
+ trackLink: trackLink,
26
+ pushInstruction: pushInstruction,
27
+ };
28
+ }
29
+ export default useMatomo;
30
+ //# sourceMapp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AC;IAEF,IAAM,eAAe,GAAG,WAAW,CACjC,UAAC,WAAkB,IAAK,OAAA,OAAO,CAAC,eAAe,CAAC,WAAW,CAAC,EAApC,CAAoC,EAC5D,CAAC,OAAO,CAAC,CACV,CAAC;IAEF,OAAO;QACL,UAAU,YAAA;QACV,aAAa,eAAA;QACb,SAAS,WAAA;QACT,SAAS,WAAA;QACT,SAAS,WAAA;QACT,eAAe,iBAAA;KAChB,CAAC;AACJ,CAAC;AAED,eAAe,SAAS,CAAC"}
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ import { MatomoInstance } from './types';
3
+ declare const MatomoContext: import("react").Context<MatomoInstance | null>;
4
+ export default MatomoContext;
5
+ //# sourceMappingURL=MatomoContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MatomoContext.d.ts","sourceRoot":"","sources":["../src/MatomoContext.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAExC,QAAA,MAAM,aAAa,gDAA6C,CAAA;AAEhE,eAAe,aAAa,CAAA"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var react_1 = require("react");
4
+ var MatomoContext = (0, react_1.createContext)(null);
5
+ exports.default = MatomoContext;
6
+ //# sourceMappingURL=MatomoContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MatomoContext.js","sourceRoot":"","sources":["../src/MatomoContext.tsx"],"names":[],"mappings":";;AAAA,+BAAqC;AAGrC,IAAM,aAAa,GAAG,IAAA,qBAAa,EAAwB,IAAI,CAAC,CAAA;AAEhE,kBAAe,aAAa,CAAA"}
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ import { MatomoProviderProps } from './types';
3
+ declare const MatomoProvider: React.FC<MatomoProviderProps>;
4
+ export default MatomoProvider;
5
+ //# sourceMappingURL=MatomoProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MatomoProvider.d.ts","sourceRoot":"","sources":["../src/MatomoProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAqC,MAAM,OAAO,CAAA;AAIzD,OAAO,EAAE,mBAAmB,EAA+B,MAAM,SAAS,CAAA;AAI1E,QAAA,MAAM,cAAc,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,CAkFjD,CAAA;AAED,eAAe,cAAc,CAAA"}
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
26
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
27
+ if (ar || !(i in from)) {
28
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
29
+ ar[i] = from[i];
30
+ }
31
+ }
32
+ return to.concat(ar || Array.prototype.slice.call(from));
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ var react_1 = __importStar(require("react"));
39
+ var react_router_dom_1 = require("react-router-dom"); // For automatic page view tracking
40
+ var MatomoContext_1 = __importDefault(require("./MatomoContext"));
41
+ var MatomoTracker_1 = __importDefault(require("./MatomoTracker"));
42
+ var constants_1 = require("./constants");
43
+ var MatomoProvider = function (_a) {
44
+ var children = _a.children, urlBase = _a.urlBase, siteId = _a.siteId, _b = _a.disabled, disabled = _b === void 0 ? false : _b, _c = _a.trackCookies, trackCookies = _c === void 0 ? true : _c;
45
+ var matomoInstanceRef = (0, react_1.useRef)(null);
46
+ var location = (0, react_router_dom_1.useLocation)(); // From react-router-dom
47
+ // Initialize MatomoTracker instance
48
+ if (!matomoInstanceRef.current && typeof window !== 'undefined' && !disabled) {
49
+ var matomoSiteId = typeof siteId === 'string' ? parseInt(siteId, 10) : siteId;
50
+ if (isNaN(matomoSiteId)) {
51
+ console.error("Matomo siteId must be a number or a string parseable to a number.");
52
+ }
53
+ else {
54
+ var effectiveConfigurations = {};
55
+ if (!trackCookies) {
56
+ effectiveConfigurations.disableCookies = true;
57
+ }
58
+ var trackerOptions = {
59
+ urlBase: urlBase,
60
+ siteId: matomoSiteId,
61
+ disabled: disabled,
62
+ configurations: effectiveConfigurations, // Pass only relevant configurations
63
+ };
64
+ matomoInstanceRef.current = new MatomoTracker_1.default(trackerOptions);
65
+ }
66
+ }
67
+ var matomoActions = (0, react_1.useMemo)(function () {
68
+ var currentInstance = matomoInstanceRef.current; // Use a variable for the dependency array
69
+ if (!currentInstance || disabled) {
70
+ var noOp = function () { };
71
+ var noOpInstance = {
72
+ trackEvent: noOp,
73
+ trackPageView: noOp,
74
+ trackGoal: noOp,
75
+ setUserId: noOp,
76
+ trackLink: noOp,
77
+ pushInstruction: function () {
78
+ var _args = [];
79
+ for (var _i = 0; _i < arguments.length; _i++) {
80
+ _args[_i] = arguments[_i];
81
+ }
82
+ },
83
+ };
84
+ return noOpInstance;
85
+ }
86
+ return {
87
+ trackEvent: function (category, action, name, value) {
88
+ return currentInstance.trackEvent({ category: category, action: action, name: name, value: value });
89
+ },
90
+ trackPageView: function (customTitle) {
91
+ return currentInstance.trackPageView(customTitle ? { documentTitle: customTitle } : undefined);
92
+ },
93
+ trackGoal: function (goalId, revenue) {
94
+ return currentInstance.pushInstruction(constants_1.TRACK_TYPES.TRACK_GOAL, goalId, revenue);
95
+ },
96
+ setUserId: function (uid) { return currentInstance.pushInstruction(constants_1.TRACK_TYPES.SET_USER_ID, uid); },
97
+ trackLink: function (url, linkType) {
98
+ return currentInstance.trackLink({ href: url, linkType: linkType });
99
+ },
100
+ pushInstruction: function (instruction) {
101
+ if (Array.isArray(instruction) && instruction.length > 0) {
102
+ var name_1 = instruction[0], args = instruction.slice(1);
103
+ currentInstance.pushInstruction.apply(currentInstance, __spreadArray([name_1], args, false));
104
+ }
105
+ else {
106
+ console.warn('pushInstruction expects a non-empty array.');
107
+ }
108
+ },
109
+ };
110
+ }, [disabled, matomoInstanceRef.current]); // Use matomoInstanceRef.current here
111
+ // Effect for automatic page view tracking on route change
112
+ (0, react_1.useEffect)(function () {
113
+ if (matomoActions && matomoActions.trackPageView && !disabled) {
114
+ var currentPath = location.pathname + location.search + location.hash;
115
+ matomoActions.pushInstruction(['setCustomUrl', window.location.origin + currentPath]);
116
+ matomoActions.trackPageView(); // Track with potentially new document.title or default
117
+ }
118
+ }, [location, matomoActions, disabled]);
119
+ return (react_1.default.createElement(MatomoContext_1.default.Provider, { value: matomoActions }, children));
120
+ };
121
+ exports.default = MatomoProvider;
122
+ //# sourceMappingURL=MatomoProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MatomoProvider.js","sourceRoot":"","sources":["../src/MatomoProvider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAAyD;AACzD,qDAA8C,CAAC,mCAAmC;AAClF,kEAA2C;AAC3C,kEAA2C;AAE3C,yCAAyC;AAGzC,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SAAf,CAAgB;oBACpC,eAAe,CAAC,eAAe,OAA/B,eAAe,iBAAiB,MAAI,GAAK,IAAI,UAAE;iBAChD;qBAAM;oBACL,OAAO,CAAC,IAAI,CAAC,4CAA4C,CAAC,CAAC;iBAC5D;YACH,CAAC;SACF,CAAA;IACH,CAAC,EAAE,CAAC,QAAQ,EAAE,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAA,CAAC,qCAAqC;IAE/E,0DAA0D;IAC1D,IAAA,iBAAS,EAAC;QACR,IAAI,aAAa,IAAI,aAAa,CAAC,aAAa,IAAI,CAAC,QAAQ,EAAE;YAC7D,IAAM,WAAW,GAAG,QAAQ,CAAC,QAAQ,GAAG,QAAQ,CAAC,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAA;YACvE,aAAa,CAAC,eAAe,CAAC,CAAC,cAAc,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,WAAW,CAAC,CAAC,CAAA;YACrF,aAAa,CAAC,aAAa,EAAE,CAAA,CAAC,uDAAuD;SACtF;IACH,CAAC,EAAE,CAAC,QAAQ,EAAE,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAA;IAGvC,OAAO,CACL,8BAAC,uBAAa,CAAC,QAAQ,IAAC,KAAK,EAAE,aAAa,IACzC,QAAQ,CACc,CAC1B,CAAA;AACH,CAAC,CAAA;AAED,kBAAe,cAAc,CAAA"}
@@ -0,0 +1,49 @@
1
+ import { AddEcommerceItemParams, RemoveEcommerceItemParams, SetEcommerceViewParams, TrackEcommerceOrderParams, TrackEventParams, TrackLinkParams, TrackPageViewParams, TrackSiteSearchParams, UserOptions } from "./tracker-types";
2
+ declare global {
3
+ interface Window {
4
+ _paq?: Array<any[]>;
5
+ }
6
+ }
7
+ declare class MatomoTracker {
8
+ private mutationObserver?;
9
+ private isInitialized;
10
+ constructor(userOptions: UserOptions);
11
+ private initialize;
12
+ enableHeartBeatTimer(seconds: number): void;
13
+ enableLinkTracking(active: boolean): void;
14
+ private trackEventsForElements;
15
+ trackEvents(): void;
16
+ stopObserving(): void;
17
+ trackEvent({ category, action, name, value, ...otherParams }: TrackEventParams): void;
18
+ trackSiteSearch({ keyword, category, count, ...otherParams }: TrackSiteSearchParams): void;
19
+ trackLink({ href, linkType, customDimensions, }: TrackLinkParams): void;
20
+ trackPageView(params?: TrackPageViewParams): void;
21
+ addEcommerceItem({ sku, productName, productCategory, productPrice, productQuantity, }: AddEcommerceItemParams): void;
22
+ removeEcommerceItem({ sku }: RemoveEcommerceItemParams): void;
23
+ clearEcommerceCart(): void;
24
+ trackEcommerceOrder({ orderId, orderRevenue, orderSubTotal, taxAmount, shippingAmount, discountOffered, }: TrackEcommerceOrderParams): void;
25
+ trackEcommerceCartUpdate(amount: number): void;
26
+ setEcommerceView({ sku, productName, productCategory, productPrice, }: SetEcommerceViewParams): void;
27
+ setEcommerceCategoryView(productCategory: string): void;
28
+ private track;
29
+ /**
30
+ * Pushes an instruction to Matomo for execution, this is equivalent to pushing entries into the `_paq` array.
31
+ *
32
+ * For example:
33
+ *
34
+ * ```ts
35
+ * pushInstruction('setDocumentTitle', document.title)
36
+ * ```
37
+ * Is the equivalent of:
38
+ *
39
+ * ```ts
40
+ * _paq.push(['setDocumentTitle', document.title]);
41
+ * ```
42
+ *
43
+ * @param name The name of the instruction to be executed.
44
+ * @param args The arguments to pass along with the instruction.
45
+ */
46
+ pushInstruction(name: string, ...args: any[]): MatomoTracker;
47
+ }
48
+ export default MatomoTracker;
49
+ //# sourceMappingURL=MatomoTracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MatomoTracker.d.ts","sourceRoot":"","sources":["../src/MatomoTracker.ts"],"names":[],"mappings":"AACA,OAAO,EACL,sBAAsB,EACtB,yBAAyB,EAEzB,sBAAsB,EACtB,yBAAyB,EACzB,gBAAgB,EAChB,eAAe,EACf,mBAAmB,EAEnB,qBAAqB,EACrB,WAAW,EACZ,MAAM,iBAAiB,CAAC;AAEzB,OAAO,CAAC,MAAM,CAA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AsB,GAAG,IAAI;IAYhC,wBAAwB,CAAC,eAAe,EAAE,MAAM,GAAG,IAAI;IAKvD,OAAO,CAAC,KAAK;IAoCb;;;;;;;;;;;;;;;;OAgBG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,aAAa;CAS7D;AAED,eAAe,aAAa,CAAC"}
@@ -0,0 +1,332 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __rest = (this && this.__rest) || function (s, e) {
14
+ var t = {};
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
+ t[p] = s[p];
17
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
+ t[p[i]] = s[p[i]];
21
+ }
22
+ return t;
23
+ };
24
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
25
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
26
+ if (ar || !(i in from)) {
27
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
28
+ ar[i] = from[i];
29
+ }
30
+ }
31
+ return to.concat(ar || Array.prototype.slice.call(from));
32
+ };
33
+ Object.defineProperty(exports, "__esModule", { value: true });
34
+ var constants_1 = require("./constants");
35
+ var MatomoTracker = /** @class */ (function () {
36
+ function MatomoTracker(userOptions) {
37
+ this.isInitialized = false;
38
+ if (typeof window === "undefined") {
39
+ // Don't run in SSR or non-browser environments
40
+ return;
41
+ }
42
+ if (!userOptions.urlBase) {
43
+ throw new Error("Matomo urlBase is required.");
44
+ }
45
+ if (userOptions.siteId === undefined || userOptions.siteId === null) {
46
+ throw new Error("Matomo siteId is required.");
47
+ }
48
+ this.initialize(userOptions);
49
+ }
50
+ MatomoTracker.prototype.initialize = function (_a) {
51
+ var _this = this;
52
+ var _b;
53
+ var urlBase = _a.urlBase, siteId = _a.siteId, userId = _a.userId, trackerUrl = _a.trackerUrl, srcUrl = _a.srcUrl, _c = _a.disabled, disabled = _c === void 0 ? false : _c, heartBeat = _a.heartBeat, _d = _a.linkTracking, linkTracking = _d === void 0 ? true : _d, _e = _a.configurations, configurations = _e === void 0 ? {} : _e;
54
+ if (disabled) {
55
+ // If disabled, make all tracking calls no-ops by not initializing _paq or the script
56
+ this.pushInstruction = function () {
57
+ var _args = [];
58
+ for (var _i = 0; _i < arguments.length; _i++) {
59
+ _args[_i] = arguments[_i];
60
+ }
61
+ return _this;
62
+ }; // No-op
63
+ // Make other methods no-op as well
64
+ var noOp = function () { };
65
+ this.trackEvent = noOp;
66
+ this.trackPageView = noOp;
67
+ this.trackSiteSearch = noOp;
68
+ this.trackLink = noOp;
69
+ // ... and so on for all public methods
70
+ return;
71
+ }
72
+ var normalizedUrlBase = urlBase.endsWith("/") ? urlBase : "".concat(urlBase, "/");
73
+ window._paq = window._paq || [];
74
+ // Prevent re-initialization if _paq already has Matomo commands beyond simple presence
75
+ if (this.isInitialized ||
76
+ (window._paq.length > 0 &&
77
+ window._paq.some(function (cmd) { return cmd[0] === "setTrackerUrl"; }))) {
78
+ // If siteId or userId needs to be updated on an existing instance:
79
+ if (siteId)
80
+ this.pushInstruction("setSiteId", siteId);
81
+ if (userId)
82
+ this.pushInstruction("setUserId", userId);
83
+ // Potentially re-apply other configurations if needed, or assume they are sticky
84
+ this.isInitialized = true;
85
+ return;
86
+ }
87
+ this.pushInstruction("setTrackerUrl", trackerUrl !== null && trackerUrl !== void 0 ? trackerUrl : "".concat(normalizedUrlBase, "matomo.php"));
88
+ this.pushInstruction("setSiteId", siteId);
89
+ if (userId) {
90
+ this.pushInstruction("setUserId", userId);
91
+ }
92
+ Object.entries(configurations).forEach(function (_a) {
93
+ var name = _a[0], instructions = _a[1];
94
+ if (name === "disableCookies" && typeof instructions === "boolean") {
95
+ _this.pushInstruction("disableCookies"); // Matomo's disableCookies takes no arguments if called this way
96
+ }
97
+ else if (instructions instanceof Array) {
98
+ _this.pushInstruction.apply(_this, __spreadArray([name], instructions, false));
99
+ }
100
+ else {
101
+ _this.pushInstruction(name, instructions);
102
+ }
103
+ });
104
+ if (!heartBeat || (heartBeat && heartBeat.active)) {
105
+ this.enableHeartBeatTimer((_b = (heartBeat && heartBeat.seconds)) !== null && _b !== void 0 ? _b : 15);
106
+ }
107
+ this.enableLinkTracking(linkTracking);
108
+ var doc = document;
109
+ var scriptElement = doc.createElement("script");
110
+ var scripts = doc.getElementsByTagName("script")[0];
111
+ scriptElement.type = "text/javascript";
112
+ scriptElement.async = true;
113
+ scriptElement.defer = true;
114
+ scriptElement.src = srcUrl || "".concat(normalizedUrlBase, "matomo.js");
115
+ if (scripts && scripts.parentNode) {
116
+ scripts.parentNode.insertBefore(scriptElement, scripts);
117
+ }
118
+ this.isInitialized = true;
119
+ };
120
+ MatomoTracker.prototype.enableHeartBeatTimer = function (seconds) {
121
+ this.pushInstruction("enableHeartBeatTimer", seconds);
122
+ };
123
+ MatomoTracker.prototype.enableLinkTracking = function (active) {
124
+ this.pushInstruction("enableLinkTracking", active);
125
+ };
126
+ MatomoTracker.prototype.trackEventsForElements = function (elements) {
127
+ var _this = this;
128
+ if (elements.length) {
129
+ elements.forEach(function (element) {
130
+ element.addEventListener("click", function () {
131
+ var _a = element.dataset, matomoCategory = _a.matomoCategory, matomoAction = _a.matomoAction, matomoName = _a.matomoName, matomoValue = _a.matomoValue;
132
+ if (matomoCategory && matomoAction) {
133
+ _this.trackEvent({
134
+ category: matomoCategory,
135
+ action: matomoAction,
136
+ name: matomoName,
137
+ value: matomoValue ? Number(matomoValue) : undefined,
138
+ });
139
+ }
140
+ else {
141
+ throw new Error("Error: data-matomo-category and data-matomo-action are required.");
142
+ }
143
+ });
144
+ });
145
+ }
146
+ };
147
+ // Tracks events based on data attributes
148
+ MatomoTracker.prototype.trackEvents = function () {
149
+ var _this = this;
150
+ if (typeof window === "undefined" || !document)
151
+ return;
152
+ var matchString = '[data-matomo-event="click"]';
153
+ var firstTime = false;
154
+ if (!this.mutationObserver) {
155
+ firstTime = true;
156
+ this.mutationObserver = new MutationObserver(function (mutations) {
157
+ mutations.forEach(function (mutation) {
158
+ mutation.addedNodes.forEach(function (node) {
159
+ // only track HTML elements
160
+ if (!(node instanceof HTMLElement))
161
+ return;
162
+ // check the inserted element for being a code snippet
163
+ if (node.matches(matchString)) {
164
+ _this.trackEventsForElements([node]);
165
+ }
166
+ var elements = Array.from(node.querySelectorAll(matchString));
167
+ _this.trackEventsForElements(elements);
168
+ });
169
+ });
170
+ });
171
+ }
172
+ this.mutationObserver.observe(document, { childList: true, subtree: true });
173
+ // Now track all already existing elements
174
+ if (firstTime) {
175
+ var elements = Array.from(document.querySelectorAll(matchString));
176
+ this.trackEventsForElements(elements);
177
+ }
178
+ };
179
+ MatomoTracker.prototype.stopObserving = function () {
180
+ if (this.mutationObserver) {
181
+ this.mutationObserver.disconnect();
182
+ }
183
+ };
184
+ // Tracks events
185
+ // https://matomo.org/docs/event-tracking/#tracking-events
186
+ MatomoTracker.prototype.trackEvent = function (_a) {
187
+ var category = _a.category, action = _a.action, name = _a.name, value = _a.value, otherParams = __rest(_a, ["category", "action", "name", "value"]);
188
+ if (category && action) {
189
+ this.track(__assign({ data: [constants_1.TRACK_TYPES.TRACK_EVENT, category, action, name, value] }, otherParams));
190
+ }
191
+ else {
192
+ throw new Error("Error: category and action are required.");
193
+ }
194
+ };
195
+ // Tracks site search
196
+ // https://developer.matomo.org/guides/tracking-javascript-guide#internal-search-tracking
197
+ MatomoTracker.prototype.trackSiteSearch = function (_a) {
198
+ var keyword = _a.keyword, category = _a.category, count = _a.count, otherParams = __rest(_a, ["keyword", "category", "count"]);
199
+ if (keyword) {
200
+ this.track(__assign({ data: [constants_1.TRACK_TYPES.TRACK_SEARCH, keyword, category, count] }, otherParams));
201
+ }
202
+ else {
203
+ throw new Error("Error: keyword is required.");
204
+ }
205
+ };
206
+ // Tracks outgoing links to other sites and downloads
207
+ // https://developer.matomo.org/guides/tracking-javascript-guide#enabling-download-outlink-tracking
208
+ MatomoTracker.prototype.trackLink = function (_a) {
209
+ var _this = this;
210
+ var href = _a.href, _b = _a.linkType, linkType = _b === void 0 ? "link" : _b, customDimensions = _a.customDimensions;
211
+ if (customDimensions &&
212
+ Array.isArray(customDimensions) &&
213
+ customDimensions.length) {
214
+ customDimensions.forEach(function (customDimension) {
215
+ return _this.pushInstruction("setCustomDimension", customDimension.id, customDimension.value);
216
+ });
217
+ }
218
+ this.pushInstruction(constants_1.TRACK_TYPES.TRACK_LINK, href, linkType);
219
+ };
220
+ // Tracks page views
221
+ // https://developer.matomo.org/guides/spa-tracking#tracking-a-new-page-view
222
+ MatomoTracker.prototype.trackPageView = function (params) {
223
+ this.track(__assign({ data: [constants_1.TRACK_TYPES.TRACK_VIEW] }, params));
224
+ };
225
+ // Adds a product to an Ecommerce order to be tracked via trackEcommerceOrder.
226
+ // https://matomo.org/docs/ecommerce-analytics
227
+ // https://matomo.org/docs/ecommerce-analytics/#1-addecommerceitemproductsku-productname-productcategory-price-quantity
228
+ MatomoTracker.prototype.addEcommerceItem = function (_a) {
229
+ var sku = _a.sku, productName = _a.productName, productCategory = _a.productCategory, _b = _a.productPrice, productPrice = _b === void 0 ? 0.0 : _b, _c = _a.productQuantity, productQuantity = _c === void 0 ? 1 : _c;
230
+ this.pushInstruction("addEcommerceItem", sku, productName, productCategory, productPrice, productQuantity);
231
+ };
232
+ // Removes a product from an Ecommerce order to be tracked via trackEcommerceOrder.
233
+ // https://matomo.org/docs/ecommerce-analytics
234
+ MatomoTracker.prototype.removeEcommerceItem = function (_a) {
235
+ var sku = _a.sku;
236
+ this.pushInstruction("removeEcommerceItem", sku);
237
+ };
238
+ // Removes all products from an Ecommerce order to be tracked via trackEcommerceOrder.
239
+ // https://matomo.org/docs/ecommerce-analytics
240
+ MatomoTracker.prototype.clearEcommerceCart = function () {
241
+ this.pushInstruction("clearEcommerceCart");
242
+ };
243
+ // Tracks an Ecommerce order containing items added via addEcommerceItem.
244
+ // https://matomo.org/docs/ecommerce-analytics/#2-trackecommerceorderorderid-revenue-subtotal-tax-shipping-discount
245
+ MatomoTracker.prototype.trackEcommerceOrder = function (_a) {
246
+ var orderId = _a.orderId, orderRevenue = _a.orderRevenue, orderSubTotal = _a.orderSubTotal, taxAmount = _a.taxAmount, shippingAmount = _a.shippingAmount, _b = _a.discountOffered, discountOffered = _b === void 0 ? false : _b;
247
+ this.track({
248
+ data: [
249
+ constants_1.TRACK_TYPES.TRACK_ECOMMERCE_ORDER,
250
+ orderId,
251
+ orderRevenue,
252
+ orderSubTotal,
253
+ taxAmount,
254
+ shippingAmount,
255
+ discountOffered,
256
+ ],
257
+ });
258
+ };
259
+ // Tracks updates to an Ecommerce Cart before an actual purchase.
260
+ // This will replace currently tracked information of the cart. Always include all items of the updated cart!
261
+ // https://matomo.org/docs/ecommerce-analytics/#example-tracking-an-ecommerce-cart-update-containing-two-products
262
+ MatomoTracker.prototype.trackEcommerceCartUpdate = function (amount) {
263
+ this.pushInstruction(constants_1.TRACK_TYPES.TRACK_ECOMMERCE_CART_UPDATE, amount);
264
+ };
265
+ // Marks the next page view as an Ecommerce product page.
266
+ // https://matomo.org/docs/ecommerce-analytics/#example-tracking-a-product-page-view
267
+ MatomoTracker.prototype.setEcommerceView = function (_a) {
268
+ var sku = _a.sku, productName = _a.productName, productCategory = _a.productCategory, productPrice = _a.productPrice;
269
+ this.pushInstruction("setEcommerceView", sku, productName, productCategory, productPrice);
270
+ };
271
+ // Marks the next tracked page view as an Ecommerce category page.
272
+ // https://matomo.org/docs/ecommerce-analytics/#example-tracking-a-category-page-view
273
+ MatomoTracker.prototype.setEcommerceCategoryView = function (productCategory) {
274
+ this.setEcommerceView({ productCategory: productCategory, productName: false, sku: false });
275
+ };
276
+ // Sends the tracked page/view/search to Matomo
277
+ MatomoTracker.prototype.track = function (_a) {
278
+ var _this = this;
279
+ var _b = _a.data, data = _b === void 0 ? [] : _b, documentTitle = _a.documentTitle, // Changed: use passed documentTitle or fallback to window.document.title later
280
+ href = _a.href, _c = _a.customDimensions, customDimensions = _c === void 0 ? false : _c;
281
+ if (typeof window === "undefined")
282
+ return;
283
+ if (data.length) {
284
+ if (customDimensions &&
285
+ Array.isArray(customDimensions) &&
286
+ customDimensions.length) {
287
+ customDimensions.forEach(function (customDimension // Corrected type
288
+ ) {
289
+ return _this.pushInstruction("setCustomDimension", customDimension.id, customDimension.value);
290
+ });
291
+ }
292
+ this.pushInstruction("setCustomUrl", href !== null && href !== void 0 ? href : window.location.href);
293
+ // Use provided documentTitle, fallback to actual document.title only if not provided
294
+ this.pushInstruction("setDocumentTitle", documentTitle !== null && documentTitle !== void 0 ? documentTitle : window.document.title);
295
+ this.pushInstruction.apply(this, data);
296
+ }
297
+ };
298
+ /**
299
+ * Pushes an instruction to Matomo for execution, this is equivalent to pushing entries into the `_paq` array.
300
+ *
301
+ * For example:
302
+ *
303
+ * ```ts
304
+ * pushInstruction('setDocumentTitle', document.title)
305
+ * ```
306
+ * Is the equivalent of:
307
+ *
308
+ * ```ts
309
+ * _paq.push(['setDocumentTitle', document.title]);
310
+ * ```
311
+ *
312
+ * @param name The name of the instruction to be executed.
313
+ * @param args The arguments to pass along with the instruction.
314
+ */
315
+ MatomoTracker.prototype.pushInstruction = function (name) {
316
+ var args = [];
317
+ for (var _i = 1; _i < arguments.length; _i++) {
318
+ args[_i - 1] = arguments[_i];
319
+ }
320
+ if (typeof window !== "undefined" && window._paq) {
321
+ window._paq.push(__spreadArray([name], args, true));
322
+ }
323
+ else if (typeof window !== "undefined" && name === "setTrackerUrl") {
324
+ // Allow setting up _paq if it's the first instruction (like setTrackerUrl)
325
+ window._paq = [__spreadArray([name], args, true)];
326
+ }
327
+ return this;
328
+ };
329
+ return MatomoTracker;
330
+ }());
331
+ exports.default = MatomoTracker;
332
+ //# sourceMappingURL=MatomoTracker.js.map