@ogcio/analytics-sdk 0.0.1-beta.4 → 0.1.0-beta.11
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 +333 -139
- 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/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +10 -0
- package/dist/utils/index.js.map +1 -1
- package/package.json +23 -31
package/dist/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { isBrowser } from "./utils/index.js";
|
|
2
|
-
import {
|
|
1
|
+
import { isBrowser, throwIfEmpty } from "./utils/index.js";
|
|
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,19 @@ 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
|
-
|
|
404
|
+
throwIfEmpty(id, "id");
|
|
405
|
+
return this.clientRequest(() => this.client.GET("/api/v1/websites/{id}", {
|
|
287
406
|
params: {
|
|
288
407
|
path: {
|
|
289
408
|
id,
|
|
@@ -292,15 +411,22 @@ export class Analytics extends BaseClient {
|
|
|
292
411
|
},
|
|
293
412
|
}));
|
|
294
413
|
},
|
|
414
|
+
/**
|
|
415
|
+
* Creates a new website configuration
|
|
416
|
+
*/
|
|
295
417
|
createWebsite: async (data) => {
|
|
296
|
-
this.
|
|
297
|
-
return this.clientRequest(this.client.POST("/api/v1/websites/", {
|
|
418
|
+
this.checkToken();
|
|
419
|
+
return this.clientRequest(() => this.client.POST("/api/v1/websites/", {
|
|
298
420
|
body: data,
|
|
299
421
|
}));
|
|
300
422
|
},
|
|
423
|
+
/**
|
|
424
|
+
* Updates an existing website configuration.
|
|
425
|
+
*/
|
|
301
426
|
updateWebsite: async (id, data) => {
|
|
302
|
-
this.
|
|
303
|
-
|
|
427
|
+
this.checkToken();
|
|
428
|
+
throwIfEmpty(id, "id");
|
|
429
|
+
return this.clientRequest(() => this.client.PATCH("/api/v1/websites/{id}", {
|
|
304
430
|
params: {
|
|
305
431
|
path: {
|
|
306
432
|
id,
|
|
@@ -311,19 +437,30 @@ export class Analytics extends BaseClient {
|
|
|
311
437
|
},
|
|
312
438
|
};
|
|
313
439
|
generate = {
|
|
314
|
-
|
|
315
|
-
|
|
440
|
+
/**
|
|
441
|
+
* Generates the JavaScript tracking code for initializing analytics on a website.
|
|
442
|
+
*
|
|
443
|
+
* @note This method is available only on the backend-side
|
|
444
|
+
*/
|
|
445
|
+
initTrackingJsCode: async (body) => {
|
|
446
|
+
this.checkToken();
|
|
316
447
|
if (!this.trackingWebsiteId)
|
|
317
448
|
throw new Error("Tracking website ID is not set");
|
|
318
|
-
return this.clientRequest(this.client.
|
|
449
|
+
return this.clientRequest(() => this.client.POST("/api/v1/websites/{id}/tracking-code", {
|
|
319
450
|
params: {
|
|
320
451
|
path: {
|
|
321
452
|
id: this.trackingWebsiteId,
|
|
322
453
|
},
|
|
323
|
-
|
|
454
|
+
body,
|
|
324
455
|
},
|
|
325
456
|
}));
|
|
326
457
|
},
|
|
458
|
+
/**
|
|
459
|
+
* Generates JavaScript code for tracking custom events.
|
|
460
|
+
*
|
|
461
|
+
* @note This method is available only on the backend-side
|
|
462
|
+
*
|
|
463
|
+
*/
|
|
327
464
|
trackEventJsCode: async (data) => {
|
|
328
465
|
return this.trackBackendAction({
|
|
329
466
|
url: "/api/v1/websites/{id}/track-event",
|
|
@@ -331,6 +468,12 @@ export class Analytics extends BaseClient {
|
|
|
331
468
|
generateCode: true,
|
|
332
469
|
});
|
|
333
470
|
},
|
|
471
|
+
/**
|
|
472
|
+
* Generates JavaScript code for tracking page views.
|
|
473
|
+
*
|
|
474
|
+
* @note This method is available only on the backend-side
|
|
475
|
+
*
|
|
476
|
+
*/
|
|
334
477
|
trackPageViewJsCode: async (data) => {
|
|
335
478
|
return this.trackBackendAction({
|
|
336
479
|
url: "/api/v1/websites/{id}/track-page-view",
|
|
@@ -338,6 +481,12 @@ export class Analytics extends BaseClient {
|
|
|
338
481
|
generateCode: true,
|
|
339
482
|
});
|
|
340
483
|
},
|
|
484
|
+
/**
|
|
485
|
+
* Generates JavaScript code for tracking site searches.
|
|
486
|
+
*
|
|
487
|
+
* @note This method is available only on the backend-side
|
|
488
|
+
*
|
|
489
|
+
*/
|
|
341
490
|
trackSiteSearchJsCode: async (data) => {
|
|
342
491
|
return this.trackBackendAction({
|
|
343
492
|
url: "/api/v1/websites/{id}/track-search",
|
|
@@ -345,6 +494,12 @@ export class Analytics extends BaseClient {
|
|
|
345
494
|
generateCode: true,
|
|
346
495
|
});
|
|
347
496
|
},
|
|
497
|
+
/**
|
|
498
|
+
* Generates JavaScript code for tracking link clicks.
|
|
499
|
+
*
|
|
500
|
+
* @note This method is available only on the backend-side
|
|
501
|
+
*
|
|
502
|
+
*/
|
|
348
503
|
trackLinkJsCode: async (data) => {
|
|
349
504
|
return this.trackBackendAction({
|
|
350
505
|
url: "/api/v1/websites/{id}/track-link",
|
|
@@ -352,6 +507,12 @@ export class Analytics extends BaseClient {
|
|
|
352
507
|
generateCode: true,
|
|
353
508
|
});
|
|
354
509
|
},
|
|
510
|
+
/**
|
|
511
|
+
* Generates JavaScript code for tracking content impressions.
|
|
512
|
+
*
|
|
513
|
+
* @note This method is available only on the backend-side
|
|
514
|
+
*
|
|
515
|
+
*/
|
|
355
516
|
trackContentImpressionJsCode: async (data) => {
|
|
356
517
|
return this.trackBackendAction({
|
|
357
518
|
url: "/api/v1/websites/{id}/track-content-impression",
|
|
@@ -359,6 +520,12 @@ export class Analytics extends BaseClient {
|
|
|
359
520
|
generateCode: true,
|
|
360
521
|
});
|
|
361
522
|
},
|
|
523
|
+
/**
|
|
524
|
+
* Generates JavaScript code for tracking content interactions.
|
|
525
|
+
*
|
|
526
|
+
* @note This method is available only on the backend-side
|
|
527
|
+
*
|
|
528
|
+
*/
|
|
362
529
|
trackContentInteractionJsCode: async (data) => {
|
|
363
530
|
return this.trackBackendAction({
|
|
364
531
|
url: "/api/v1/websites/{id}/track-content-interaction",
|
|
@@ -368,6 +535,10 @@ export class Analytics extends BaseClient {
|
|
|
368
535
|
},
|
|
369
536
|
};
|
|
370
537
|
track = {
|
|
538
|
+
/**
|
|
539
|
+
* Tracks custom event
|
|
540
|
+
* @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.
|
|
541
|
+
*/
|
|
371
542
|
event: async (data) => {
|
|
372
543
|
if (!isBrowser()) {
|
|
373
544
|
return this.trackBackendAction({
|
|
@@ -376,15 +547,20 @@ export class Analytics extends BaseClient {
|
|
|
376
547
|
generateCode: false,
|
|
377
548
|
});
|
|
378
549
|
}
|
|
379
|
-
const { event,
|
|
550
|
+
const { event, siteIds, contextOverride } = data;
|
|
380
551
|
const eventArguments = [
|
|
381
552
|
event.category,
|
|
382
553
|
event.action,
|
|
383
554
|
...(event.name ? [event.name] : []),
|
|
384
555
|
...(event.value ? [event.value] : []),
|
|
385
556
|
];
|
|
386
|
-
return this.trackPush("trackEvent", eventArguments,
|
|
557
|
+
return this.trackPush("trackEvent", eventArguments, siteIds, contextOverride);
|
|
387
558
|
},
|
|
559
|
+
/**
|
|
560
|
+
* Tracks page views
|
|
561
|
+
* @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.
|
|
562
|
+
|
|
563
|
+
*/
|
|
388
564
|
pageView: async (data) => {
|
|
389
565
|
if (!isBrowser()) {
|
|
390
566
|
return this.trackBackendAction({
|
|
@@ -393,10 +569,15 @@ export class Analytics extends BaseClient {
|
|
|
393
569
|
generateCode: false,
|
|
394
570
|
});
|
|
395
571
|
}
|
|
396
|
-
const { event,
|
|
397
|
-
const eventArguments = [event.
|
|
398
|
-
return this.trackPush("trackPageView", eventArguments,
|
|
572
|
+
const { event, siteIds, contextOverride } = data;
|
|
573
|
+
const eventArguments = [event.title];
|
|
574
|
+
return this.trackPush("trackPageView", eventArguments, siteIds, contextOverride);
|
|
399
575
|
},
|
|
576
|
+
/**
|
|
577
|
+
* Tracks site search
|
|
578
|
+
* @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.
|
|
579
|
+
|
|
580
|
+
*/
|
|
400
581
|
siteSearch: async (data) => {
|
|
401
582
|
if (!isBrowser()) {
|
|
402
583
|
return this.trackBackendAction({
|
|
@@ -405,14 +586,21 @@ export class Analytics extends BaseClient {
|
|
|
405
586
|
generateCode: false,
|
|
406
587
|
});
|
|
407
588
|
}
|
|
408
|
-
const { event,
|
|
589
|
+
const { event, siteIds, contextOverride } = data;
|
|
409
590
|
const eventArguments = [
|
|
410
|
-
event.
|
|
411
|
-
...(event.
|
|
412
|
-
...(event.
|
|
591
|
+
event.query,
|
|
592
|
+
...(event.category ? [event.category] : []),
|
|
593
|
+
...(event.count ? [event.count] : []),
|
|
413
594
|
];
|
|
414
|
-
return this.trackPush("trackSiteSearch", eventArguments,
|
|
595
|
+
return this.trackPush("trackSiteSearch", eventArguments, siteIds, contextOverride);
|
|
415
596
|
},
|
|
597
|
+
/**
|
|
598
|
+
* Tracks link click
|
|
599
|
+
*
|
|
600
|
+
* if linkType is 'link' the link is an outgoing link, if 'download' the link is a download
|
|
601
|
+
* @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.
|
|
602
|
+
|
|
603
|
+
*/
|
|
416
604
|
link: async (data) => {
|
|
417
605
|
if (!isBrowser()) {
|
|
418
606
|
return this.trackBackendAction({
|
|
@@ -421,10 +609,15 @@ export class Analytics extends BaseClient {
|
|
|
421
609
|
generateCode: false,
|
|
422
610
|
});
|
|
423
611
|
}
|
|
424
|
-
const { event,
|
|
612
|
+
const { event, siteIds, contextOverride } = data;
|
|
425
613
|
const eventArguments = [event.link, event.linkType];
|
|
426
|
-
return this.trackPush("trackLink", eventArguments,
|
|
614
|
+
return this.trackPush("trackLink", eventArguments, siteIds, contextOverride);
|
|
427
615
|
},
|
|
616
|
+
/**
|
|
617
|
+
* Tracks content impression
|
|
618
|
+
* @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.
|
|
619
|
+
|
|
620
|
+
*/
|
|
428
621
|
contentImpression: async (data) => {
|
|
429
622
|
if (!isBrowser()) {
|
|
430
623
|
return this.trackBackendAction({
|
|
@@ -433,14 +626,15 @@ export class Analytics extends BaseClient {
|
|
|
433
626
|
generateCode: false,
|
|
434
627
|
});
|
|
435
628
|
}
|
|
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);
|
|
629
|
+
const { event, siteIds, contextOverride } = data;
|
|
630
|
+
const eventArguments = [event.name, event.piece, event.target];
|
|
631
|
+
return this.trackPush("trackContentImpression", eventArguments, siteIds, contextOverride);
|
|
443
632
|
},
|
|
633
|
+
/**
|
|
634
|
+
* Tracks content interaction
|
|
635
|
+
* @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.
|
|
636
|
+
|
|
637
|
+
*/
|
|
444
638
|
contentInteraction: async (data) => {
|
|
445
639
|
if (!isBrowser()) {
|
|
446
640
|
return this.trackBackendAction({
|
|
@@ -449,14 +643,14 @@ export class Analytics extends BaseClient {
|
|
|
449
643
|
generateCode: false,
|
|
450
644
|
});
|
|
451
645
|
}
|
|
452
|
-
const { event,
|
|
646
|
+
const { event, siteIds, contextOverride } = data;
|
|
453
647
|
const eventArguments = [
|
|
454
|
-
event.
|
|
455
|
-
event.
|
|
456
|
-
event.
|
|
457
|
-
event.
|
|
648
|
+
event.interaction,
|
|
649
|
+
event.name,
|
|
650
|
+
event.piece,
|
|
651
|
+
event.target,
|
|
458
652
|
];
|
|
459
|
-
return this.trackPush("trackContentInteraction", eventArguments,
|
|
653
|
+
return this.trackPush("trackContentInteraction", eventArguments, siteIds, contextOverride);
|
|
460
654
|
},
|
|
461
655
|
};
|
|
462
656
|
}
|