Skip to content

Commit

Permalink
config init
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed May 21, 2024
1 parent e04ccd0 commit 48e3d5e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 60 deletions.
4 changes: 1 addition & 3 deletions packages/clients/walkerjs/src/__tests__/elblayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,7 @@ describe('ElbLayer', () => {
expect(walkerjs.config).toStrictEqual(expect.objectContaining(update)); // Partial test
expect(walkerjs.config).toStrictEqual(config); // Full test

update = { version: 2 };
elb('walker config', update);
expect(walkerjs.config).toStrictEqual(expect.objectContaining(update));
// @TODO Add more tests for other config properties

update = { pageview: false };
elb('walker config', update);
Expand Down
1 change: 1 addition & 0 deletions packages/clients/walkerjs/src/__tests__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Init', () => {
consent: { functional: true },
custom: { private: 'space' },
config: expect.objectContaining({
dataLayer: true,
globalsStatic: { static: 'global' },
pageview: true,
prefix: 'data-prefix',
Expand Down
86 changes: 31 additions & 55 deletions packages/clients/walkerjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ export function Walkerjs(
elbLayerInit(instance);

// Assign instance and/or elb to the window object
if (customConfig.elb)
(window as unknown as Record<string, unknown>)[customConfig.elb] = elb;
if (customConfig.instance)
(window as unknown as Record<string, unknown>)[customConfig.instance] =
if (instance.config.elb)
(window as unknown as Record<string, unknown>)[instance.config.elb] = elb;
if (instance.config.instance)
(window as unknown as Record<string, unknown>)[instance.config.instance] =
instance;

// Run on events for default consent states
onApply(instance, 'consent');

if (customConfig.dataLayer) {
if (instance.config.dataLayer) {
// Add a dataLayer destination
window.dataLayer = window.dataLayer || [];
const destination: WebDestination.Destination = {
Expand All @@ -64,7 +64,7 @@ export function Walkerjs(
}

// Automatically start running
if (customConfig.run) {
if (instance.config.run) {
ready(run, instance);
}

Expand Down Expand Up @@ -278,8 +278,6 @@ export function Walkerjs(
initConfig: WebClient.InitConfig,
instance: Partial<WebClient.Instance> = {},
): WebClient.State {
const currentConfig: Partial<WebClient.Config> = instance.config || {};

const defaultConfig: WebClient.Config = {
dataLayer: false, // Do not use dataLayer by default
elbLayer: window.elbLayer || (window.elbLayer = []), // Async access api in window as array
Expand All @@ -291,52 +289,30 @@ export function Walkerjs(
storage: false, // Do not use storage by default
},
sessionStatic: {}, // Static session data
tagging: initConfig.tagging || 0, // Helpful to differentiate the clients used setup version
globalsStatic: initConfig.globalsStatic || {}, // Static global properties
};

// If 'pageview' is explicitly provided in values, use it; otherwise, use current or default
const pageview =
'pageview' in initConfig
? !!initConfig.pageview
: currentConfig.pageview || defaultConfig.pageview;

// Value hierarchy: values > current > default
const config = {
...defaultConfig,
...currentConfig,
...initConfig,
pageview,
tagging: 0, // Helpful to differentiate the clients used setup version
globalsStatic: {}, // Static global properties
};

// Wait for explicit run command to start
const allowed = false;
const config: WebClient.Config = assign(
defaultConfig,
{
...(instance.config || {}), // current config
...initConfig, // new config
},
{ merge: false },
);

// Event counter for each run
const count = 0;
// Optional values
if (initConfig.elb) config.elb = initConfig.elb;
if (initConfig.instance) config.instance = initConfig.instance;

// Default mode enables both, auto run and dataLayer destination
// Process default mode to enable both auto-run and dataLayer destination
if (initConfig.default) {
initConfig.run = true;
initConfig.dataLayer = true;
config.run = true;
config.dataLayer = true;
}

// Random id to group events of a run
const group = '';

// Temporary event queue for all events of a run
const queue: WalkerOS.Events = [];

// The first round is a special one due to state changes
const round = 0;

// Session data
const session = undefined;

// Offset counter to calculate timing property
const timing = 0;

// Configurational values
// Extract remaining values from initConfig
const {
consent = {}, // Handle the consent states
custom = {}, // Custom state support
Expand All @@ -347,23 +323,23 @@ export function Walkerjs(
} = initConfig;

// Globals enhanced with the static globals from init and previous values
const globals = assign({}, config.globalsStatic);
const globals = { ...config.globalsStatic };

return {
allowed,
allowed: false, // Wait for explicit run command to start
config,
consent,
count,
count: 0, // Event counter for each run
custom,
destinations,
globals,
group,
group: '', // Random id to group events of a run
hooks,
on,
queue,
round,
session,
timing,
queue: [], // Temporary event queue for all events of a run
round: 0, // The first round is a special one due to state changes
session: undefined, // Session data
timing: 0, // Offset counter to calculate timing property
user,
};
}
Expand Down
18 changes: 16 additions & 2 deletions packages/utils/src/core/assign.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
export function assign<T>(target: T, source: Object = {}): T {
import { WalkerOS } from '@elbwalker/types';

interface Assign {
merge?: boolean;
}

export function assign<T>(
target: T,
source: WalkerOS.AnyObject = {},
options: Assign = { merge: true },
): T {
// Check for array properties to merge them before overriding
Object.entries(source).forEach(([key, sourceProp]) => {
const targetProp = target[key as keyof typeof target];

// Only merge arrays
if (Array.isArray(targetProp) && Array.isArray(sourceProp)) {
if (
options.merge &&
Array.isArray(targetProp) &&
Array.isArray(sourceProp)
) {
source[key as keyof typeof source] = sourceProp.reduce(
(acc, item) => {
// Remove duplicates
Expand Down

0 comments on commit 48e3d5e

Please sign in to comment.