Skip to content

Commit

Permalink
add log to constructor, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Jaffee committed Feb 14, 2024
1 parent ef2b390 commit 4416d41
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 91 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ pagesAnalytics.report({

### Debugging

#### Debug Mode

The SDK has a debug mode which can be activated by setting the `debug` property in your AnalyticsConfig to `true`.
This will cause more useful information to be printed out in the logs. In addition, calling report will not actually send an event to the Yext Events API. Instead it will print the request to console.

#### fetch + keepalive

We use `fetch()` + `keepalive` by default in [supported browsers](https://developer.mozilla.org/en-US/docs/Web/API/fetch) to make debugging easier. For browsers like Firefox that do not support `keepalive`, [we use the Beacon API](https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API). Users can set `forceFetch: true` in their config, which will make these browsers use `fetch()` instead of the `Beacon API`. Be warned, since `forceFetch` uses `fetch()` without `keepalive`, **requests in progress for browsers like FireFox will be canceled if the page is unloaded**.

## Module support
Expand Down
22 changes: 6 additions & 16 deletions src/AnalyticsEventReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,26 @@ export class AnalyticsEventReporter implements AnalyticsEventService {
}
this.config = config;
this.payload = payload;
console.log('[DEBUG] AnalyticsConfig object:\n', this.config);
console.log(
'[DEBUG] EventPayload object following call to constructor:\n',
this.payload
);
}

with(payload: EventPayload): AnalyticsEventService {
const currentPayload =
this.payload === undefined ? payload : merge(this.payload, payload);
if (this.config.debug) {
console.log('[DEBUG] AnalyticsConfig Object:\n', this.config);
console.log(
'[DEBUG] Payload following call to `with`:\n',
'[DEBUG] EventPayload object following call to `with()`:\n',
currentPayload
);
}
return new AnalyticsEventReporter(this.config, currentPayload);
}

public async report(newPayload?: EventPayload): Promise<string> {
if (this.config.debug) {
console.log(
`[DEBUG] Merging the following payloads:
Original Payload from call to \`with\`:
${this.payload ? JSON.stringify(this.payload) : '{}'}
New Payload from call to \`report:\`
${newPayload ? JSON.stringify(newPayload) : '{}'}`
);
}

const finalPayload: EventPayload = merge(
this.payload ?? {},
newPayload ?? {}
Expand Down
151 changes: 76 additions & 75 deletions test-site/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { analytics } from '@yext/analytics';
const analyticsProvider = analytics({
authorizationType: 'apiKey',
authorization: process.env.YEXT_API_KEY,
sessionTrackingEnabled: false
sessionTrackingEnabled: false,
debug: true
}).with({
action: 'CHAT_LINK_CLICK',
pageUrl: 'http://www.yext-test-pageurl.com',
Expand Down Expand Up @@ -53,85 +54,85 @@ const analyticsProvider = analytics({
}
});

const analyticsProvideWithSessionTracking = analytics({
authorizationType: 'apiKey',
authorization: process.env.YEXT_API_KEY
}).with({
action: 'CHAT_LINK_CLICK',
pageUrl: 'http://www.yext-test-pageurl.com',
destinationUrl: 'http://www.yext-test-destinationurl.com',
referrerUrl: 'http://www.yext-test-referrerurl.com',
label: 'test-label',
timestamp: new Date(),
bot: false,
browserAgent: {
browser: 'test-browser',
browserVersion: 'test-browser-version',
os: 'test-os',
osVersion: 'test-os-version',
device: 'test-device',
deviceClass: 'test-device-class',
userAgent: 'test-user-agent'
},
clientSdk: {
sdk: 'version'
},
internalUser: false,
chat: {
botId: 'analytics-test-bot'
},
count: 1,
customTags: {
testcustomtag: 'testcustomtagvalue'
},
customValues: {
testcustomvalue: 1
},
entity: 'testEntityId',
ip: {
address: '0.0.0.0',
algorithm: 'HASH'
},
visitor: {
'test-id-method': 'visitor-test-id'
}
});
// const analyticsProvideWithSessionTracking = analytics({
// authorizationType: 'apiKey',
// authorization: process.env.YEXT_API_KEY
// }).with({
// action: 'CHAT_LINK_CLICK',
// pageUrl: 'http://www.yext-test-pageurl.com',
// destinationUrl: 'http://www.yext-test-destinationurl.com',
// referrerUrl: 'http://www.yext-test-referrerurl.com',
// label: 'test-label',
// timestamp: new Date(),
// bot: false,
// browserAgent: {
// browser: 'test-browser',
// browserVersion: 'test-browser-version',
// os: 'test-os',
// osVersion: 'test-os-version',
// device: 'test-device',
// deviceClass: 'test-device-class',
// userAgent: 'test-user-agent'
// },
// clientSdk: {
// sdk: 'version'
// },
// internalUser: false,
// chat: {
// botId: 'analytics-test-bot'
// },
// count: 1,
// customTags: {
// testcustomtag: 'testcustomtagvalue'
// },
// customValues: {
// testcustomvalue: 1
// },
// entity: 'testEntityId',
// ip: {
// address: '0.0.0.0',
// algorithm: 'HASH'
// },
// visitor: {
// 'test-id-method': 'visitor-test-id'
// }
// });

export function fireChatEvent() {
analyticsProvider.report();
}

export function fireSearchEvent() {
analyticsProvider.report({
action: 'PRODUCT',
chat: null,
search: {
searchId: '11111111-1111-1111-1111-111111111111',
queryId: '11111111-1111-1111-1111-111111111111',
verticalKey: 'test-vertical-key',
isDirectAnswer: false,
versionLabel: 'PRODUCTION',
versionNumber: 1,
experienceKey: 'test-experience-key'
}
});
}
// export function fireSearchEvent() {
// analyticsProvider.report({
// action: 'PRODUCT',
// chat: null,
// search: {
// searchId: '11111111-1111-1111-1111-111111111111',
// queryId: '11111111-1111-1111-1111-111111111111',
// verticalKey: 'test-vertical-key',
// isDirectAnswer: false,
// versionLabel: 'PRODUCTION',
// versionNumber: 1,
// experienceKey: 'test-experience-key'
// }
// });
// }

export function firePagesEvent() {
analyticsProvider.report({
action: 'WEBSITE',
chat: null,
pages: {
siteUid: 123456789,
template: 'test-template'
}
});
}
// export function firePagesEvent() {
// analyticsProvider.report({
// action: 'WEBSITE',
// chat: null,
// pages: {
// siteUid: 123456789,
// template: 'test-template'
// }
// });
// }

export function fireCallToActionEvent() {
analyticsProvider.report({ action: 'CTA_CLICK' });
}
// export function fireCallToActionEvent() {
// analyticsProvider.report({ action: 'CTA_CLICK' });
// }

export function fireEventWithSessionTracking() {
analyticsProvideWithSessionTracking.report();
}
// export function fireEventWithSessionTracking() {
// analyticsProvideWithSessionTracking.report();
// }

0 comments on commit 4416d41

Please sign in to comment.