diff --git a/src/__tests__/elblayer.test.ts b/src/__tests__/elblayer.test.ts index be7f7255..cd068bb0 100644 --- a/src/__tests__/elblayer.test.ts +++ b/src/__tests__/elblayer.test.ts @@ -185,7 +185,7 @@ describe('ElbLayer', () => { allowed: true, consent: {}, count: expect.any(Number), - destinations: expect.any(Array), + destinations: expect.any(Object), elbLayer: w.elbLayer, globals: {}, group: expect.any(String), diff --git a/src/elbwalker.ts b/src/elbwalker.ts index 9a8ef297..14396384 100644 --- a/src/elbwalker.ts +++ b/src/elbwalker.ts @@ -65,7 +65,9 @@ function Elbwalker( pushToDestination(instance, destination, pushEvent); }); - instance.config.destinations.push(destination); + // @TODO use the destination id as key if given and set it to the config + // @TODO getId could be duplicate, use incremental id instead + instance.config.destinations[getId(6)] = destination; } function addHook( @@ -194,7 +196,7 @@ function Elbwalker( // Event counter for each run count: values.count || current.count || 0, // Destination list - destinations: values.destinations || current.destinations || [], + destinations: values.destinations || current.destinations || {}, // Async access api in window as array elbLayer: values.elbLayer || @@ -384,7 +386,7 @@ function Elbwalker( // Add event to internal queue config.queue.push(pushEvent); - config.destinations.forEach((destination) => { + Object.values(config.destinations).forEach((destination) => { pushToDestination(instance, destination, pushEvent); }); } @@ -477,7 +479,7 @@ function Elbwalker( instance.config.queue = []; // Reset all destination queues - instance.config.destinations.forEach((destination) => { + Object.values(instance.config.destinations).forEach((destination) => { destination.queue = []; }); @@ -507,7 +509,7 @@ function Elbwalker( }); if (runQueue) { - config.destinations.forEach((destination) => { + Object.values(config.destinations).forEach((destination) => { let queue = destination.queue || []; // Try to push and remove successful ones from queue diff --git a/src/types/destinations.d.ts b/src/types/destinations.d.ts index 28d21a16..26406052 100644 --- a/src/types/destinations.d.ts +++ b/src/types/destinations.d.ts @@ -16,6 +16,7 @@ export namespace WebDestination { interface Config { consent?: IElbwalker.Consent; // Required consent states to init and push events custom?: Custom; // Arbitrary but protected configurations for custom enhancements + id?: string; // A unique key for the destination init?: boolean; // If the destination has been initialized by calling the init method loadScript?: boolean; // If an additional script to work should be loaded mapping?: Mapping; // A map to handle events individually diff --git a/src/types/elbwalker.d.ts b/src/types/elbwalker.d.ts index b83564ed..6f0216ac 100644 --- a/src/types/elbwalker.d.ts +++ b/src/types/elbwalker.d.ts @@ -61,7 +61,7 @@ export namespace IElbwalker { allowed: boolean; consent: Consent; count: number; - destinations: Array; + destinations: Destinations; // @TODO custom state support elbLayer: ElbLayer; globals: Walker.Properties; @@ -77,6 +77,10 @@ export namespace IElbwalker { default?: boolean; } + interface Destinations { + [name: string]: WebDestination.Function; + } + type Events = Array; interface Event { event: string;