diff --git a/docs/analytics.eventpayload.md b/docs/analytics.eventpayload.md index 5082632..f450a50 100644 --- a/docs/analytics.eventpayload.md +++ b/docs/analytics.eventpayload.md @@ -33,8 +33,8 @@ export interface EventPayload | [locale?](./analytics.eventpayload.locale.md) | | string |

_(Optional)_ The locale of the user who generated the event.

If not specified the system will automatically determine the locale from the autogenerated Accept-Language header.

| | [location?](./analytics.eventpayload.location.md) | | Coordinates \| string |

_(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.

If not specified the system will automatically determine all location information from the request's IP address, or the value of the ip property if specified.

| | [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 |

_(Optional)_ 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.

| -| [referrerUrl?](./analytics.eventpayload.referrerurl.md) | | string |

_(Optional)_ 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.

| +| [pageUrl?](./analytics.eventpayload.pageurl.md) | | string |

_(Optional)_ The URL of the page where the event occurred.

If not specified the system will automatically use document.URL

| +| [referrerUrl?](./analytics.eventpayload.referrerurl.md) | | string |

_(Optional)_ The URL of the page which the visitor came from prior to the event.

If not specified the system will automatically use document.referrer

| | [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 | diff --git a/docs/analytics.eventpayload.pageurl.md b/docs/analytics.eventpayload.pageurl.md index 0b0535f..d7cf63f 100644 --- a/docs/analytics.eventpayload.pageurl.md +++ b/docs/analytics.eventpayload.pageurl.md @@ -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:** diff --git a/docs/analytics.eventpayload.referrerurl.md b/docs/analytics.eventpayload.referrerurl.md index 904a99e..0bc97f9 100644 --- a/docs/analytics.eventpayload.referrerurl.md +++ b/docs/analytics.eventpayload.referrerurl.md @@ -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:** diff --git a/package-lock.json b/package-lock.json index 6f1fd04..3fd7944 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@yext/analytics", - "version": "1.0.2", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@yext/analytics", - "version": "1.0.2", + "version": "1.1.0", "license": "BSD-3-Clause", "dependencies": { "ulidx": "^2.0.0" diff --git a/package.json b/package.json index 8fab97f..9c75631 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/AnalyticsEventReporter.ts b/src/AnalyticsEventReporter.ts index 4f61c62..2d4b2be 100644 --- a/src/AnalyticsEventReporter.ts +++ b/src/AnalyticsEventReporter.ts @@ -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. */ diff --git a/src/EventPayload.ts b/src/EventPayload.ts index 397db81..38d3431 100644 --- a/src/EventPayload.ts +++ b/src/EventPayload.ts @@ -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?: { diff --git a/test-site/package-lock.json b/test-site/package-lock.json index 15bf78e..7f2c78a 100644 --- a/test-site/package-lock.json +++ b/test-site/package-lock.json @@ -23,7 +23,7 @@ }, "..": { "name": "@yext/analytics", - "version": "1.0.2", + "version": "1.1.0", "license": "BSD-3-Clause", "dependencies": { "ulidx": "^2.0.0" diff --git a/tests/AnalyticsEventReporter.test.ts b/tests/AnalyticsEventReporter.test.ts index 9440960..ae0d818 100644 --- a/tests/AnalyticsEventReporter.test.ts +++ b/tests/AnalyticsEventReporter.test.ts @@ -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', @@ -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 @@ -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' @@ -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' @@ -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 @@ -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 @@ -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 @@ -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 ); @@ -508,7 +575,7 @@ describe('Test report function', () => { { authorization: 'KEY validKey', clientSdk: { - ANALYTICS: '1.0.2' + ANALYTICS: '1.1.0' } }, config @@ -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 @@ -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', @@ -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,