@ogcio/analytics-sdk 0.0.1-beta.3 → 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 +699 -388
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +335 -135
- 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 +24 -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,75 +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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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) {
|
|
299
|
+
this.clearTrackingContext();
|
|
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);
|
|
210
307
|
}
|
|
211
308
|
}
|
|
309
|
+
// Deep copy to avoid mutation of original context
|
|
310
|
+
this.trackingContext = structuredClone(context);
|
|
212
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
|
+
*/
|
|
213
319
|
clearTrackingContext() {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
]);
|
|
320
|
+
if (isBrowser()) {
|
|
321
|
+
if (this.trackingContext?.userId) {
|
|
322
|
+
push(["resetUserId"]);
|
|
323
|
+
}
|
|
324
|
+
if (this.trackingContext?.customDimensions) {
|
|
325
|
+
pushDeleteCustomDimensions(this.trackingContext?.customDimensions);
|
|
221
326
|
}
|
|
222
327
|
}
|
|
223
328
|
this.trackingContext = undefined;
|
|
224
329
|
}
|
|
330
|
+
/**
|
|
331
|
+
* Retrieves the current tracking context configuration.
|
|
332
|
+
*/
|
|
225
333
|
getTrackingContext() {
|
|
226
334
|
return this.trackingContext;
|
|
227
335
|
}
|
|
228
|
-
|
|
229
|
-
*
|
|
230
|
-
*
|
|
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.
|
|
231
339
|
*/
|
|
232
340
|
setDryRun(dryRun) {
|
|
233
341
|
this.isDryRun = dryRun;
|
|
234
|
-
|
|
342
|
+
if (this.isDryRun) {
|
|
343
|
+
console.warn(`Dry run set to: ${this.isDryRun}`);
|
|
344
|
+
}
|
|
235
345
|
}
|
|
236
|
-
|
|
237
|
-
*
|
|
238
|
-
*
|
|
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.
|
|
239
349
|
*/
|
|
240
350
|
setOptOut(optOut) {
|
|
241
351
|
this.isOptOut = optOut;
|
|
242
352
|
console.warn(`Opt out set to: ${this.isOptOut}`);
|
|
243
353
|
}
|
|
354
|
+
/**
|
|
355
|
+
* Returns the current analytics configuration state.
|
|
356
|
+
*/
|
|
244
357
|
getAnalyticsConfig() {
|
|
245
358
|
return {
|
|
246
359
|
dryRun: this.isDryRun,
|
|
247
360
|
optOut: this.isOptOut,
|
|
248
361
|
};
|
|
249
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* Updates the analytics configuration state.
|
|
365
|
+
*/
|
|
250
366
|
setAnalyticsConfig(config) {
|
|
251
367
|
this.isDryRun = config.dryRun;
|
|
252
368
|
this.isOptOut = config.optOut;
|
|
253
369
|
}
|
|
254
|
-
|
|
255
|
-
*
|
|
256
|
-
*
|
|
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.
|
|
257
374
|
*
|
|
258
|
-
* @
|
|
259
|
-
*
|
|
260
|
-
* 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.
|
|
261
377
|
*/
|
|
262
378
|
async initClientTracker(overrideConfig) {
|
|
263
379
|
if (!isBrowser()) {
|
|
264
|
-
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;
|
|
265
385
|
}
|
|
266
386
|
await this.ensureWebsiteConfiguration();
|
|
267
387
|
initTracker({
|
|
@@ -270,12 +390,18 @@ export class Analytics extends BaseClient {
|
|
|
270
390
|
});
|
|
271
391
|
}
|
|
272
392
|
administration = {
|
|
393
|
+
/**
|
|
394
|
+
* Retrieves a list of all configured websites.
|
|
395
|
+
*/
|
|
273
396
|
getWebsites: async () => {
|
|
274
|
-
this.
|
|
275
|
-
return this.clientRequest(this.client.GET("/api/v1/websites/"));
|
|
397
|
+
this.checkToken();
|
|
398
|
+
return await this.clientRequest(() => this.client.GET("/api/v1/websites/"));
|
|
276
399
|
},
|
|
400
|
+
/**
|
|
401
|
+
* Retrieves detailed configuration for a specific website.
|
|
402
|
+
*/
|
|
277
403
|
getWebsiteById: async (id, query) => {
|
|
278
|
-
return this.clientRequest(this.client.GET("/api/v1/websites/{id}", {
|
|
404
|
+
return this.clientRequest(() => this.client.GET("/api/v1/websites/{id}", {
|
|
279
405
|
params: {
|
|
280
406
|
path: {
|
|
281
407
|
id,
|
|
@@ -284,15 +410,21 @@ export class Analytics extends BaseClient {
|
|
|
284
410
|
},
|
|
285
411
|
}));
|
|
286
412
|
},
|
|
413
|
+
/**
|
|
414
|
+
* Creates a new website configuration
|
|
415
|
+
*/
|
|
287
416
|
createWebsite: async (data) => {
|
|
288
|
-
this.
|
|
289
|
-
return this.clientRequest(this.client.POST("/api/v1/websites/", {
|
|
417
|
+
this.checkToken();
|
|
418
|
+
return this.clientRequest(() => this.client.POST("/api/v1/websites/", {
|
|
290
419
|
body: data,
|
|
291
420
|
}));
|
|
292
421
|
},
|
|
422
|
+
/**
|
|
423
|
+
* Updates an existing website configuration.
|
|
424
|
+
*/
|
|
293
425
|
updateWebsite: async (id, data) => {
|
|
294
|
-
this.
|
|
295
|
-
return this.clientRequest(this.client.PATCH("/api/v1/websites/{id}", {
|
|
426
|
+
this.checkToken();
|
|
427
|
+
return this.clientRequest(() => this.client.PATCH("/api/v1/websites/{id}", {
|
|
296
428
|
params: {
|
|
297
429
|
path: {
|
|
298
430
|
id,
|
|
@@ -303,19 +435,30 @@ export class Analytics extends BaseClient {
|
|
|
303
435
|
},
|
|
304
436
|
};
|
|
305
437
|
generate = {
|
|
306
|
-
|
|
307
|
-
|
|
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();
|
|
308
445
|
if (!this.trackingWebsiteId)
|
|
309
446
|
throw new Error("Tracking website ID is not set");
|
|
310
|
-
return this.clientRequest(this.client.
|
|
447
|
+
return this.clientRequest(() => this.client.POST("/api/v1/websites/{id}/tracking-code", {
|
|
311
448
|
params: {
|
|
312
449
|
path: {
|
|
313
450
|
id: this.trackingWebsiteId,
|
|
314
451
|
},
|
|
315
|
-
|
|
452
|
+
body,
|
|
316
453
|
},
|
|
317
454
|
}));
|
|
318
455
|
},
|
|
456
|
+
/**
|
|
457
|
+
* Generates JavaScript code for tracking custom events.
|
|
458
|
+
*
|
|
459
|
+
* @note This method is available only on the backend-side
|
|
460
|
+
*
|
|
461
|
+
*/
|
|
319
462
|
trackEventJsCode: async (data) => {
|
|
320
463
|
return this.trackBackendAction({
|
|
321
464
|
url: "/api/v1/websites/{id}/track-event",
|
|
@@ -323,6 +466,12 @@ export class Analytics extends BaseClient {
|
|
|
323
466
|
generateCode: true,
|
|
324
467
|
});
|
|
325
468
|
},
|
|
469
|
+
/**
|
|
470
|
+
* Generates JavaScript code for tracking page views.
|
|
471
|
+
*
|
|
472
|
+
* @note This method is available only on the backend-side
|
|
473
|
+
*
|
|
474
|
+
*/
|
|
326
475
|
trackPageViewJsCode: async (data) => {
|
|
327
476
|
return this.trackBackendAction({
|
|
328
477
|
url: "/api/v1/websites/{id}/track-page-view",
|
|
@@ -330,6 +479,12 @@ export class Analytics extends BaseClient {
|
|
|
330
479
|
generateCode: true,
|
|
331
480
|
});
|
|
332
481
|
},
|
|
482
|
+
/**
|
|
483
|
+
* Generates JavaScript code for tracking site searches.
|
|
484
|
+
*
|
|
485
|
+
* @note This method is available only on the backend-side
|
|
486
|
+
*
|
|
487
|
+
*/
|
|
333
488
|
trackSiteSearchJsCode: async (data) => {
|
|
334
489
|
return this.trackBackendAction({
|
|
335
490
|
url: "/api/v1/websites/{id}/track-search",
|
|
@@ -337,6 +492,12 @@ export class Analytics extends BaseClient {
|
|
|
337
492
|
generateCode: true,
|
|
338
493
|
});
|
|
339
494
|
},
|
|
495
|
+
/**
|
|
496
|
+
* Generates JavaScript code for tracking link clicks.
|
|
497
|
+
*
|
|
498
|
+
* @note This method is available only on the backend-side
|
|
499
|
+
*
|
|
500
|
+
*/
|
|
340
501
|
trackLinkJsCode: async (data) => {
|
|
341
502
|
return this.trackBackendAction({
|
|
342
503
|
url: "/api/v1/websites/{id}/track-link",
|
|
@@ -344,6 +505,12 @@ export class Analytics extends BaseClient {
|
|
|
344
505
|
generateCode: true,
|
|
345
506
|
});
|
|
346
507
|
},
|
|
508
|
+
/**
|
|
509
|
+
* Generates JavaScript code for tracking content impressions.
|
|
510
|
+
*
|
|
511
|
+
* @note This method is available only on the backend-side
|
|
512
|
+
*
|
|
513
|
+
*/
|
|
347
514
|
trackContentImpressionJsCode: async (data) => {
|
|
348
515
|
return this.trackBackendAction({
|
|
349
516
|
url: "/api/v1/websites/{id}/track-content-impression",
|
|
@@ -351,6 +518,12 @@ export class Analytics extends BaseClient {
|
|
|
351
518
|
generateCode: true,
|
|
352
519
|
});
|
|
353
520
|
},
|
|
521
|
+
/**
|
|
522
|
+
* Generates JavaScript code for tracking content interactions.
|
|
523
|
+
*
|
|
524
|
+
* @note This method is available only on the backend-side
|
|
525
|
+
*
|
|
526
|
+
*/
|
|
354
527
|
trackContentInteractionJsCode: async (data) => {
|
|
355
528
|
return this.trackBackendAction({
|
|
356
529
|
url: "/api/v1/websites/{id}/track-content-interaction",
|
|
@@ -360,6 +533,10 @@ export class Analytics extends BaseClient {
|
|
|
360
533
|
},
|
|
361
534
|
};
|
|
362
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
|
+
*/
|
|
363
540
|
event: async (data) => {
|
|
364
541
|
if (!isBrowser()) {
|
|
365
542
|
return this.trackBackendAction({
|
|
@@ -368,15 +545,20 @@ export class Analytics extends BaseClient {
|
|
|
368
545
|
generateCode: false,
|
|
369
546
|
});
|
|
370
547
|
}
|
|
371
|
-
const { event,
|
|
548
|
+
const { event, siteIds, contextOverride } = data;
|
|
372
549
|
const eventArguments = [
|
|
373
550
|
event.category,
|
|
374
551
|
event.action,
|
|
375
552
|
...(event.name ? [event.name] : []),
|
|
376
553
|
...(event.value ? [event.value] : []),
|
|
377
554
|
];
|
|
378
|
-
return this.trackPush("trackEvent", eventArguments,
|
|
555
|
+
return this.trackPush("trackEvent", eventArguments, siteIds, contextOverride);
|
|
379
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
|
+
*/
|
|
380
562
|
pageView: async (data) => {
|
|
381
563
|
if (!isBrowser()) {
|
|
382
564
|
return this.trackBackendAction({
|
|
@@ -385,10 +567,15 @@ export class Analytics extends BaseClient {
|
|
|
385
567
|
generateCode: false,
|
|
386
568
|
});
|
|
387
569
|
}
|
|
388
|
-
const { event,
|
|
389
|
-
const eventArguments = [event.
|
|
390
|
-
return this.trackPush("trackPageView", eventArguments,
|
|
570
|
+
const { event, siteIds, contextOverride } = data;
|
|
571
|
+
const eventArguments = [event.title];
|
|
572
|
+
return this.trackPush("trackPageView", eventArguments, siteIds, contextOverride);
|
|
391
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
|
+
*/
|
|
392
579
|
siteSearch: async (data) => {
|
|
393
580
|
if (!isBrowser()) {
|
|
394
581
|
return this.trackBackendAction({
|
|
@@ -397,14 +584,21 @@ export class Analytics extends BaseClient {
|
|
|
397
584
|
generateCode: false,
|
|
398
585
|
});
|
|
399
586
|
}
|
|
400
|
-
const { event,
|
|
587
|
+
const { event, siteIds, contextOverride } = data;
|
|
401
588
|
const eventArguments = [
|
|
402
|
-
event.
|
|
403
|
-
...(event.
|
|
404
|
-
...(event.
|
|
589
|
+
event.query,
|
|
590
|
+
...(event.category ? [event.category] : []),
|
|
591
|
+
...(event.count ? [event.count] : []),
|
|
405
592
|
];
|
|
406
|
-
return this.trackPush("trackSiteSearch", eventArguments,
|
|
593
|
+
return this.trackPush("trackSiteSearch", eventArguments, siteIds, contextOverride);
|
|
407
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
|
+
*/
|
|
408
602
|
link: async (data) => {
|
|
409
603
|
if (!isBrowser()) {
|
|
410
604
|
return this.trackBackendAction({
|
|
@@ -413,10 +607,15 @@ export class Analytics extends BaseClient {
|
|
|
413
607
|
generateCode: false,
|
|
414
608
|
});
|
|
415
609
|
}
|
|
416
|
-
const { event,
|
|
610
|
+
const { event, siteIds, contextOverride } = data;
|
|
417
611
|
const eventArguments = [event.link, event.linkType];
|
|
418
|
-
return this.trackPush("trackLink", eventArguments,
|
|
612
|
+
return this.trackPush("trackLink", eventArguments, siteIds, contextOverride);
|
|
419
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
|
+
*/
|
|
420
619
|
contentImpression: async (data) => {
|
|
421
620
|
if (!isBrowser()) {
|
|
422
621
|
return this.trackBackendAction({
|
|
@@ -425,14 +624,15 @@ export class Analytics extends BaseClient {
|
|
|
425
624
|
generateCode: false,
|
|
426
625
|
});
|
|
427
626
|
}
|
|
428
|
-
const { event,
|
|
429
|
-
const eventArguments = [
|
|
430
|
-
|
|
431
|
-
event.content_piece,
|
|
432
|
-
event.content_target,
|
|
433
|
-
];
|
|
434
|
-
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);
|
|
435
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
|
+
*/
|
|
436
636
|
contentInteraction: async (data) => {
|
|
437
637
|
if (!isBrowser()) {
|
|
438
638
|
return this.trackBackendAction({
|
|
@@ -441,14 +641,14 @@ export class Analytics extends BaseClient {
|
|
|
441
641
|
generateCode: false,
|
|
442
642
|
});
|
|
443
643
|
}
|
|
444
|
-
const { event,
|
|
644
|
+
const { event, siteIds, contextOverride } = data;
|
|
445
645
|
const eventArguments = [
|
|
446
|
-
event.
|
|
447
|
-
event.
|
|
448
|
-
event.
|
|
449
|
-
event.
|
|
646
|
+
event.interaction,
|
|
647
|
+
event.name,
|
|
648
|
+
event.piece,
|
|
649
|
+
event.target,
|
|
450
650
|
];
|
|
451
|
-
return this.trackPush("trackContentInteraction", eventArguments,
|
|
651
|
+
return this.trackPush("trackContentInteraction", eventArguments, siteIds, contextOverride);
|
|
452
652
|
},
|
|
453
653
|
};
|
|
454
654
|
}
|