Skip to content

Commit

Permalink
getAllEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Jun 6, 2024
1 parent 2510769 commit 1733e5d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/clients/walkerjs/src/__tests__/elblayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ describe('ElbLayer', () => {

const defaultInterface: WebClient.Instance = {
push: expect.any(Function),
getAllEvents: expect.any(Function),
getEvents: expect.any(Function),
sessionStart: expect.any(Function),
client: expect.any(String),
...defaultState,
Expand Down
12 changes: 11 additions & 1 deletion packages/clients/walkerjs/src/__tests__/walker.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEvents } from '../lib/walker';
import { getAllEvents, getEvents } from '../lib/walker';
import { Trigger } from '../lib/trigger';
import fs from 'fs';

Expand All @@ -11,6 +11,16 @@ describe('Walker', () => {
document.body.innerHTML = html;
});

test('getAllEvents', () => {
const events = getAllEvents();
expect(events.length).toBeGreaterThan(0);
expect(events[0]).toMatchObject({
entity: expect.any(String),
action: expect.any(String),
data: expect.any(Object),
});
});

test('Basic collection', () => {
expect(getEvents(getElem('basic'), Trigger.Load)).toMatchObject([
{
Expand Down
3 changes: 3 additions & 0 deletions packages/clients/walkerjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { run } from './lib/run';
import { createSessionStart } from './lib/session';
import { getState } from './lib/state';
import { elb, initGlobalTrigger, ready } from './lib/trigger';
import { getAllEvents, getEvents } from './lib/walker';

// Export types and elb
export * from './types';
Expand All @@ -22,6 +23,8 @@ export function Walkerjs(
...state,
// Placeholder functions to be overwritten with instance-reference
push: (() => {}) as unknown as WebClient.Elb,
getAllEvents,
getEvents,
sessionStart: (() => {}) as unknown as typeof sessionStart,
};

Expand Down
3 changes: 1 addition & 2 deletions packages/clients/walkerjs/src/lib/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ export function load(instance: WebClient.Instance) {
// Trigger static page view if enabled
if (pageview) {
const [data, context] = getPageViewData(prefix);
instance.config.elbLayer.push('page view', data, Trigger.Load, context);
// elb('page view', data, Trigger.Load, context);
elb('page view', data, Trigger.Load, context);
}

initScopeTrigger(instance);
Expand Down
20 changes: 20 additions & 0 deletions packages/clients/walkerjs/src/lib/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ export function getElbValues(
return values;
}

export function getAllEvents(
scope: Element = document.body,
prefix: string = Const.Commands.Prefix,
): Walker.Events {
let events: Walker.Events = [];
const action = Const.Commands.Action;

scope
.querySelectorAll(`[${getElbAttributeName(prefix, action, false)}]`)
.forEach((elem) => {
Object.keys(getElbValues(prefix, elem, action, false)).forEach(
(trigger) => {
events = events.concat(getEvents(elem, trigger, prefix));
},
);
});

return events;
}

export function getEvents(
target: Element,
trigger: Walker.Trigger,
Expand Down
10 changes: 8 additions & 2 deletions packages/clients/walkerjs/src/types/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ type WalkerEvent = Array<
>;

export interface Instance extends State, WalkerOS.Instance {
push: Elb;
sessionStart: (options?: SessionStartOptions) => void | SessionData;
client: string;
config: Config;
destinations: Destinations;
push: Elb;
getAllEvents: (scope: Element, prefix: string) => Walker.Events;
getEvents: (
target: Element,
trigger: Walker.Trigger,
prefix: string,
) => Walker.Events;
sessionStart: (options?: SessionStartOptions) => void | SessionData;
}

export interface State extends WalkerOS.State {
Expand Down

0 comments on commit 1733e5d

Please sign in to comment.