Skip to content
Merged
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
4 changes: 2 additions & 2 deletions docs/analytics.eventpayload.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export interface EventPayload
| [locale?](./analytics.eventpayload.locale.md) | | string | <p>_(Optional)_ The locale of the user who generated the event.</p><p>If not specified the system will automatically determine the locale from the autogenerated Accept-Language header.</p> |
| [location?](./analytics.eventpayload.location.md) | | Coordinates \| string | <p>_(Optional)_ The location information of the visitor for the event. Either a Coordinates object with both latitude and longitude or a string with the country of the visitor for the event, as a ISO 3166-1 alpha-2 country code. For more information see https://www.iso.org/iso-3166-country-codes.html.</p><p>If not specified the system will automatically determine all location information from the request's IP address, or the value of the <code>ip</code> property if specified.</p> |
| [pages?](./analytics.eventpayload.pages.md) | | { scope?: string; originalEventName?: string; siteUid?: number; template?: string; } | _(Optional)_ Fields specific to reporting Yext Pages Analytics Events |
| [pageUrl?](./analytics.eventpayload.pageurl.md) | | string | <p>_(Optional)_ The URL of the page where the event occurred.</p><p>If not specified the system will automatically use the <code>Referrer</code> header from the autogenerated request headers.</p> |
| [referrerUrl?](./analytics.eventpayload.referrerurl.md) | | string | <p>_(Optional)_ The URL of the page which the visitor came from prior to the event.</p><p>If not specified the system will automatically use the <code>Referrer</code> header from the autogenerated request headers.</p> |
| [pageUrl?](./analytics.eventpayload.pageurl.md) | | string | <p>_(Optional)_ The URL of the page where the event occurred.</p><p>If not specified the system will automatically use document.URL</p> |
| [referrerUrl?](./analytics.eventpayload.referrerurl.md) | | string | <p>_(Optional)_ The URL of the page which the visitor came from prior to the event.</p><p>If not specified the system will automatically use document.referrer</p> |
| [search?](./analytics.eventpayload.search.md) | | { searchId?: string; queryId?: string; verticalKey?: string; isDirectAnswer?: boolean; versionLabel?: [VersionLabel](./analytics.versionlabel.md)<!-- -->; versionNumber?: number; experienceKey: string; isGenerativeDirectAnswer?: boolean; } | _(Optional)_ Fields specific to reporting Yext Search Analytics Events |
| [searchTerm?](./analytics.eventpayload.searchterm.md) | | string | _(Optional)_ The query entered by the user. |
| [sessionId?](./analytics.eventpayload.sessionid.md) | | string \| null | _(Optional)_ Unique identifier to tie together events in a single browsing session |
Expand Down
2 changes: 1 addition & 1 deletion docs/analytics.eventpayload.pageurl.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

The URL of the page where the event occurred.

If not specified the system will automatically use the `Referrer` header from the autogenerated request headers.
If not specified the system will automatically use document.URL

**Signature:**

Expand Down
2 changes: 1 addition & 1 deletion docs/analytics.eventpayload.referrerurl.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

The URL of the page which the visitor came from prior to the event.

If not specified the system will automatically use the `Referrer` header from the autogenerated request headers.
If not specified the system will automatically use document.referrer

**Signature:**

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/analytics",
"version": "1.0.2",
"version": "1.1.0",
"description": "An analytics library for Yext",
"author": "fusion@yext.com",
"license": "BSD-3-Clause",
Expand Down
10 changes: 10 additions & 0 deletions src/AnalyticsEventReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ export class AnalyticsEventReporter implements AnalyticsEventService {
});
}

/** If pageUrl is undefined, default to document.URL if it exists */
if (finalPayload.pageUrl === undefined && document.URL !== '') {
finalPayload.pageUrl = document.URL;
}

/** If referrerUrl is undefined, default to document.referrer if it exists */
if (finalPayload.referrerUrl === undefined && document.referrer !== '') {
finalPayload.referrerUrl = document.referrer;
}

/** If useBeacon returns false, use postWithFetch.
If result is successful, return result json.
If request fails, return errors. */
Expand Down
8 changes: 2 additions & 6 deletions src/EventPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,11 @@ export interface EventPayload {
};
/** The URL of the page where the event occurred.
*
* If not specified the system will automatically
* use the `Referrer` header from the autogenerated request headers.
*/
* If not specified the system will automatically use document.URL */
pageUrl?: string;
/** The URL of the page which the visitor came from prior to the event.
*
* If not specified the system will automatically
* use the `Referrer` header from the autogenerated request headers.
*/
* If not specified the system will automatically use document.referrer */
referrerUrl?: string;
/** Fields specific to reporting Yext Search Analytics Events */
search?: {
Expand Down
2 changes: 1 addition & 1 deletion test-site/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 81 additions & 14 deletions tests/AnalyticsEventReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('Test report function', () => {
action: 'C_CUSTOM_ACTION',
authorization: 'KEY validKey',
clientSdk: {
ANALYTICS: '1.0.2'
ANALYTICS: '1.1.0'
},
referrerUrl: 'https://yext.com',
destinationUrl: 'https://google.com',
Expand Down Expand Up @@ -162,16 +162,78 @@ describe('Test report function', () => {
action: 'c_lowercase_custom_action',
authorization: 'Bearer bearerToken',
clientSdk: {
ANALYTICS: '1.0.2'
ANALYTICS: '1.1.0'
},
destinationUrl: 'https://google.com',
count: 5
pageUrl: 'http://localhost/',
count: 5,
sessionId: undefined
},
config
);
}
);

it('should default pageUrl and referrerUrl to document values if undefined', async () => {
mockPostWithFetch.mockResolvedValue({ id: 2222 });
mockUseBeacon.mockReturnValueOnce(false);

const originalURL = document.URL;
const originalReferrer = document.referrer;

Object.defineProperty(document, 'URL', {
value: 'https://default-url.com',
configurable: true
});

Object.defineProperty(document, 'referrer', {
value: 'https://referrer-site.com',
configurable: true
});

const config: AnalyticsConfig = {
authorizationType: 'bearer',
authorization: 'bearerToken',
forceFetch: true
};

const reporter = new AnalyticsEventReporter(config).with({
action: 'ADD_TO_CART',
count: 1
});

const res = await reporter.report({});

expect(res).toEqual({ id: 2222 });

expect(mockPostWithFetch).toHaveBeenCalledWith(
'https://us.yextevents.com/accounts/me/events',
expect.objectContaining({
action: 'ADD_TO_CART',
pageUrl: 'https://default-url.com',
referrerUrl: 'https://referrer-site.com',
authorization: 'Bearer bearerToken',
clientSdk: {
ANALYTICS: '1.1.0'
},
count: 1,
sessionId: undefined
}),
config
);

// Reset to not affect other tests
Object.defineProperty(document, 'URL', {
value: originalURL,
configurable: true
});

Object.defineProperty(document, 'referrer', {
value: originalReferrer,
configurable: true
});
});

it('call post with correct fields, report return error json if post returns an error', async () => {
const mockSetupSessionId = getOrSetupSessionId as jest.MockedFunction<
typeof getOrSetupSessionId
Expand Down Expand Up @@ -217,10 +279,11 @@ describe('Test report function', () => {
action: 'ADD_TO_CART',
authorization: 'Bearer bearerToken',
clientSdk: {
ANALYTICS: '1.0.2',
ANALYTICS: '1.1.0',
chat: '1.0.1.0'
},
destinationUrl: 'https://google.com',
pageUrl: 'http://localhost/',
referrerUrl: 'https://yext.com',
count: 5,
sessionId: 'ULID1234'
Expand Down Expand Up @@ -274,10 +337,11 @@ describe('Test report function', () => {
action: 'ADD_TO_CART',
authorization: 'Bearer bearerToken',
clientSdk: {
ANALYTICS: '1.0.2',
ANALYTICS: '1.1.0',
chat: '1.0.1.0'
},
destinationUrl: 'https://google.com',
pageUrl: 'http://localhost/',
referrerUrl: 'https://yext.com',
count: 5,
sessionId: 'ULIDORIGINAL'
Expand Down Expand Up @@ -328,10 +392,11 @@ describe('Test report function', () => {
action: 'ADD_TO_CART',
authorization: 'Bearer bearerToken',
clientSdk: {
ANALYTICS: '1.0.2',
ANALYTICS: '1.1.0',
chat: '1.0.1.0'
},
destinationUrl: 'https://google.com',
pageUrl: 'http://localhost/',
referrerUrl: 'https://yext.com',
count: 5,
sessionId: undefined
Expand Down Expand Up @@ -378,7 +443,7 @@ describe('Test report function', () => {
action: 'ADD_TO_CART',
authorization: 'KEY validKey',
clientSdk: {
ANALYTICS: '1.0.2'
ANALYTICS: '1.1.0'
},
referrerUrl: 'https://yext.com',
count: 5
Expand Down Expand Up @@ -426,7 +491,7 @@ describe('Test report function', () => {
action: 'ADD_TO_CART',
authorization: 'KEY validKey',
clientSdk: {
ANALYTICS: '1.0.2'
ANALYTICS: '1.1.0'
},
referrerUrl: 'https://yext.com',
count: 5
Expand Down Expand Up @@ -469,8 +534,10 @@ describe('Test report function', () => {
{
authorization: 'KEY validKey',
clientSdk: {
ANALYTICS: '1.0.2'
}
ANALYTICS: '1.1.0'
},
pageUrl: 'http://localhost/',
sessionId: undefined
},
config
);
Expand Down Expand Up @@ -508,7 +575,7 @@ describe('Test report function', () => {
{
authorization: 'KEY validKey',
clientSdk: {
ANALYTICS: '1.0.2'
ANALYTICS: '1.1.0'
}
},
config
Expand Down Expand Up @@ -551,7 +618,7 @@ describe('Test report function', () => {
action: 'ADD_TO_CART',
authorization: 'KEY validKey',
clientSdk: {
ANALYTICS: '1.0.2'
ANALYTICS: '1.1.0'
},
referrerUrl: 'https://yext.com',
count: 5
Expand Down Expand Up @@ -582,7 +649,7 @@ describe('Test report function', () => {
action: 'APPLY',
authorization: 'KEY validKey',
clientSdk: {
ANALYTICS: '1.0.2',
ANALYTICS: '1.1.0',
chat: '1.0.1.0'
},
destinationUrl: 'https://google.com',
Expand Down Expand Up @@ -710,7 +777,7 @@ describe('Test report function', () => {
responseId: 'responseId'
},
clientSdk: {
ANALYTICS: '1.0.2',
ANALYTICS: '1.1.0',
chat: '1.0.0'
},
count: 5,
Expand Down