@ogcio/analytics-sdk 0.0.1-beta.1 → 0.0.1-beta.2

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 (50) hide show
  1. package/dist/index.d.ts +547 -3
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +86 -9
  4. package/dist/matomo/helpers.d.ts +8 -0
  5. package/dist/matomo/helpers.d.ts.map +1 -1
  6. package/dist/matomo/helpers.js +79 -0
  7. package/dist/matomo/types.d.ts +5 -2
  8. package/dist/matomo/types.d.ts.map +1 -1
  9. package/dist/schema.d.ts +209 -13
  10. package/dist/schema.d.ts.map +1 -1
  11. package/package.json +8 -8
  12. package/src/index.ts +109 -15
  13. package/src/matomo/helpers.ts +101 -0
  14. package/src/matomo/types.ts +18 -2
  15. package/src/schema.ts +209 -13
  16. package/test-report.xml +181 -0
  17. package/dist/ogcio-analytics-sdk-0.0.1.tgz +0 -0
  18. package/dist/package/dist/base/base-client.d.ts +0 -36
  19. package/dist/package/dist/base/base-client.d.ts.map +0 -1
  20. package/dist/package/dist/base/base-client.js +0 -96
  21. package/dist/package/dist/index.d.ts +0 -651
  22. package/dist/package/dist/index.d.ts.map +0 -1
  23. package/dist/package/dist/index.js +0 -321
  24. package/dist/package/dist/matomo/helpers.d.ts +0 -9
  25. package/dist/package/dist/matomo/helpers.d.ts.map +0 -1
  26. package/dist/package/dist/matomo/helpers.js +0 -10
  27. package/dist/package/dist/matomo/types.d.ts +0 -33
  28. package/dist/package/dist/matomo/types.d.ts.map +0 -1
  29. package/dist/package/dist/matomo/types.js +0 -1
  30. package/dist/package/dist/schema.d.ts +0 -1295
  31. package/dist/package/dist/schema.d.ts.map +0 -1
  32. package/dist/package/dist/schema.js +0 -5
  33. package/dist/package/dist/support-files/auth/index.d.ts +0 -3
  34. package/dist/package/dist/support-files/auth/index.d.ts.map +0 -1
  35. package/dist/package/dist/support-files/auth/index.js +0 -76
  36. package/dist/package/dist/support-files/auth/types.d.ts +0 -31
  37. package/dist/package/dist/support-files/auth/types.d.ts.map +0 -1
  38. package/dist/package/dist/support-files/auth/types.js +0 -1
  39. package/dist/package/dist/support-files/building-blocks-sdk.d.ts +0 -17
  40. package/dist/package/dist/support-files/building-blocks-sdk.d.ts.map +0 -1
  41. package/dist/package/dist/support-files/building-blocks-sdk.js +0 -11
  42. package/dist/package/dist/utils/index.d.ts +0 -7
  43. package/dist/package/dist/utils/index.d.ts.map +0 -1
  44. package/dist/package/dist/utils/index.js +0 -19
  45. package/dist/package/examples/environment.d.ts +0 -11
  46. package/dist/package/examples/index.ts +0 -201
  47. package/dist/package/examples/tsconfig.json +0 -15
  48. package/dist/package/package.json +0 -56
  49. package/dist/package/tsconfig.json +0 -18
  50. package/dist/package/types.d.ts +0 -19
@@ -1,321 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { isBrowser } from "./utils/index.js";
8
- import { getTracker, push } from "./matomo/helpers.js";
9
- export * from "./support-files/auth/index.js";
10
- // Since Analytics is a client of the Building-Blocks-SDK, we need to temporarily copy and import the base client from the Building-Blocks-SDK
11
- // in the future, we should move the base client to a base-building-blocks package, in order to avoid circular dependencies and copy-pasting code
12
- import BaseClient, { ANALYTICS, needsBackend, } from "./base/base-client.js";
13
- export class Analytics extends BaseClient {
14
- serviceName = ANALYTICS;
15
- organizationId;
16
- matomoToken;
17
- isDryRun = false;
18
- isOptOut = false;
19
- trackingWebsiteId;
20
- trackingMetadata;
21
- websiteConfiguration;
22
- websiteConfigurationPromise;
23
- constructor({ trackingWebsiteId, organizationId, matomoToken, ...baseApiClientOptions }) {
24
- super(baseApiClientOptions);
25
- this.organizationId = organizationId;
26
- if (matomoToken) {
27
- this.setMatomoToken(matomoToken);
28
- }
29
- if (trackingWebsiteId) {
30
- this.setTrackingWebsiteId(trackingWebsiteId);
31
- }
32
- }
33
- async retrieveWebsiteConfiguration(websiteId) {
34
- if (!this.organizationId) {
35
- throw new Error("Organization ID is not set");
36
- }
37
- const { data } = await this.getWebsiteById(websiteId, {
38
- org_id: this.organizationId,
39
- });
40
- if (!data)
41
- throw new Error(`Website ${websiteId} not found`);
42
- this.websiteConfiguration = data.data;
43
- }
44
- matomoOnRequestMiddleware({ request }) {
45
- const headerTokenName = "x-matomo-token";
46
- request.headers.set(headerTokenName, this.matomoToken || "");
47
- return request;
48
- }
49
- setMatomoToken(matomoToken) {
50
- this.client.eject({ onRequest: this.matomoOnRequestMiddleware.bind(this) });
51
- this.matomoToken = matomoToken;
52
- this.client.use({ onRequest: this.matomoOnRequestMiddleware.bind(this) });
53
- }
54
- clientRequest = async (promise) => {
55
- if (this.isDryRun) {
56
- console.warn("Dry run is enabled, skipping request");
57
- return { data: undefined, error: undefined };
58
- }
59
- if (this.isOptOut) {
60
- console.warn("Opt out is enabled, skipping request");
61
- return { data: undefined, error: undefined };
62
- }
63
- const result = await promise;
64
- return { data: result.data, error: result.error };
65
- };
66
- /*
67
- * Set the tracking website ID and retrieve the website configuration.
68
- */
69
- setTrackingWebsiteId(websiteId) {
70
- this.trackingWebsiteId = websiteId;
71
- if (this.organizationId) {
72
- // We intentionally don't await the promise here to allow for non-blocking initialization.
73
- // The website configuration will be ensured before any client-tracking operations in the `ensureWebsiteConfiguration` method.
74
- this.websiteConfigurationPromise =
75
- this.retrieveWebsiteConfiguration(websiteId);
76
- }
77
- }
78
- getTrackingWebsiteId() {
79
- return this.trackingWebsiteId;
80
- }
81
- setTrackingMetadata(metadata) {
82
- this.trackingMetadata = metadata;
83
- }
84
- getTrackingMetadata() {
85
- return this.trackingMetadata;
86
- }
87
- /*
88
- * The dryRun flag, when set, prevents any data from being sent to the API.
89
- * This is useful for testing/debugging purposes.
90
- */
91
- setDryRun(dryRun) {
92
- this.isDryRun = dryRun;
93
- console.warn(`Dry run set to: ${this.isDryRun}`);
94
- }
95
- /*
96
- * The optOut flag, when set, prevents any data from being sent to the API.
97
- * This is useful for users who want to opt out of data collection.
98
- */
99
- setOptOut(optOut) {
100
- this.isOptOut = optOut;
101
- console.warn(`Opt out set to: ${this.isOptOut}`);
102
- }
103
- getAnalyticsConfig() {
104
- return {
105
- dryRun: this.isDryRun,
106
- optOut: this.isOptOut,
107
- };
108
- }
109
- setAnalyticsConfig(config) {
110
- this.isDryRun = config.dryRun;
111
- this.isOptOut = config.optOut;
112
- }
113
- async ensureWebsiteConfiguration() {
114
- try {
115
- await this.websiteConfigurationPromise;
116
- }
117
- catch (error) {
118
- console.error("Error retrieving website configuration", error);
119
- }
120
- if (!this.websiteConfiguration) {
121
- throw new Error("Website configuration cannot be retrieved");
122
- }
123
- }
124
- async trackPush(eventName, eventArguments, siteIds) {
125
- await this.ensureWebsiteConfiguration();
126
- if (this.isDryRun) {
127
- console.warn("Dry run is enabled, skipping request");
128
- return;
129
- }
130
- if (this.isOptOut) {
131
- console.warn("Opt out is enabled, skipping request");
132
- return;
133
- }
134
- if (!siteIds?.length)
135
- return push([eventName, ...eventArguments]);
136
- const trackerEndpoint = `${this.websiteConfiguration.tracker_url}/${this.websiteConfiguration.bypass_adblockers ? "tr4ck3rp" : "matomo.php"}`;
137
- try {
138
- for (const siteId of siteIds.map(String)) {
139
- const matomoTracker = getTracker(trackerEndpoint, siteId);
140
- matomoTracker[eventName](...eventArguments);
141
- }
142
- return { data: "success" };
143
- }
144
- catch (error) {
145
- console.error("Error tracking event", error);
146
- return { error };
147
- }
148
- }
149
- async trackBackendAction(params) {
150
- if (!this.trackingWebsiteId) {
151
- throw new Error("Tracking website ID is not set");
152
- }
153
- const { url, data, generateCode } = params;
154
- return this.clientRequest(this.client.POST(url, {
155
- params: {
156
- path: {
157
- id: this.trackingWebsiteId,
158
- },
159
- query: {
160
- code: generateCode,
161
- },
162
- },
163
- body: {
164
- ...data,
165
- ...this.trackingMetadata,
166
- },
167
- }));
168
- }
169
- async getWebsites() {
170
- return this.clientRequest(this.client.GET("/api/v1/websites/"));
171
- }
172
- async getWebsiteById(id, query) {
173
- return this.clientRequest(this.client.GET("/api/v1/websites/{id}", {
174
- params: {
175
- path: {
176
- id,
177
- },
178
- query,
179
- },
180
- }));
181
- }
182
- async createWebsite(data) {
183
- return this.clientRequest(this.client.POST("/api/v1/websites/", {
184
- body: data,
185
- }));
186
- }
187
- async updateWebsite(id, data) {
188
- return this.clientRequest(this.client.PATCH("/api/v1/websites/{id}", {
189
- params: {
190
- path: {
191
- id,
192
- },
193
- },
194
- body: data,
195
- }));
196
- }
197
- async generateTrackingCode(query) {
198
- if (!this.trackingWebsiteId)
199
- throw new Error("Tracking website ID is not set");
200
- return this.clientRequest(this.client.GET("/api/v1/websites/{id}/tracking-code", {
201
- params: {
202
- path: {
203
- id: this.trackingWebsiteId,
204
- },
205
- query,
206
- },
207
- }));
208
- }
209
- async trackEvent(data) {
210
- if (!isBrowser()) {
211
- return this.trackBackendAction({
212
- url: "/api/v1/websites/{id}/track-event",
213
- data,
214
- generateCode: false,
215
- });
216
- }
217
- const { event, site_ids } = data;
218
- const eventArguments = [
219
- event.category,
220
- event.action,
221
- ...(event.name ? [event.name] : []),
222
- ...(event.value ? [event.value] : []),
223
- ];
224
- return this.trackPush("trackEvent", eventArguments, site_ids);
225
- }
226
- async generateTrackEventCode(data) {
227
- return this.trackBackendAction({
228
- url: "/api/v1/websites/{id}/track-event",
229
- data,
230
- generateCode: true,
231
- });
232
- }
233
- async trackPageView(data) {
234
- if (!isBrowser()) {
235
- return this.trackBackendAction({
236
- url: "/api/v1/websites/{id}/track-page-view",
237
- data,
238
- generateCode: false,
239
- });
240
- }
241
- const { event, site_ids } = data;
242
- const eventArguments = [event.action_name];
243
- return this.trackPush("trackPageView", eventArguments, site_ids);
244
- }
245
- async generateTrackPageViewCode(data) {
246
- return this.trackBackendAction({
247
- url: "/api/v1/websites/{id}/track-page-view",
248
- data,
249
- generateCode: true,
250
- });
251
- }
252
- async trackSiteSearch(data) {
253
- if (!isBrowser()) {
254
- return this.trackBackendAction({
255
- url: "/api/v1/websites/{id}/track-search",
256
- data,
257
- generateCode: false,
258
- });
259
- }
260
- const { event, site_ids } = data;
261
- const eventArguments = [
262
- event.search_keyword,
263
- ...(event.search_category ? [event.search_category] : []),
264
- ...(event.search_count ? [event.search_count] : []),
265
- ];
266
- return this.trackPush("trackSiteSearch", eventArguments, site_ids);
267
- }
268
- async generateTrackSiteSearchCode(data) {
269
- return this.trackBackendAction({
270
- url: "/api/v1/websites/{id}/track-search",
271
- data,
272
- generateCode: true,
273
- });
274
- }
275
- async trackOutLink(data) {
276
- if (!isBrowser()) {
277
- return this.trackBackendAction({
278
- url: "/api/v1/websites/{id}/track-out-link",
279
- data,
280
- generateCode: false,
281
- });
282
- }
283
- const { event, site_ids } = data;
284
- const eventArguments = [event.link];
285
- return this.trackPush("trackLink", eventArguments, site_ids);
286
- }
287
- async generateTrackOutLinkCode(data) {
288
- return this.trackBackendAction({
289
- url: "/api/v1/websites/{id}/track-out-link",
290
- data,
291
- generateCode: true,
292
- });
293
- }
294
- }
295
- __decorate([
296
- needsBackend
297
- ], Analytics.prototype, "trackBackendAction", null);
298
- __decorate([
299
- needsBackend
300
- ], Analytics.prototype, "getWebsites", null);
301
- __decorate([
302
- needsBackend
303
- ], Analytics.prototype, "createWebsite", null);
304
- __decorate([
305
- needsBackend
306
- ], Analytics.prototype, "updateWebsite", null);
307
- __decorate([
308
- needsBackend
309
- ], Analytics.prototype, "generateTrackingCode", null);
310
- __decorate([
311
- needsBackend
312
- ], Analytics.prototype, "generateTrackEventCode", null);
313
- __decorate([
314
- needsBackend
315
- ], Analytics.prototype, "generateTrackPageViewCode", null);
316
- __decorate([
317
- needsBackend
318
- ], Analytics.prototype, "generateTrackSiteSearchCode", null);
319
- __decorate([
320
- needsBackend
321
- ], Analytics.prototype, "generateTrackOutLinkCode", null);
@@ -1,9 +0,0 @@
1
- import type { PushQueueItem } from "./types.js";
2
- export declare function push(collection: PushQueueItem): number;
3
- export declare function getTracker(trackerUrl: string, siteId: string): {
4
- trackSiteSearch: (...eventArguments: (string | number)[]) => void;
5
- trackEvent: (...eventArguments: (string | number)[]) => void;
6
- trackPageView: (...eventArguments: (string | number)[]) => void;
7
- trackLink: (...eventArguments: (string | number)[]) => void;
8
- };
9
- //# sourceMappingURL=helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/matomo/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,wBAAgB,IAAI,CAAC,UAAU,EAAE,aAAa,UAO7C;AAED,wBAAgB,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;;;;;EAE5D"}
@@ -1,10 +0,0 @@
1
- export function push(collection) {
2
- if (!window._paq)
3
- window._paq = [];
4
- window._paq.push(["setCustomUrl", window.location.href]);
5
- window._paq.push(["setDocumentTitle", document.title]);
6
- return window._paq.push(collection);
7
- }
8
- export function getTracker(trackerUrl, siteId) {
9
- return window.Matomo.getTracker(trackerUrl, siteId);
10
- }
@@ -1,33 +0,0 @@
1
- type SiteSearchTrackEvent = "trackSiteSearch";
2
- type CustomEventTrackEvent = "trackEvent";
3
- type PageViewsTrackEvent = "trackPageView";
4
- type OutlinkTrackEvent = "trackLink";
5
- type PaqServiceMethods = "setCustomUrl" | "setDocumentTitle";
6
- type CustomDimensionsMethods = "setCustomDimensionValue" | "deleteCustomDimension" | "setCustomDimension";
7
- export type TrackEventMethods = SiteSearchTrackEvent | CustomEventTrackEvent | PageViewsTrackEvent | OutlinkTrackEvent;
8
- export type PushQueueMethods = CustomDimensionsMethods | TrackEventMethods | PaqServiceMethods;
9
- export type VisitorInfo = [
10
- isNew: "0" | "1",
11
- visitorId: string,
12
- firstVisitTS: number,
13
- previousVisitCount: string | number,
14
- currentVisitTS: number,
15
- lastVisitTS: number | "",
16
- lastEcommerceOrderTS: number | ""
17
- ];
18
- export type Tracker = {
19
- hasCookies: () => boolean;
20
- getCookieDomain: () => string;
21
- getCookiePath: () => string;
22
- getSessionCookieTimeout: () => number;
23
- getConfigVisitorCookieTimeout: () => number;
24
- getCustomDimensionValue: (id: string | number) => string | undefined;
25
- getLinkTrackingTimer: () => number;
26
- getEcommerceItems: () => object;
27
- getUserId: () => string;
28
- getVisitorId: () => string;
29
- getVisitorInfo: () => VisitorInfo;
30
- };
31
- export type PushQueueItem = [PushQueueMethods, ...unknown[]] | [(this: Tracker) => void];
32
- export {};
33
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/matomo/types.ts"],"names":[],"mappings":"AAAA,KAAK,oBAAoB,GAAG,iBAAiB,CAAC;AAC9C,KAAK,qBAAqB,GAAG,YAAY,CAAC;AAC1C,KAAK,mBAAmB,GAAG,eAAe,CAAC;AAC3C,KAAK,iBAAiB,GAAG,WAAW,CAAC;AACrC,KAAK,iBAAiB,GAAG,cAAc,GAAG,kBAAkB,CAAC;AAC7D,KAAK,uBAAuB,GACxB,yBAAyB,GACzB,uBAAuB,GACvB,oBAAoB,CAAC;AAEzB,MAAM,MAAM,iBAAiB,GACzB,oBAAoB,GACpB,qBAAqB,GACrB,mBAAmB,GACnB,iBAAiB,CAAC;AAEtB,MAAM,MAAM,gBAAgB,GACxB,uBAAuB,GACvB,iBAAiB,GACjB,iBAAiB,CAAC;AAEtB,MAAM,MAAM,WAAW,GAAG;IACxB,KAAK,EAAE,GAAG,GAAG,GAAG;IAChB,SAAS,EAAE,MAAM;IACjB,YAAY,EAAE,MAAM;IACpB,kBAAkB,EAAE,MAAM,GAAG,MAAM;IACnC,cAAc,EAAE,MAAM;IACtB,WAAW,EAAE,MAAM,GAAG,EAAE;IACxB,oBAAoB,EAAE,MAAM,GAAG,EAAE;CAClC,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,UAAU,EAAE,MAAM,OAAO,CAAC;IAC1B,eAAe,EAAE,MAAM,MAAM,CAAC;IAC9B,aAAa,EAAE,MAAM,MAAM,CAAC;IAC5B,uBAAuB,EAAE,MAAM,MAAM,CAAC;IACtC,6BAA6B,EAAE,MAAM,MAAM,CAAC;IAC5C,uBAAuB,EAAE,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;IACrE,oBAAoB,EAAE,MAAM,MAAM,CAAC;IACnC,iBAAiB,EAAE,MAAM,MAAM,CAAC;IAChC,SAAS,EAAE,MAAM,MAAM,CAAC;IACxB,YAAY,EAAE,MAAM,MAAM,CAAC;IAC3B,cAAc,EAAE,MAAM,WAAW,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,aAAa,GACrB,CAAC,gBAAgB,EAAE,GAAG,OAAO,EAAE,CAAC,GAChC,CAAC,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC,CAAC"}
@@ -1 +0,0 @@
1
- export {};