@ogcio/analytics-sdk 0.0.1-beta.4 → 0.1.0-beta.10
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.
- package/dist/base/base-client.d.ts +11 -15
- package/dist/base/base-client.d.ts.map +1 -1
- package/dist/base/base-client.js +35 -34
- package/dist/base/base-client.js.map +1 -1
- package/dist/base/client-utils.d.ts +37 -0
- package/dist/base/client-utils.d.ts.map +1 -0
- package/dist/base/client-utils.js +80 -0
- package/dist/base/client-utils.js.map +1 -0
- package/dist/browser-types.d.ts +16 -8
- package/dist/index.d.ts +685 -379
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +330 -138
- package/dist/index.js.map +1 -1
- package/dist/matomo/helpers.d.ts +14 -11
- package/dist/matomo/helpers.d.ts.map +1 -1
- package/dist/matomo/helpers.js +102 -34
- package/dist/matomo/helpers.js.map +1 -1
- package/dist/schema.d.ts +594 -224
- package/dist/schema.d.ts.map +1 -1
- package/dist/support-files/auth/index.d.ts.map +1 -1
- package/dist/support-files/auth/index.js +3 -0
- package/dist/support-files/auth/index.js.map +1 -1
- package/dist/support-files/building-blocks-sdk.d.ts +2 -3
- package/dist/support-files/building-blocks-sdk.d.ts.map +1 -1
- package/dist/support-files/building-blocks-sdk.js +2 -0
- package/dist/support-files/building-blocks-sdk.js.map +1 -1
- package/dist/tests/testUtils/analytics.mock.d.ts +12 -0
- package/dist/tests/testUtils/analytics.mock.d.ts.map +1 -0
- package/dist/tests/testUtils/analytics.mock.js +21 -0
- package/dist/tests/testUtils/analytics.mock.js.map +1 -0
- package/dist/tests/testUtils/api.mock.handlers.d.ts +7 -0
- package/dist/tests/testUtils/api.mock.handlers.d.ts.map +1 -0
- package/dist/tests/testUtils/api.mock.handlers.js +77 -0
- package/dist/tests/testUtils/api.mock.handlers.js.map +1 -0
- package/dist/tests/testUtils/setup.mock.d.ts +3 -0
- package/dist/tests/testUtils/setup.mock.d.ts.map +1 -0
- package/dist/tests/testUtils/setup.mock.js +43 -0
- package/dist/tests/testUtils/setup.mock.js.map +1 -0
- package/dist/tests/testUtils/wait-request.d.ts +2 -0
- package/dist/tests/testUtils/wait-request.d.ts.map +1 -0
- package/dist/tests/testUtils/wait-request.js +2 -0
- package/dist/tests/testUtils/wait-request.js.map +1 -0
- package/package.json +21 -30
package/dist/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { isBrowser } from "./utils/index.js";
|
|
2
|
-
import {
|
|
2
|
+
import { initMatomoArray, initTracker, MatomoConstants, push, pushDeleteCustomDimensions, pushMatomoCustomDimensions, pushSingleTracker, } from "./matomo/helpers.js";
|
|
3
3
|
export * from "./support-files/auth/index.js";
|
|
4
4
|
// 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
|
|
5
5
|
// 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
|
|
6
|
-
import BaseClient,
|
|
6
|
+
import { BaseClient, ANALYTICS, } from "./base/base-client.js";
|
|
7
|
+
import { formatError, formatResponse, } from "./base/client-utils.js";
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
9
|
const DIMENSION_PREFIX = "dimension";
|
|
8
10
|
export class Analytics extends BaseClient {
|
|
9
11
|
serviceName = ANALYTICS;
|
|
@@ -13,15 +15,15 @@ export class Analytics extends BaseClient {
|
|
|
13
15
|
trackingWebsiteId;
|
|
14
16
|
trackingMetadata;
|
|
15
17
|
trackingContext;
|
|
16
|
-
//
|
|
18
|
+
// Browser environment only variables
|
|
17
19
|
organizationId;
|
|
18
20
|
websiteConfiguration;
|
|
19
21
|
websiteConfigurationPromise;
|
|
20
|
-
constructor({ trackingWebsiteId, organizationId, matomoToken,
|
|
22
|
+
constructor({ trackingWebsiteId, organizationId, matomoToken, dryRun, optOut, ...baseApiClientOptions }) {
|
|
21
23
|
super(baseApiClientOptions);
|
|
22
24
|
this.organizationId = organizationId;
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
25
|
+
this.isDryRun = !!dryRun;
|
|
26
|
+
this.isOptOut = !!optOut;
|
|
25
27
|
if (matomoToken) {
|
|
26
28
|
this.setMatomoToken(matomoToken);
|
|
27
29
|
}
|
|
@@ -36,35 +38,36 @@ export class Analytics extends BaseClient {
|
|
|
36
38
|
if (!this.organizationId) {
|
|
37
39
|
throw new Error("Organization ID is not set");
|
|
38
40
|
}
|
|
39
|
-
const { data } = await this.administration.getWebsiteById(websiteId, {
|
|
40
|
-
|
|
41
|
+
const { data, error } = await this.administration.getWebsiteById(websiteId, {
|
|
42
|
+
orgId: this.organizationId,
|
|
41
43
|
});
|
|
42
|
-
if (!data)
|
|
44
|
+
if (error || !data)
|
|
43
45
|
throw new Error(`Website ${websiteId} not found`);
|
|
44
|
-
this.websiteConfiguration = data
|
|
46
|
+
this.websiteConfiguration = data;
|
|
45
47
|
}
|
|
46
48
|
matomoOnRequestMiddleware({ request }) {
|
|
47
49
|
const headerTokenName = "x-matomo-token";
|
|
48
50
|
request.headers.set(headerTokenName, this.matomoToken || "");
|
|
49
51
|
return request;
|
|
50
52
|
}
|
|
51
|
-
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
54
|
+
async clientRequest(promiseFn) {
|
|
52
55
|
if (this.isDryRun) {
|
|
53
56
|
console.warn("Dry run is enabled, skipping request");
|
|
54
|
-
return {
|
|
57
|
+
return formatResponse({
|
|
58
|
+
data: undefined,
|
|
59
|
+
error: undefined,
|
|
60
|
+
response: new Response(),
|
|
61
|
+
}, this.serviceName, this.logger);
|
|
55
62
|
}
|
|
56
63
|
if (this.isOptOut) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return { data: result.data, error: result.error };
|
|
63
|
-
}
|
|
64
|
-
catch (error) {
|
|
65
|
-
console.error("Error making request", error);
|
|
66
|
-
return { error, data: undefined };
|
|
64
|
+
return formatResponse({
|
|
65
|
+
data: undefined,
|
|
66
|
+
error: undefined,
|
|
67
|
+
response: new Response(),
|
|
68
|
+
}, this.serviceName, this.logger);
|
|
67
69
|
}
|
|
70
|
+
return promiseFn().then((response) => formatResponse(response, this.serviceName, this.logger), (reason) => formatError(reason, this.serviceName, this.logger));
|
|
68
71
|
}
|
|
69
72
|
async ensureWebsiteConfiguration() {
|
|
70
73
|
try {
|
|
@@ -78,29 +81,57 @@ export class Analytics extends BaseClient {
|
|
|
78
81
|
}
|
|
79
82
|
}
|
|
80
83
|
async trackPush(eventName, eventArguments, siteIds, eventContext) {
|
|
84
|
+
if (this.isDryRun) {
|
|
85
|
+
console.warn(`Dry run is enabled, skipping ${eventName} tracking with arguments: ${eventArguments.join(", ")}`);
|
|
86
|
+
return { data: undefined, error: undefined };
|
|
87
|
+
}
|
|
88
|
+
if (this.isOptOut) {
|
|
89
|
+
return { data: undefined, error: undefined };
|
|
90
|
+
}
|
|
81
91
|
await this.ensureWebsiteConfiguration();
|
|
82
92
|
// validate siteIds based on website configuration
|
|
83
93
|
if (siteIds?.length) {
|
|
84
|
-
const validSiteIds = this.websiteConfiguration?.
|
|
94
|
+
const validSiteIds = this.websiteConfiguration?.siteIds;
|
|
85
95
|
if (!validSiteIds?.length) {
|
|
86
|
-
|
|
87
|
-
|
|
96
|
+
const message = "No valid site IDs found in website configuration";
|
|
97
|
+
console.error(message);
|
|
98
|
+
return {
|
|
99
|
+
data: undefined,
|
|
100
|
+
error: {
|
|
101
|
+
code: "MISSING_SITE_IDS",
|
|
102
|
+
detail: message,
|
|
103
|
+
name: "UnprocessableEntity",
|
|
104
|
+
},
|
|
105
|
+
};
|
|
88
106
|
}
|
|
89
107
|
siteIds.forEach((siteId) => {
|
|
90
108
|
if (!validSiteIds.includes(siteId)) {
|
|
91
|
-
|
|
92
|
-
|
|
109
|
+
const message = `Invalid site ID: ${siteId}. Valid site IDs are: ${validSiteIds.join(", ")}`;
|
|
110
|
+
console.error(message);
|
|
111
|
+
return {
|
|
112
|
+
data: undefined,
|
|
113
|
+
error: {
|
|
114
|
+
code: "INVALID_SITE_IDS",
|
|
115
|
+
detail: message,
|
|
116
|
+
name: "UnprocessableEntity",
|
|
117
|
+
},
|
|
118
|
+
};
|
|
93
119
|
}
|
|
94
120
|
});
|
|
95
121
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
122
|
+
// we set some useful values to be used on the frontend
|
|
123
|
+
push(["setCustomUrl", window.location.href]);
|
|
124
|
+
push(["setDocumentTitle", document.title]);
|
|
125
|
+
// to avoid unnecessary calls to resetUserId, we only call it if it was set in the global trackingContext and a new userId is provided
|
|
126
|
+
if (eventContext?.userId) {
|
|
127
|
+
if (this.trackingContext?.userId) {
|
|
128
|
+
push(["resetUserId"]);
|
|
129
|
+
}
|
|
130
|
+
push(["setUserId", eventContext.userId]);
|
|
103
131
|
}
|
|
132
|
+
const globalTrackingContext = this.trackingContext
|
|
133
|
+
?.customDimensions;
|
|
134
|
+
const validCustomDimensions = Object.keys(this.websiteConfiguration?.customDimensions || {});
|
|
104
135
|
// merge of global trackingContext and trackPush eventContext
|
|
105
136
|
const context = {
|
|
106
137
|
...this.trackingContext,
|
|
@@ -110,58 +141,72 @@ export class Analytics extends BaseClient {
|
|
|
110
141
|
...eventContext?.customDimensions,
|
|
111
142
|
},
|
|
112
143
|
};
|
|
113
|
-
if (
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
144
|
+
if (eventContext?.customDimensions &&
|
|
145
|
+
Object.keys(eventContext.customDimensions).length) {
|
|
146
|
+
if (globalTrackingContext) {
|
|
147
|
+
pushDeleteCustomDimensions(globalTrackingContext);
|
|
148
|
+
}
|
|
149
|
+
if (context.customDimensions) {
|
|
150
|
+
pushMatomoCustomDimensions(this.trackingWebsiteId, validCustomDimensions, context.customDimensions);
|
|
151
|
+
}
|
|
118
152
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
console.warn(`Warning: Cannot set custom dimensions - no dimensions configured for website ${this.trackingWebsiteId}`);
|
|
153
|
+
try {
|
|
154
|
+
// CASE 1: if there are no siteIds, we use push() for direct tracking across all configured site IDs
|
|
155
|
+
if (!siteIds?.length) {
|
|
156
|
+
push([eventName, ...eventArguments]);
|
|
124
157
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
158
|
+
else {
|
|
159
|
+
// CASE 2: if there are siteIds, we need to initialize individual trackers for each siteId and track the event separately
|
|
160
|
+
const trackerEndpoint = `${this.websiteConfiguration.trackerUrl}/${this.websiteConfiguration.bypassAdblockers
|
|
161
|
+
? MatomoConstants.TrackerBypass
|
|
162
|
+
: MatomoConstants.Tracker}`;
|
|
163
|
+
for (const siteId of siteIds.map(String)) {
|
|
164
|
+
pushSingleTracker({
|
|
165
|
+
trackerUrl: trackerEndpoint,
|
|
166
|
+
siteId,
|
|
167
|
+
eventName,
|
|
168
|
+
eventArguments,
|
|
169
|
+
context,
|
|
170
|
+
});
|
|
132
171
|
}
|
|
133
172
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
173
|
+
// After tracking, we clear the overridden context and we restore the original global tracking context if exist
|
|
174
|
+
if (eventContext?.customDimensions &&
|
|
175
|
+
Object.keys(eventContext.customDimensions).length) {
|
|
176
|
+
pushDeleteCustomDimensions(eventContext?.customDimensions);
|
|
177
|
+
if (globalTrackingContext) {
|
|
178
|
+
pushMatomoCustomDimensions(this.trackingWebsiteId, validCustomDimensions, globalTrackingContext);
|
|
179
|
+
}
|
|
137
180
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const trackerEndpoint = `${this.websiteConfiguration.tracker_url}/${this.websiteConfiguration.bypass_adblockers
|
|
144
|
-
? MatomoConstants.TrackerBypass
|
|
145
|
-
: MatomoConstants.Tracker}`;
|
|
146
|
-
try {
|
|
147
|
-
for (const siteId of siteIds.map(String)) {
|
|
148
|
-
const matomoTracker = getTracker(trackerEndpoint, siteId);
|
|
149
|
-
matomoTracker[eventName](...eventArguments);
|
|
181
|
+
if (eventContext?.userId) {
|
|
182
|
+
push(["resetUserId"]);
|
|
183
|
+
if (this.trackingContext?.userId) {
|
|
184
|
+
push(["setUserId", this.trackingContext?.userId]);
|
|
185
|
+
}
|
|
150
186
|
}
|
|
151
|
-
return { data:
|
|
187
|
+
return { data: {}, error: undefined };
|
|
152
188
|
}
|
|
153
189
|
catch (error) {
|
|
154
190
|
console.error("Error tracking event", error);
|
|
155
|
-
return {
|
|
191
|
+
return {
|
|
192
|
+
error: {
|
|
193
|
+
code: "TRACKING_ERROR",
|
|
194
|
+
detail: "Error tracking event",
|
|
195
|
+
name: "InternalServerError",
|
|
196
|
+
},
|
|
197
|
+
};
|
|
156
198
|
}
|
|
157
199
|
}
|
|
158
200
|
async trackBackendAction(params) {
|
|
159
|
-
this.
|
|
201
|
+
if (this.isDryRun) {
|
|
202
|
+
console.warn(`Dry run is enabled, skipping request to ${params.url} with arguments: ${JSON.stringify(params.data)}`);
|
|
203
|
+
return { data: undefined, error: undefined };
|
|
204
|
+
}
|
|
160
205
|
if (!this.trackingWebsiteId) {
|
|
161
206
|
throw new Error("Tracking website ID is not set");
|
|
162
207
|
}
|
|
163
208
|
const { url, data, generateCode } = params;
|
|
164
|
-
return this.clientRequest(this.client.POST(url, {
|
|
209
|
+
return this.clientRequest(() => this.client.POST(url, {
|
|
165
210
|
params: {
|
|
166
211
|
path: {
|
|
167
212
|
id: this.trackingWebsiteId,
|
|
@@ -172,20 +217,44 @@ export class Analytics extends BaseClient {
|
|
|
172
217
|
},
|
|
173
218
|
body: {
|
|
174
219
|
...data,
|
|
175
|
-
|
|
220
|
+
metadataOverride: {
|
|
221
|
+
...this.trackingMetadata,
|
|
222
|
+
...data.metadataOverride,
|
|
223
|
+
},
|
|
224
|
+
contextOverride: {
|
|
225
|
+
...this.trackingContext,
|
|
226
|
+
...data.contextOverride,
|
|
227
|
+
},
|
|
176
228
|
},
|
|
177
229
|
}));
|
|
178
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Updates the Matomo authentication token.
|
|
233
|
+
* The token is optional, and is used to validate some params, during the backend requests.
|
|
234
|
+
* If you don't set the token, the validation will be skipped.
|
|
235
|
+
*
|
|
236
|
+
* @note The token is not used in browser environments.
|
|
237
|
+
*/
|
|
179
238
|
setMatomoToken(matomoToken) {
|
|
180
239
|
this.client.eject({ onRequest: this.matomoOnRequestMiddleware.bind(this) });
|
|
181
240
|
this.matomoToken = matomoToken;
|
|
182
241
|
this.client.use({ onRequest: this.matomoOnRequestMiddleware.bind(this) });
|
|
183
242
|
}
|
|
184
|
-
|
|
185
|
-
*
|
|
243
|
+
/**
|
|
244
|
+
* Configures the website ID for tracking purposes
|
|
245
|
+
*
|
|
246
|
+
* When used client-side, this method triggers a background request to fetch the website configuration.
|
|
247
|
+
* The retrieved configuration is then used to initialize the Matomo tracker via the `initClientTracker` method.
|
|
248
|
+
*
|
|
249
|
+
* When used server-side, this method only sets the website ID without making any requests,
|
|
250
|
+
* since configuration retrieval is handled separately on the backend.
|
|
186
251
|
*/
|
|
187
252
|
setTrackingWebsiteId(websiteId) {
|
|
188
253
|
this.trackingWebsiteId = websiteId;
|
|
254
|
+
if (this.isDryRun === true) {
|
|
255
|
+
console.warn(`Dry run is enabled, skipping website configuration retrieval for website ${websiteId}`);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
189
258
|
if (isBrowser() && this.organizationId) {
|
|
190
259
|
// We intentionally don't await the promise here to allow for non-blocking initialization.
|
|
191
260
|
// The website configuration will be ensured before any client-tracking operations in the `ensureWebsiteConfiguration` method.
|
|
@@ -193,83 +262,126 @@ export class Analytics extends BaseClient {
|
|
|
193
262
|
this.retrieveWebsiteConfiguration(websiteId);
|
|
194
263
|
}
|
|
195
264
|
}
|
|
265
|
+
/**
|
|
266
|
+
* Retrieves the currently configured tracking website ID.
|
|
267
|
+
*/
|
|
196
268
|
getTrackingWebsiteId() {
|
|
197
269
|
return this.trackingWebsiteId;
|
|
198
270
|
}
|
|
271
|
+
/**
|
|
272
|
+
* Sets metadata that will be included with all subsequent tracking requests.
|
|
273
|
+
* Setting a new value clears and overrides all previous metadata.
|
|
274
|
+
*
|
|
275
|
+
* @note Metadata is only used for backend tracking requests, as the metadata in browser environments are automatically retrieved.
|
|
276
|
+
*/
|
|
199
277
|
setTrackingMetadata(metadata) {
|
|
200
278
|
this.trackingMetadata = metadata;
|
|
201
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Retrieves the current tracking metadata configuration.
|
|
282
|
+
*/
|
|
202
283
|
getTrackingMetadata() {
|
|
203
284
|
return this.trackingMetadata;
|
|
204
285
|
}
|
|
205
|
-
|
|
286
|
+
/**
|
|
287
|
+
* Removes all configured tracking metadata.
|
|
288
|
+
*/
|
|
289
|
+
clearTrackingMetadata() {
|
|
290
|
+
this.trackingMetadata = undefined;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Sets the tracking context for all subsequent tracking operations.
|
|
294
|
+
* Setting a new value will clear and override all previously configured context data.
|
|
295
|
+
*
|
|
296
|
+
* @note When used in a browser environment, this will update the Matomo tracker with the provided user ID and custom dimensions.
|
|
297
|
+
*/
|
|
298
|
+
async setTrackingContext(context) {
|
|
206
299
|
this.clearTrackingContext();
|
|
207
|
-
if (isBrowser()
|
|
208
|
-
|
|
209
|
-
push([
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
300
|
+
if (isBrowser()) {
|
|
301
|
+
if (context.userId) {
|
|
302
|
+
push(["setUserId", context.userId]);
|
|
303
|
+
}
|
|
304
|
+
if (context.customDimensions) {
|
|
305
|
+
await this.ensureWebsiteConfiguration();
|
|
306
|
+
pushMatomoCustomDimensions(this.trackingWebsiteId, Object.keys(this.websiteConfiguration?.customDimensions || {}), context.customDimensions);
|
|
214
307
|
}
|
|
215
308
|
}
|
|
216
309
|
// Deep copy to avoid mutation of original context
|
|
217
310
|
this.trackingContext = structuredClone(context);
|
|
218
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* Clears all tracking context data, including user ID and custom dimensions,
|
|
314
|
+
* from both the internal state and the Matomo tracker.
|
|
315
|
+
*
|
|
316
|
+
* @note When running in a browser environment, this method also clears
|
|
317
|
+
* the user ID and custom dimensions from the Matomo tracker instance.
|
|
318
|
+
*/
|
|
219
319
|
clearTrackingContext() {
|
|
220
320
|
if (isBrowser()) {
|
|
221
|
-
|
|
321
|
+
if (this.trackingContext?.userId) {
|
|
322
|
+
push(["resetUserId"]);
|
|
323
|
+
}
|
|
222
324
|
if (this.trackingContext?.customDimensions) {
|
|
223
|
-
|
|
224
|
-
push([
|
|
225
|
-
"deleteCustomDimension",
|
|
226
|
-
dimensionId.replace(DIMENSION_PREFIX, ""),
|
|
227
|
-
]);
|
|
228
|
-
}
|
|
325
|
+
pushDeleteCustomDimensions(this.trackingContext?.customDimensions);
|
|
229
326
|
}
|
|
230
327
|
}
|
|
231
328
|
this.trackingContext = undefined;
|
|
232
329
|
}
|
|
330
|
+
/**
|
|
331
|
+
* Retrieves the current tracking context configuration.
|
|
332
|
+
*/
|
|
233
333
|
getTrackingContext() {
|
|
234
334
|
return this.trackingContext;
|
|
235
335
|
}
|
|
236
|
-
|
|
237
|
-
*
|
|
238
|
-
*
|
|
336
|
+
/**
|
|
337
|
+
* Enables or disables dry run mode. When enabled, no request will be sent
|
|
338
|
+
* to the API service, useful for testing and debugging.
|
|
239
339
|
*/
|
|
240
340
|
setDryRun(dryRun) {
|
|
241
341
|
this.isDryRun = dryRun;
|
|
242
|
-
|
|
342
|
+
if (this.isDryRun) {
|
|
343
|
+
console.warn(`Dry run set to: ${this.isDryRun}`);
|
|
344
|
+
}
|
|
243
345
|
}
|
|
244
|
-
|
|
245
|
-
*
|
|
246
|
-
*
|
|
346
|
+
/**
|
|
347
|
+
* Enables or disables tracking opt-out. When enabled, the inizialization of the script will be done, but no tracking data will be sent
|
|
348
|
+
* to the analytics service respecting user privacy preferences.
|
|
247
349
|
*/
|
|
248
350
|
setOptOut(optOut) {
|
|
249
351
|
this.isOptOut = optOut;
|
|
250
352
|
console.warn(`Opt out set to: ${this.isOptOut}`);
|
|
251
353
|
}
|
|
354
|
+
/**
|
|
355
|
+
* Returns the current analytics configuration state.
|
|
356
|
+
*/
|
|
252
357
|
getAnalyticsConfig() {
|
|
253
358
|
return {
|
|
254
359
|
dryRun: this.isDryRun,
|
|
255
360
|
optOut: this.isOptOut,
|
|
256
361
|
};
|
|
257
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* Updates the analytics configuration state.
|
|
365
|
+
*/
|
|
258
366
|
setAnalyticsConfig(config) {
|
|
259
367
|
this.isDryRun = config.dryRun;
|
|
260
368
|
this.isOptOut = config.optOut;
|
|
261
369
|
}
|
|
262
|
-
|
|
263
|
-
*
|
|
264
|
-
*
|
|
370
|
+
/**
|
|
371
|
+
* Initializes the Matomo tracker by injecting the tracking script
|
|
372
|
+
* and configuring it with the website configuration of the currently set trackingWebsiteId.
|
|
373
|
+
* In order to set the user ID and custom dimensions, please use the `setTrackingContext` method.
|
|
265
374
|
*
|
|
266
|
-
* @
|
|
267
|
-
*
|
|
268
|
-
* await analytics.initClientTracker({ user_id: "123" });
|
|
375
|
+
* @note This method is only available in a browser environment, since it injects the tracking script directly into the HEAD of the document.
|
|
376
|
+
* The method also preserves any tracking commands issued before the tracker script loads. These commands are pushed to the Matomo tracker once the script is loaded.
|
|
269
377
|
*/
|
|
270
378
|
async initClientTracker(overrideConfig) {
|
|
271
379
|
if (!isBrowser()) {
|
|
272
|
-
throw new Error("initClientTracker can only be called
|
|
380
|
+
throw new Error("initClientTracker can only be called in a browser environment");
|
|
381
|
+
}
|
|
382
|
+
if (this.isDryRun) {
|
|
383
|
+
console.warn(`Dry run is enabled, skipping initClientTracker with arguments: ${overrideConfig ? JSON.stringify(overrideConfig) : "{}"}`);
|
|
384
|
+
return;
|
|
273
385
|
}
|
|
274
386
|
await this.ensureWebsiteConfiguration();
|
|
275
387
|
initTracker({
|
|
@@ -278,12 +390,18 @@ export class Analytics extends BaseClient {
|
|
|
278
390
|
});
|
|
279
391
|
}
|
|
280
392
|
administration = {
|
|
393
|
+
/**
|
|
394
|
+
* Retrieves a list of all configured websites.
|
|
395
|
+
*/
|
|
281
396
|
getWebsites: async () => {
|
|
282
|
-
this.
|
|
283
|
-
return this.clientRequest(this.client.GET("/api/v1/websites/"));
|
|
397
|
+
this.checkToken();
|
|
398
|
+
return await this.clientRequest(() => this.client.GET("/api/v1/websites/"));
|
|
284
399
|
},
|
|
400
|
+
/**
|
|
401
|
+
* Retrieves detailed configuration for a specific website.
|
|
402
|
+
*/
|
|
285
403
|
getWebsiteById: async (id, query) => {
|
|
286
|
-
return this.clientRequest(this.client.GET("/api/v1/websites/{id}", {
|
|
404
|
+
return this.clientRequest(() => this.client.GET("/api/v1/websites/{id}", {
|
|
287
405
|
params: {
|
|
288
406
|
path: {
|
|
289
407
|
id,
|
|
@@ -292,15 +410,21 @@ export class Analytics extends BaseClient {
|
|
|
292
410
|
},
|
|
293
411
|
}));
|
|
294
412
|
},
|
|
413
|
+
/**
|
|
414
|
+
* Creates a new website configuration
|
|
415
|
+
*/
|
|
295
416
|
createWebsite: async (data) => {
|
|
296
|
-
this.
|
|
297
|
-
return this.clientRequest(this.client.POST("/api/v1/websites/", {
|
|
417
|
+
this.checkToken();
|
|
418
|
+
return this.clientRequest(() => this.client.POST("/api/v1/websites/", {
|
|
298
419
|
body: data,
|
|
299
420
|
}));
|
|
300
421
|
},
|
|
422
|
+
/**
|
|
423
|
+
* Updates an existing website configuration.
|
|
424
|
+
*/
|
|
301
425
|
updateWebsite: async (id, data) => {
|
|
302
|
-
this.
|
|
303
|
-
return this.clientRequest(this.client.PATCH("/api/v1/websites/{id}", {
|
|
426
|
+
this.checkToken();
|
|
427
|
+
return this.clientRequest(() => this.client.PATCH("/api/v1/websites/{id}", {
|
|
304
428
|
params: {
|
|
305
429
|
path: {
|
|
306
430
|
id,
|
|
@@ -311,19 +435,30 @@ export class Analytics extends BaseClient {
|
|
|
311
435
|
},
|
|
312
436
|
};
|
|
313
437
|
generate = {
|
|
314
|
-
|
|
315
|
-
|
|
438
|
+
/**
|
|
439
|
+
* Generates the JavaScript tracking code for initializing analytics on a website.
|
|
440
|
+
*
|
|
441
|
+
* @note This method is available only on the backend-side
|
|
442
|
+
*/
|
|
443
|
+
initTrackingJsCode: async (body) => {
|
|
444
|
+
this.checkToken();
|
|
316
445
|
if (!this.trackingWebsiteId)
|
|
317
446
|
throw new Error("Tracking website ID is not set");
|
|
318
|
-
return this.clientRequest(this.client.
|
|
447
|
+
return this.clientRequest(() => this.client.POST("/api/v1/websites/{id}/tracking-code", {
|
|
319
448
|
params: {
|
|
320
449
|
path: {
|
|
321
450
|
id: this.trackingWebsiteId,
|
|
322
451
|
},
|
|
323
|
-
|
|
452
|
+
body,
|
|
324
453
|
},
|
|
325
454
|
}));
|
|
326
455
|
},
|
|
456
|
+
/**
|
|
457
|
+
* Generates JavaScript code for tracking custom events.
|
|
458
|
+
*
|
|
459
|
+
* @note This method is available only on the backend-side
|
|
460
|
+
*
|
|
461
|
+
*/
|
|
327
462
|
trackEventJsCode: async (data) => {
|
|
328
463
|
return this.trackBackendAction({
|
|
329
464
|
url: "/api/v1/websites/{id}/track-event",
|
|
@@ -331,6 +466,12 @@ export class Analytics extends BaseClient {
|
|
|
331
466
|
generateCode: true,
|
|
332
467
|
});
|
|
333
468
|
},
|
|
469
|
+
/**
|
|
470
|
+
* Generates JavaScript code for tracking page views.
|
|
471
|
+
*
|
|
472
|
+
* @note This method is available only on the backend-side
|
|
473
|
+
*
|
|
474
|
+
*/
|
|
334
475
|
trackPageViewJsCode: async (data) => {
|
|
335
476
|
return this.trackBackendAction({
|
|
336
477
|
url: "/api/v1/websites/{id}/track-page-view",
|
|
@@ -338,6 +479,12 @@ export class Analytics extends BaseClient {
|
|
|
338
479
|
generateCode: true,
|
|
339
480
|
});
|
|
340
481
|
},
|
|
482
|
+
/**
|
|
483
|
+
* Generates JavaScript code for tracking site searches.
|
|
484
|
+
*
|
|
485
|
+
* @note This method is available only on the backend-side
|
|
486
|
+
*
|
|
487
|
+
*/
|
|
341
488
|
trackSiteSearchJsCode: async (data) => {
|
|
342
489
|
return this.trackBackendAction({
|
|
343
490
|
url: "/api/v1/websites/{id}/track-search",
|
|
@@ -345,6 +492,12 @@ export class Analytics extends BaseClient {
|
|
|
345
492
|
generateCode: true,
|
|
346
493
|
});
|
|
347
494
|
},
|
|
495
|
+
/**
|
|
496
|
+
* Generates JavaScript code for tracking link clicks.
|
|
497
|
+
*
|
|
498
|
+
* @note This method is available only on the backend-side
|
|
499
|
+
*
|
|
500
|
+
*/
|
|
348
501
|
trackLinkJsCode: async (data) => {
|
|
349
502
|
return this.trackBackendAction({
|
|
350
503
|
url: "/api/v1/websites/{id}/track-link",
|
|
@@ -352,6 +505,12 @@ export class Analytics extends BaseClient {
|
|
|
352
505
|
generateCode: true,
|
|
353
506
|
});
|
|
354
507
|
},
|
|
508
|
+
/**
|
|
509
|
+
* Generates JavaScript code for tracking content impressions.
|
|
510
|
+
*
|
|
511
|
+
* @note This method is available only on the backend-side
|
|
512
|
+
*
|
|
513
|
+
*/
|
|
355
514
|
trackContentImpressionJsCode: async (data) => {
|
|
356
515
|
return this.trackBackendAction({
|
|
357
516
|
url: "/api/v1/websites/{id}/track-content-impression",
|
|
@@ -359,6 +518,12 @@ export class Analytics extends BaseClient {
|
|
|
359
518
|
generateCode: true,
|
|
360
519
|
});
|
|
361
520
|
},
|
|
521
|
+
/**
|
|
522
|
+
* Generates JavaScript code for tracking content interactions.
|
|
523
|
+
*
|
|
524
|
+
* @note This method is available only on the backend-side
|
|
525
|
+
*
|
|
526
|
+
*/
|
|
362
527
|
trackContentInteractionJsCode: async (data) => {
|
|
363
528
|
return this.trackBackendAction({
|
|
364
529
|
url: "/api/v1/websites/{id}/track-content-interaction",
|
|
@@ -368,6 +533,10 @@ export class Analytics extends BaseClient {
|
|
|
368
533
|
},
|
|
369
534
|
};
|
|
370
535
|
track = {
|
|
536
|
+
/**
|
|
537
|
+
* Tracks custom event
|
|
538
|
+
* @note The contextOverride parameter allows overriding specific custom dimensions for this event only, while preserving other global tracking context values. Set a dimension to null to remove it for this event.
|
|
539
|
+
*/
|
|
371
540
|
event: async (data) => {
|
|
372
541
|
if (!isBrowser()) {
|
|
373
542
|
return this.trackBackendAction({
|
|
@@ -376,15 +545,20 @@ export class Analytics extends BaseClient {
|
|
|
376
545
|
generateCode: false,
|
|
377
546
|
});
|
|
378
547
|
}
|
|
379
|
-
const { event,
|
|
548
|
+
const { event, siteIds, contextOverride } = data;
|
|
380
549
|
const eventArguments = [
|
|
381
550
|
event.category,
|
|
382
551
|
event.action,
|
|
383
552
|
...(event.name ? [event.name] : []),
|
|
384
553
|
...(event.value ? [event.value] : []),
|
|
385
554
|
];
|
|
386
|
-
return this.trackPush("trackEvent", eventArguments,
|
|
555
|
+
return this.trackPush("trackEvent", eventArguments, siteIds, contextOverride);
|
|
387
556
|
},
|
|
557
|
+
/**
|
|
558
|
+
* Tracks page views
|
|
559
|
+
* @note The contextOverride parameter allows overriding specific custom dimensions for this event only, while preserving other global tracking context values. Set a dimension to null to remove it for this event.
|
|
560
|
+
|
|
561
|
+
*/
|
|
388
562
|
pageView: async (data) => {
|
|
389
563
|
if (!isBrowser()) {
|
|
390
564
|
return this.trackBackendAction({
|
|
@@ -393,10 +567,15 @@ export class Analytics extends BaseClient {
|
|
|
393
567
|
generateCode: false,
|
|
394
568
|
});
|
|
395
569
|
}
|
|
396
|
-
const { event,
|
|
397
|
-
const eventArguments = [event.
|
|
398
|
-
return this.trackPush("trackPageView", eventArguments,
|
|
570
|
+
const { event, siteIds, contextOverride } = data;
|
|
571
|
+
const eventArguments = [event.title];
|
|
572
|
+
return this.trackPush("trackPageView", eventArguments, siteIds, contextOverride);
|
|
399
573
|
},
|
|
574
|
+
/**
|
|
575
|
+
* Tracks site search
|
|
576
|
+
* @note The contextOverride parameter allows overriding specific custom dimensions for this event only, while preserving other global tracking context values. Set a dimension to null to remove it for this event.
|
|
577
|
+
|
|
578
|
+
*/
|
|
400
579
|
siteSearch: async (data) => {
|
|
401
580
|
if (!isBrowser()) {
|
|
402
581
|
return this.trackBackendAction({
|
|
@@ -405,14 +584,21 @@ export class Analytics extends BaseClient {
|
|
|
405
584
|
generateCode: false,
|
|
406
585
|
});
|
|
407
586
|
}
|
|
408
|
-
const { event,
|
|
587
|
+
const { event, siteIds, contextOverride } = data;
|
|
409
588
|
const eventArguments = [
|
|
410
|
-
event.
|
|
411
|
-
...(event.
|
|
412
|
-
...(event.
|
|
589
|
+
event.query,
|
|
590
|
+
...(event.category ? [event.category] : []),
|
|
591
|
+
...(event.count ? [event.count] : []),
|
|
413
592
|
];
|
|
414
|
-
return this.trackPush("trackSiteSearch", eventArguments,
|
|
593
|
+
return this.trackPush("trackSiteSearch", eventArguments, siteIds, contextOverride);
|
|
415
594
|
},
|
|
595
|
+
/**
|
|
596
|
+
* Tracks link click
|
|
597
|
+
*
|
|
598
|
+
* if linkType is 'link' the link is an outgoing link, if 'download' the link is a download
|
|
599
|
+
* @note The contextOverride parameter allows overriding specific custom dimensions for this event only, while preserving other global tracking context values. Set a dimension to null to remove it for this event.
|
|
600
|
+
|
|
601
|
+
*/
|
|
416
602
|
link: async (data) => {
|
|
417
603
|
if (!isBrowser()) {
|
|
418
604
|
return this.trackBackendAction({
|
|
@@ -421,10 +607,15 @@ export class Analytics extends BaseClient {
|
|
|
421
607
|
generateCode: false,
|
|
422
608
|
});
|
|
423
609
|
}
|
|
424
|
-
const { event,
|
|
610
|
+
const { event, siteIds, contextOverride } = data;
|
|
425
611
|
const eventArguments = [event.link, event.linkType];
|
|
426
|
-
return this.trackPush("trackLink", eventArguments,
|
|
612
|
+
return this.trackPush("trackLink", eventArguments, siteIds, contextOverride);
|
|
427
613
|
},
|
|
614
|
+
/**
|
|
615
|
+
* Tracks content impression
|
|
616
|
+
* @note The contextOverride parameter allows overriding specific custom dimensions for this event only, while preserving other global tracking context values. Set a dimension to null to remove it for this event.
|
|
617
|
+
|
|
618
|
+
*/
|
|
428
619
|
contentImpression: async (data) => {
|
|
429
620
|
if (!isBrowser()) {
|
|
430
621
|
return this.trackBackendAction({
|
|
@@ -433,14 +624,15 @@ export class Analytics extends BaseClient {
|
|
|
433
624
|
generateCode: false,
|
|
434
625
|
});
|
|
435
626
|
}
|
|
436
|
-
const { event,
|
|
437
|
-
const eventArguments = [
|
|
438
|
-
|
|
439
|
-
event.content_piece,
|
|
440
|
-
event.content_target,
|
|
441
|
-
];
|
|
442
|
-
return this.trackPush("trackContentImpression", eventArguments, site_ids, contextOverride);
|
|
627
|
+
const { event, siteIds, contextOverride } = data;
|
|
628
|
+
const eventArguments = [event.name, event.piece, event.target];
|
|
629
|
+
return this.trackPush("trackContentImpression", eventArguments, siteIds, contextOverride);
|
|
443
630
|
},
|
|
631
|
+
/**
|
|
632
|
+
* Tracks content interaction
|
|
633
|
+
* @note The contextOverride parameter allows overriding specific custom dimensions for this event only, while preserving other global tracking context values. Set a dimension to null to remove it for this event.
|
|
634
|
+
|
|
635
|
+
*/
|
|
444
636
|
contentInteraction: async (data) => {
|
|
445
637
|
if (!isBrowser()) {
|
|
446
638
|
return this.trackBackendAction({
|
|
@@ -449,14 +641,14 @@ export class Analytics extends BaseClient {
|
|
|
449
641
|
generateCode: false,
|
|
450
642
|
});
|
|
451
643
|
}
|
|
452
|
-
const { event,
|
|
644
|
+
const { event, siteIds, contextOverride } = data;
|
|
453
645
|
const eventArguments = [
|
|
454
|
-
event.
|
|
455
|
-
event.
|
|
456
|
-
event.
|
|
457
|
-
event.
|
|
646
|
+
event.interaction,
|
|
647
|
+
event.name,
|
|
648
|
+
event.piece,
|
|
649
|
+
event.target,
|
|
458
650
|
];
|
|
459
|
-
return this.trackPush("trackContentInteraction", eventArguments,
|
|
651
|
+
return this.trackPush("trackContentInteraction", eventArguments, siteIds, contextOverride);
|
|
460
652
|
},
|
|
461
653
|
};
|
|
462
654
|
}
|