Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PPDSC-2322): add tealiumTrack to instrumentation #352

Merged
merged 10 commits into from
Sep 9, 2022
8 changes: 7 additions & 1 deletion site/components/component-api/component-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ export const ComponentAPI: React.FC<ComponentAPIProps> = ({components}) => (
{tabs.length > 1 && (
<Tabs size="medium">
{tabs.map(({label, tabSummary, content}) => (
<Tab label={label} overrides={tabOverrides} key={label}>
<Tab
label={label}
overrides={tabOverrides}
key={label}
eventContext={{component: title, tabName: label}}
eventOriginator="component api"
>
Xin00163 marked this conversation as resolved.
Show resolved Hide resolved
{tabSummary && <ContentText>{tabSummary}</ContentText>}
{content}
</Tab>
Expand Down
1 change: 1 addition & 0 deletions site/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export default class MyApp extends App<Props, State> {
const handlers = [
instrumentationHandlers.createConsoleHandler(),
instrumentationHandlers.createTealiumHandler(),
instrumentationHandlers.createTealiumTrackHandler(),
];

return (
Expand Down
12 changes: 5 additions & 7 deletions site/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,11 @@ export default class MyDocument extends Document {
}
`}
/>
{isSiteEnvProduction && (
<Tealium
accountId="newsinternational"
profileId="thetimes.newskit"
env={isSiteEnvProduction ? 'prod' : 'dev'}
/>
)}
<Tealium
accountId="newsinternational"
profileId="thetimes.newskit"
env="dev"
/>
<Main />
<NextScript />
</body>
Expand Down
1 change: 1 addition & 0 deletions site/pages/components/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ const TooltipComponent = (layoutProps: LayoutProps) => (
componentAPI={{
components: [
{
title: 'Tooltip',
Xin00163 marked this conversation as resolved.
Show resolved Hide resolved
propsSummary:
'The Tooltip has a range of props that can be used to define an appropriate experience for different use cases.',
overridesSummary:
Expand Down
1 change: 1 addition & 0 deletions site/pages/getting-started/code/instrumentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ const InstrumentationSetup = (layoutProps: LayoutProps) => (
const handlers = [
instrumentationHandlers.createConsoleHandler(),
instrumentationHandlers.createTealiumHandler(),
instrumentationHandlers.createTealiumTrackHandler(),
];

const contextObject = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
const handlers = [
instrumentationHandlers.createConsoleHandler(),
instrumentationHandlers.createTealiumHandler(),
instrumentationHandlers.createTealiumTrackHandler(),
];

const contextObject = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,12 @@ exports[`ComponentAPISection renders section as expected 1`] = `
\\"overrides\\": {
\\"typographyPreset\\": \\"utilityButton020\\",
\\"stylePreset\\": \\"componentPageTabs\\"
}
},
\\"eventContext\\": {
\\"component\\": \\"Component\\",
\\"tabName\\": \\"Props\\"
},
\\"eventOriginator\\": \\"component api\\"
}"
data-testid="Tab"
>
Expand Down Expand Up @@ -425,7 +430,12 @@ exports[`ComponentAPISection renders section as expected 1`] = `
\\"overrides\\": {
\\"typographyPreset\\": \\"utilityButton020\\",
\\"stylePreset\\": \\"componentPageTabs\\"
}
},
\\"eventContext\\": {
\\"component\\": \\"Component\\",
\\"tabName\\": \\"Overrides\\"
},
\\"eventOriginator\\": \\"component api\\"
}"
data-testid="Tab"
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @jest-environment node
*/
import {EventTrigger} from '../../types';
import {sendEventTrackingToTealium} from '../tealium-track';

const mockEvent = {
originator: 'link',
trigger: EventTrigger.Click,
context: {some: 'object', prop: 'context'},
};

describe('instrumentation event handler - tealiumTrack on server side (without extendedWindow)', () => {
test('returns null', () => {
const sendEventResult = sendEventTrackingToTealium(mockEvent);
expect(sendEventResult).toEqual(null);
});
});
57 changes: 57 additions & 0 deletions src/instrumentation/handlers/__tests__/tealium-track.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {EventTrigger} from '../../types';
import createTealiumTrackHandler, {ExtendedWindow} from '../tealium-track';

const extendedWindow: ExtendedWindow = (window as Window) as ExtendedWindow;

describe('instrumentation event handler - tealium track', () => {
const mockEvents = [
{
originator: 'link',
trigger: EventTrigger.Click,
context: {some: 'object 1', prop1: 'context'},
},
{
originator: 'link',
trigger: EventTrigger.Swipe,
context: {context: 'object 2', prop1: 'context'},
},
{
originator: 'link',
trigger: EventTrigger.Load,
context: {some: 'object 2', prop1: 'context'},
},
];

const events = [
{
originator: 'link',
trigger: EventTrigger.Load,
context: {context: 'object 2', prop1: 'context'},
},
{
originator: 'link',
trigger: EventTrigger.Click,
context: {context: 'object 2', prop1: 'context'},
},
];

beforeEach(() => {
(extendedWindow.tealiumTrack as any) = jest.fn();
});

test('triggers an event', () => {
const handler = createTealiumTrackHandler();
handler(events);

expect(extendedWindow.tealiumTrack).toBeCalledWith(events[0]);
});

test('triggers each event', () => {
const handler = createTealiumTrackHandler();
handler(mockEvents);

expect(extendedWindow.tealiumTrack).nthCalledWith(1, mockEvents[0]);
expect(extendedWindow.tealiumTrack).nthCalledWith(2, mockEvents[1]);
expect(extendedWindow.tealiumTrack).toBeCalledWith(mockEvents[2]);
});
});
2 changes: 2 additions & 0 deletions src/instrumentation/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import createConsoleHandler from './console';
import createTealiumHandler from './tealium';
import createTealiumTrackHandler from './tealium-track';

export const instrumentationHandlers = {
createConsoleHandler,
createTealiumHandler,
createTealiumTrackHandler,
};
23 changes: 23 additions & 0 deletions src/instrumentation/handlers/tealium-track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {EventHandler, InstrumentationEvent} from '../types';

export interface ExtendedWindow extends Window {
tealiumTrack(e: InstrumentationEvent): void;
}

const extendedWindow: ExtendedWindow | null =
typeof window !== 'undefined' ? ((window as Window) as ExtendedWindow) : null;

export function sendEventTrackingToTealium(e: InstrumentationEvent) {
if (extendedWindow) {
Xin00163 marked this conversation as resolved.
Show resolved Hide resolved
return extendedWindow.tealiumTrack(e);
}
return null;
}

const createTealiumTrackHandler = (): EventHandler => events =>
events.map(event => {
sendEventTrackingToTealium(event);
return event;
});

export default createTealiumTrackHandler;
3 changes: 2 additions & 1 deletion src/tabs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {DividerOverrides} from '../divider';
import {ScrollProps} from '../scroll';
import {Override} from '../utils/overrides';
import {LogicalProps} from '../utils/logical-properties';
import {EventData} from '../instrumentation';

export type TabAlign = 'start' | 'end' | 'center';
export type TabSize = 'small' | 'medium' | 'large';
Expand Down Expand Up @@ -86,6 +87,6 @@ export interface TabButtonProps extends CommonTabProps {
align?: TabAlign;
}

export interface TabProps extends CommonTabProps {
export interface TabProps extends CommonTabProps, EventData {
label: React.ReactNode;
}