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

Get user email from Navattic event data properties #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unifygtm/intent-client",
"version": "1.1.2",
"version": "1.1.3",
"description": "JavaScript client for interacting with the Unify Intent API in the browser.",
"keywords": [
"unify",
Expand Down
1 change: 1 addition & 0 deletions src/client/agent/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export const DEFAULT_FORM_EVENT_TYPES: DefaultEventType[] = [
];

export const NAVATTIC_USER_EMAIL_KEY = 'user.email';
export const NAVATTIC_USER_EMAIL_PROPERTY = 'email';
29 changes: 29 additions & 0 deletions src/client/agent/types/navattic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,25 @@ export enum NavatticAttributeSource {
QUERY_PARAMS = 'QUERY_PARAMS',
SHARE_LINK = 'SHARE_LINK',
ENRICHMENT = 'ENRICHMENT',
REDUCER = 'REDUCER',
OTHER = 'OTHER',
}

/**
* Known object types for Navattic event data.
*/
export enum NavatticObject {
COMPANY_ACCOUNT = 'COMPANY_ACCOUNT',
END_USER = 'END_USER',
}

/**
* Known capture methods for Navattic event data.
*/
export enum NavatticCaptureMethod {
DEMO = 'DEMO',
}

export interface NavatticProject {
/**
* ID of the project.
Expand Down Expand Up @@ -87,6 +103,14 @@ export interface NavatticTask {
title: string;
}

export interface NavatticEventDataProperty {
captureMethod: NavatticCaptureMethod;
object: NavatticObject;
source: NavatticAttributeSource;
name: string;
value: string;
}

export interface NavatticClientSideMetadata {
browser: string;
browser_version: string;
Expand Down Expand Up @@ -170,6 +194,11 @@ export interface BaseNavatticEventData {
* by the source they come from, e.g. a form-fill.
*/
eventAttributes: Record<NavatticAttributeSource, { [key: string]: any }>;

/**
* Data properties for the current end user, company account, etc.
*/
properties?: NavatticEventDataProperty[];
}

export type NavatticNavigateEventData = BaseNavatticEventData & {
Expand Down
18 changes: 17 additions & 1 deletion src/client/agent/unify-intent-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import {
DEFAULT_FORMS_IFRAME_ORIGIN,
NAVATTIC_IFRAME_ORIGIN,
NAVATTIC_USER_EMAIL_KEY,
NAVATTIC_USER_EMAIL_PROPERTY,
} from './constants';
import { DefaultEventData } from './types/default';
import { NavatticEventData, NavatticEventType } from './types/navattic';
import {
NavatticEventData,
NavatticEventType,
NavatticObject,
} from './types/navattic';
import { isDefaultFormEventData } from './utils';

/**
Expand Down Expand Up @@ -277,6 +282,17 @@ export class UnifyIntentAgent {
this.maybeIdentifyInputEmail(email);
}
}
} else {
const eventDataProperties = event.data.properties ?? [];
const endUserEmailProperty = eventDataProperties.find(
({ object, name }) =>
object === NavatticObject.END_USER &&
name === NAVATTIC_USER_EMAIL_PROPERTY,
);

if (endUserEmailProperty) {
this.maybeIdentifyInputEmail(endUserEmailProperty.value);
}
}
};

Expand Down
78 changes: 69 additions & 9 deletions src/tests/client/agent/unify-intent-agent.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import {
DEFAULT_FORMS_IFRAME_ORIGIN,
NAVATTIC_IFRAME_ORIGIN,
NAVATTIC_USER_EMAIL_KEY,
NAVATTIC_USER_EMAIL_PROPERTY,
} from '../../../client/agent/constants';
import { DefaultEventType } from '../../../client/agent/types/default';
import {
NavatticAttributeSource,
NavatticCaptureMethod,
NavatticEventType,
NavatticObject,
} from '../../../client/agent/types/navattic';

const mockedPageActivity = mock(PageActivity.prototype);
Expand Down Expand Up @@ -376,17 +379,21 @@ describe('UnifyIntentAgent', () => {
});

describe('Navattic demo messages', () => {
let navatticDemoEvent: MessageEventInit = {
origin: NAVATTIC_IFRAME_ORIGIN,
data: {
type: NavatticEventType.IDENTIFY_USER,
eventAttributes: {
[NavatticAttributeSource.FORM]: {
[NAVATTIC_USER_EMAIL_KEY]: 'solomon@unifygtm.com',
let navatticDemoEvent: MessageEventInit;

beforeEach(() => {
navatticDemoEvent = {
origin: NAVATTIC_IFRAME_ORIGIN,
data: {
type: NavatticEventType.IDENTIFY_USER,
eventAttributes: {
[NavatticAttributeSource.FORM]: {
[NAVATTIC_USER_EMAIL_KEY]: 'solomon@unifygtm.com',
},
},
},
},
};
};
});

it('does not log an identify event without email from the event data', () => {
const agent = new UnifyIntentAgent(mockContext);
Expand Down Expand Up @@ -463,6 +470,59 @@ describe('UnifyIntentAgent', () => {
agent.__getSubmittedEmails().entries().next().value[0],
).toEqual('solomon-enrichment@unifygtm.com');
});

describe('other event types', () => {
beforeEach(() => {
navatticDemoEvent = {
origin: NAVATTIC_IFRAME_ORIGIN,
data: {
type: NavatticEventType.VIEW_STEP,
properties: [],
},
};
});

it('logs an identify event when email in properties', () => {
const agent = new UnifyIntentAgent(mockContext);
agent.startAutoIdentify();

navatticDemoEvent.data.properties = [
{
captureMethod: NavatticCaptureMethod.DEMO,
object: NavatticObject.END_USER,
source: NavatticAttributeSource.REDUCER,
name: NAVATTIC_USER_EMAIL_PROPERTY,
value: 'solomon@unifygtm.com',
},
];
window.dispatchEvent(
new MessageEvent('message', navatticDemoEvent),
);

agent.stopAutoIdentify();

expect(mockedIdentifyActivity.track).toHaveBeenCalledTimes(1);
expect(agent.__getSubmittedEmails().size).toEqual(1);
expect(
agent.__getSubmittedEmails().entries().next().value[0],
).toEqual('solomon@unifygtm.com');
});

it('does not log an identify event when email not in properties', () => {
const agent = new UnifyIntentAgent(mockContext);
agent.startAutoIdentify();

navatticDemoEvent.data.properties = [];
window.dispatchEvent(
new MessageEvent('message', navatticDemoEvent),
);

agent.stopAutoIdentify();

expect(mockedIdentifyActivity.track).toHaveBeenCalledTimes(0);
expect(agent.__getSubmittedEmails().size).toEqual(0);
});
});
});
});
});
Expand Down