Skip to content

Commit c2656d7

Browse files
author
Paul Boocock
committed
Make addPlugin() consistent across tracker types (close #938)
1 parent 7344589 commit c2656d7

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ import {
7070
DisableAnonymousTrackingConfiguration,
7171
EnableAnonymousTrackingConfiguration,
7272
FlushBufferConfiguration,
73+
BrowserPluginConfiguration,
7374
} from './types';
74-
import { BrowserPlugin } from '../plugins';
7575

7676
/** Repesents an instance of an activity tracking configuration */
7777
type ActivityConfig = {
@@ -1233,9 +1233,9 @@ export function Tracker(
12331233
const partialTracker = newTracker(trackerId, namespace, version, endpoint, sharedState, trackerConfiguration),
12341234
tracker = {
12351235
...partialTracker,
1236-
addPlugin: (plugin: BrowserPlugin) => {
1237-
tracker.core.addPlugin(plugin);
1238-
plugin.activateBrowserPlugin?.(tracker);
1236+
addPlugin: (configuration: BrowserPluginConfiguration) => {
1237+
tracker.core.addPlugin(configuration);
1238+
configuration.plugin.activateBrowserPlugin?.(tracker);
12391239
},
12401240
};
12411241

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929
*/
3030

3131
import { BrowserPlugin } from '../plugins';
32-
import { CommonEventProperties, SelfDescribingJson, TrackerCore } from '@snowplow/tracker-core';
32+
import {
33+
CommonEventProperties,
34+
SelfDescribingJson,
35+
TrackerCore,
36+
CorePluginConfiguration,
37+
} from '@snowplow/tracker-core';
3338
import { SharedState } from '../state';
3439

3540
/* Configuration for Anonymous Tracking */
@@ -241,18 +246,38 @@ export interface PageViewEvent {
241246
contextCallback?: (() => Array<SelfDescribingJson>) | null;
242247
}
243248

249+
/**
250+
* The configuration that can be changed when disabling anonymous tracking
251+
*/
244252
export interface DisableAnonymousTrackingConfiguration {
253+
/* Available configurations for different storage strategies */
245254
stateStorageStrategy?: StateStorageStrategy;
246255
}
247256

257+
/**
258+
* The configuration that can be changed when enabling anonymous tracking
259+
*/
248260
export interface EnableAnonymousTrackingConfiguration {
261+
/* Configuration for Anonymous Tracking */
249262
options?: AnonymousTrackingOptions;
250263
}
251264

265+
/**
266+
* The configuration that can be changed when flushing the buffer
267+
*/
252268
export interface FlushBufferConfiguration {
269+
/* The size of the buffer after this flush */
253270
newBufferSize?: number;
254271
}
255272

273+
/**
274+
* The configuration of the plugin to add
275+
*/
276+
export interface BrowserPluginConfiguration extends CorePluginConfiguration {
277+
/* The plugin to add */
278+
plugin: BrowserPlugin;
279+
}
280+
256281
/**
257282
* The Browser Tracker
258283
*/
@@ -485,5 +510,9 @@ export interface BrowserTracker {
485510
*/
486511
clearUserData: () => void;
487512

488-
addPlugin: (plugin: BrowserPlugin) => void;
513+
/**
514+
* Add a plugin into the plugin collection after Tracker has already been initialised
515+
* @param configuration The plugin to add
516+
*/
517+
addPlugin: (configuration: BrowserPluginConfiguration) => void;
489518
}

libraries/tracker-core/src/core.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,31 @@ export interface TrackerCore {
288288

289289
/**
290290
* Add a plugin into the plugin collection after Core has already been initialised
291-
* @param plugin The plugin to add
291+
* @param configuration The plugin to add
292292
*/
293-
addPlugin(plugin: CorePlugin): void;
293+
addPlugin(configuration: CorePluginConfiguration): void;
294294
}
295295

296+
/**
297+
* The configuration object for the tracker core library
298+
*/
296299
export interface CoreConfiguration {
300+
/* Should payloads be base64 encoded when built */
297301
base64?: boolean;
302+
/* A list of all the plugins to include at load */
298303
corePlugins?: Array<CorePlugin>;
304+
/* The callback which will fire each time `track()` is called */
299305
callback?: (PayloadData: PayloadBuilder) => void;
300306
}
301307

308+
/**
309+
* The configuration of the plugin to add
310+
*/
311+
export interface CorePluginConfiguration {
312+
/* The plugin to add */
313+
plugin: CorePlugin;
314+
}
315+
302316
/**
303317
* Create a tracker core object
304318
*
@@ -513,7 +527,8 @@ export function trackerCore(configuration: CoreConfiguration = {}): TrackerCore
513527
partialCore = newCore(base64 ?? true, plugins, callback),
514528
core = {
515529
...partialCore,
516-
addPlugin: (plugin: CorePlugin) => {
530+
addPlugin: (configuration: CorePluginConfiguration) => {
531+
const { plugin } = configuration;
517532
plugins.push(plugin);
518533
plugin.logger?.(LOG);
519534
plugin.activateCorePlugin?.(core);

libraries/tracker-core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ export * from './contexts';
3939
export * from './plugins';
4040
export * from './payload';
4141
export * from './core';
42+
export * from './logger';
4243

4344
export { version } from '../package.json';
44-
export { LOG, LOG_LEVEL, Logger } from './logger';

trackers/browser-tracker/src/api.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import {
3232
ActivityTrackingConfiguration,
3333
ActivityTrackingConfigurationCallback,
34-
BrowserPlugin,
34+
BrowserPluginConfiguration,
3535
DisableAnonymousTrackingConfiguration,
3636
EnableAnonymousTrackingConfiguration,
3737
FlushBufferConfiguration,
@@ -448,8 +448,14 @@ export function clearUserData(trackers?: Array<string>) {
448448
});
449449
}
450450

451-
export function addPlugin(configuration: { plugin: BrowserPlugin }, trackers?: Array<string>) {
451+
/**
452+
* Add a plugin into the plugin collection after trackers have already been initialised
453+
*
454+
* @param configuration The plugin to add
455+
* @param trackers The tracker identifiers which the plugin will be added to
456+
*/
457+
export function addPlugin(configuration: BrowserPluginConfiguration, trackers?: Array<string>) {
452458
dispatchToTrackers(trackers, (t) => {
453-
t.addPlugin(configuration.plugin);
459+
t.addPlugin(configuration);
454460
});
455461
}

0 commit comments

Comments
 (0)