Skip to content

Commit 5ed20fe

Browse files
author
Paul Boocock
committed
Allow plugins to be dynamically loaded when using tracker (close #918)
1 parent c374d19 commit 5ed20fe

File tree

15 files changed

+493
-255
lines changed

15 files changed

+493
-255
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Allow plugins to be dynamically loaded when using tracker (#918)",
5+
"type": "none",
6+
"packageName": "@snowplow/browser-plugin-browser-features"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-plugin-browser-features",
10+
"email": "paul@snowplowanalytics.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Allow plugins to be dynamically loaded when using tracker (#918)",
5+
"type": "none",
6+
"packageName": "@snowplow/browser-plugin-timezone"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-plugin-timezone",
10+
"email": "paul@snowplowanalytics.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Allow plugins to be dynamically loaded when using tracker (#918)",
5+
"type": "none",
6+
"packageName": "@snowplow/browser-tracker-core"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker-core",
10+
"email": "paul@snowplowanalytics.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Allow plugins to be dynamically loaded when using tracker (#918)",
5+
"type": "none",
6+
"packageName": "@snowplow/browser-tracker"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker",
10+
"email": "paul@snowplowanalytics.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Allow plugins to be dynamically loaded when using tracker (#918)",
5+
"type": "none",
6+
"packageName": "@snowplow/javascript-tracker"
7+
}
8+
],
9+
"packageName": "@snowplow/javascript-tracker",
10+
"email": "paul@snowplowanalytics.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Allow plugins to be dynamically loaded when using tracker (#918)",
5+
"type": "none",
6+
"packageName": "@snowplow/tracker-core"
7+
}
8+
],
9+
"packageName": "@snowplow/tracker-core",
10+
"email": "paul@snowplowanalytics.com"
11+
}

libraries/browser-tracker-core/src/tracker/index.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import {
7171
EnableAnonymousTrackingConfiguration,
7272
FlushBufferConfiguration,
7373
} from './types';
74+
import { BrowserPlugin } from '../plugins';
7475

7576
/** Repesents an instance of an activity tracking configuration */
7677
type ActivityConfig = {
@@ -118,15 +119,15 @@ export function Tracker(
118119
version: string,
119120
endpoint: string,
120121
sharedState: SharedState,
121-
trackerConfiguration?: TrackerConfiguration
122+
trackerConfiguration: TrackerConfiguration = {}
122123
): BrowserTracker {
123124
const newTracker = (
124125
trackerId: string,
125126
namespace: string,
126127
version: string,
127128
endpoint: string,
128129
state: SharedState,
129-
trackerConfiguration: TrackerConfiguration = {}
130+
trackerConfiguration: TrackerConfiguration
130131
) => {
131132
/************************************************************
132133
* Private members
@@ -152,15 +153,19 @@ export function Tracker(
152153
getAnonymousTracking = (config: TrackerConfiguration) => !!config.anonymousTracking;
153154

154155
// Get all injected plugins
155-
let plugins = trackerConfiguration.plugins ?? [];
156+
trackerConfiguration.plugins = trackerConfiguration.plugins ?? [];
156157
if (trackerConfiguration?.contexts?.webPage ?? true) {
157-
plugins.push(getWebPagePlugin()); // Defaults to including the Web Page context
158+
trackerConfiguration.plugins.push(getWebPagePlugin()); // Defaults to including the Web Page context
158159
}
159160

160161
let // Tracker core
161-
core = trackerCore(trackerConfiguration.encodeBase64 ?? true, plugins, function (payloadBuilder) {
162-
addBrowserData(payloadBuilder);
163-
sendRequest(payloadBuilder);
162+
core = trackerCore({
163+
base64: trackerConfiguration.encodeBase64,
164+
corePlugins: trackerConfiguration.plugins,
165+
callback: function (payloadBuilder) {
166+
addBrowserData(payloadBuilder);
167+
sendRequest(payloadBuilder);
168+
},
164169
}),
165170
// Aliases
166171
documentAlias = document,
@@ -1224,22 +1229,21 @@ export function Tracker(
12241229
id: trackerId,
12251230
core: core,
12261231
sharedState: state,
1227-
plugins: plugins,
12281232
};
12291233
};
12301234

12311235
// Initialise the tracker
1232-
const { plugins, ...tracker } = newTracker(
1233-
trackerId,
1234-
namespace,
1235-
version,
1236-
endpoint,
1237-
sharedState,
1238-
trackerConfiguration
1239-
);
1236+
const partialTracker = newTracker(trackerId, namespace, version, endpoint, sharedState, trackerConfiguration),
1237+
tracker = {
1238+
...partialTracker,
1239+
addPlugin: (plugin: BrowserPlugin) => {
1240+
trackerConfiguration.plugins?.push(plugin);
1241+
plugin.activateBrowserPlugin?.(tracker);
1242+
},
1243+
};
12401244

12411245
// Initialise each plugin with the tracker
1242-
plugins.forEach((p) => {
1246+
trackerConfiguration.plugins?.forEach((p) => {
12431247
p.activateBrowserPlugin?.(tracker);
12441248
});
12451249

libraries/browser-tracker-core/src/tracker/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,4 +482,6 @@ export interface BrowserTracker {
482482
* Clears all cookies and local storage containing user and session identifiers
483483
*/
484484
clearUserData: () => void;
485+
486+
addPlugin: (plugin: BrowserPlugin) => void;
485487
}

0 commit comments

Comments
 (0)