Skip to content

Commit

Permalink
Merge e3e5d37 into 52aa4b4
Browse files Browse the repository at this point in the history
  • Loading branch information
ejaffee01 committed Jan 25, 2024
2 parents 52aa4b4 + e3e5d37 commit ac738a8
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 92 deletions.
2 changes: 1 addition & 1 deletion docs/analytics.action.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ The action types accepted by the Analytics Events API.
**Signature:**

```typescript
export declare type Action = `C_${string}` | `c_${string}` | 'ADD_TO_CART' | 'ALL_TAB_NAVIGATION' | 'APPLY' | 'AUTO_COMPLETE_SELECTION' | 'BACKWARD_PAGINATE' | 'BOOK' | 'BRAND_ICON' | 'CASE_START' | 'CASE_SUBMITTED' | 'CHAT_IMPRESSION' | 'CHAT_LINK_CLICK' | 'CHAT_RESPONSE' | 'COLLAPSE' | 'CTA_CLICK' | 'DRIVING_DIRECTIONS' | 'EVENT' | 'EXPAND' | 'FEATURED_MESSAGE' | 'FILTERING_WITHIN_SECTION' | 'FORWARD_PAGINATE' | 'HEADER_LINKS' | 'ITEM_IN_LIST' | 'MAP_CARD' | 'MAP_PIN' | 'MENU' | 'MESSAGE' | 'ORDER' | 'PAGE_VIEW' | 'PAGINATE' | 'POST' | 'PRESET_PROMPT' | 'PRODUCT' | 'PROFILE' | 'QUESTION_FOCUS' | 'QUESTION_SUBMIT' | 'REMOVED_FILTER' | 'REVIEW' | 'SCROLL_TO_BOTTOM_OF_PAGE' | 'SEARCH_BAR_IMPRESSION' | 'SEARCH_CLEAR_BUTTON' | 'TAP_TO_CALL' | 'THUMBS_DOWN' | 'THUMBS_UP' | 'TICKET_URL' | 'TITLE' | 'VERTICAL_TAB_NAVIGATION' | 'VERTICAL_VIEW_ALL' | 'VOICE_START' | 'VOICE_STOP' | 'WEBSITE';
export type Action = `C_${string}` | `c_${string}` | 'ADD_TO_CART' | 'ALL_TAB_NAVIGATION' | 'APPLY' | 'AUTO_COMPLETE_SELECTION' | 'BACKWARD_PAGINATE' | 'BOOK' | 'BRAND_ICON' | 'CASE_START' | 'CASE_SUBMITTED' | 'CHAT_IMPRESSION' | 'CHAT_LINK_CLICK' | 'CHAT_RESPONSE' | 'COLLAPSE' | 'CTA_CLICK' | 'DRIVING_DIRECTIONS' | 'EVENT' | 'EXPAND' | 'FEATURED_MESSAGE' | 'FILTERING_WITHIN_SECTION' | 'FORWARD_PAGINATE' | 'HEADER_LINKS' | 'ITEM_IN_LIST' | 'MAP_CARD' | 'MAP_PIN' | 'MENU' | 'MESSAGE' | 'ORDER' | 'PAGE_VIEW' | 'PAGINATE' | 'POST' | 'PRESET_PROMPT' | 'PRODUCT' | 'PROFILE' | 'QUESTION_FOCUS' | 'QUESTION_SUBMIT' | 'REMOVED_FILTER' | 'REVIEW' | 'SCROLL_TO_BOTTOM_OF_PAGE' | 'SEARCH_BAR_IMPRESSION' | 'SEARCH_CLEAR_BUTTON' | 'TAP_TO_CALL' | 'THUMBS_DOWN' | 'THUMBS_UP' | 'TICKET_URL' | 'TITLE' | 'VERTICAL_TAB_NAVIGATION' | 'VERTICAL_VIEW_ALL' | 'VOICE_START' | 'VOICE_STOP' | 'WEBSITE';
```
13 changes: 13 additions & 0 deletions docs/analytics.analyticsconfig.authorization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/analytics](./analytics.md) &gt; [AnalyticsConfig](./analytics.analyticsconfig.md) &gt; [authorization](./analytics.analyticsconfig.authorization.md)

## AnalyticsConfig.authorization property

The API Key, OAuth, or bearer token for accessing the Analytics Events API.

**Signature:**

```typescript
authorization: string;
```
13 changes: 13 additions & 0 deletions docs/analytics.analyticsconfig.authorizationtype.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/analytics](./analytics.md) &gt; [AnalyticsConfig](./analytics.analyticsconfig.md) &gt; [authorizationType](./analytics.analyticsconfig.authorizationtype.md)

## AnalyticsConfig.authorizationType property

Used for specifying if an API Key or Bearer Token is used for the authorization property.

**Signature:**

```typescript
authorizationType: 'apiKey' | 'Bearer';
```
13 changes: 0 additions & 13 deletions docs/analytics.analyticsconfig.bearer.md

This file was deleted.

13 changes: 0 additions & 13 deletions docs/analytics.analyticsconfig.key.md

This file was deleted.

4 changes: 2 additions & 2 deletions docs/analytics.analyticsconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export interface AnalyticsConfig

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [bearer?](./analytics.analyticsconfig.bearer.md) | | string | _(Optional)_ The bearer token for accessing the Analytics Events API. Only one of key or bearer should be set. |
| [authorization](./analytics.analyticsconfig.authorization.md) | | string | The API Key, OAuth, or bearer token for accessing the Analytics Events API. |
| [authorizationType](./analytics.analyticsconfig.authorizationtype.md) | | 'apiKey' \| 'Bearer' | Used for specifying if an API Key or Bearer Token is used for the authorization property. |
| [env?](./analytics.analyticsconfig.env.md) | | [Environment](./analytics.environment.md) | _(Optional)_ The Yext environment to send requests to. Defaults to 'PRODUCTION'. |
| [forceFetch?](./analytics.analyticsconfig.forcefetch.md) | | boolean | _(Optional)_ Used to force sending the request with fetch even if the browser does not support fetch with the keepalive flag (like Firefox). If the browser does support it, fetch is used by default. |
| [key?](./analytics.analyticsconfig.key.md) | | string | _(Optional)_ The API Key or OAuth for accessing the Analytics Events API Only one of key or bearer should be set. |
| [region?](./analytics.analyticsconfig.region.md) | | [Region](./analytics.region.md) | _(Optional)_ The region to send requests to. Defaults to 'US'. |
| [sessionTrackingEnabled?](./analytics.analyticsconfig.sessiontrackingenabled.md) | | boolean | _(Optional)_ Whether to enable session tracking for analytics events. Defaults to true for both environments. If set to false, sessionId will automatically be set to undefined in the event payload. |

2 changes: 1 addition & 1 deletion docs/analytics.enumorstring.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A TypeScript utility type which creates a union of an enum member and its string
**Signature:**

```typescript
export declare type EnumOrString<T extends string> = T | `${T}`;
export type EnumOrString<T extends string> = T | `${T}`;
```

## Remarks
Expand Down
2 changes: 1 addition & 1 deletion docs/analytics.environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The Yext Environments
**Signature:**

```typescript
export declare type Environment = EnumOrString<EnvironmentEnum>;
export type Environment = EnumOrString<EnvironmentEnum>;
```
**References:** [EnumOrString](./analytics.enumorstring.md)<!-- -->, [EnvironmentEnum](./analytics.environmentenum.md)

Expand Down
2 changes: 1 addition & 1 deletion docs/analytics.partialpayload.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A Payload that is a subset of the EventPayload
**Signature:**

```typescript
export declare type PartialPayload = Partial<Record<keyof EventPayload, unknown>>;
export type PartialPayload = Partial<Record<keyof EventPayload, unknown>>;
```
**References:** [EventPayload](./analytics.eventpayload.md)

2 changes: 1 addition & 1 deletion docs/analytics.region.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The physical region of the Yext account
**Signature:**

```typescript
export declare type Region = EnumOrString<RegionEnum>;
export type Region = EnumOrString<RegionEnum>;
```
**References:** [EnumOrString](./analytics.enumorstring.md)<!-- -->, [RegionEnum](./analytics.regionenum.md)

Expand Down
2 changes: 1 addition & 1 deletion docs/analytics.versionlabel.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The Search Version Label
**Signature:**

```typescript
export declare type VersionLabel = EnumOrString<VersionLabelEnum>;
export type VersionLabel = EnumOrString<VersionLabelEnum>;
```
**References:** [EnumOrString](./analytics.enumorstring.md)<!-- -->, [VersionLabelEnum](./analytics.versionlabelenum.md)

Expand Down
4 changes: 2 additions & 2 deletions etc/analytics.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export function analytics(config: AnalyticsConfig): AnalyticsEventService;

// @public
export interface AnalyticsConfig {
bearer?: string;
authorization: string;
authorizationType: 'apiKey' | 'Bearer';
env?: Environment;
forceFetch?: boolean;
key?: string;
region?: Region;
sessionTrackingEnabled?: boolean;
}
Expand Down
12 changes: 4 additions & 8 deletions src/AnalyticsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import { Region } from './Region';
* @public
*/
export interface AnalyticsConfig {
/** The API Key or OAuth for accessing the Analytics Events API
* Only one of key or bearer should be set.
* */
key?: string;
/** The bearer token for accessing the Analytics Events API.
* Only one of key or bearer should be set.
* */
bearer?: string;
/** Used for specifying if an API Key or Bearer Token is used for the authorization property. */
authorizationType: 'apiKey' | 'Bearer';
/** The API Key, OAuth, or bearer token for accessing the Analytics Events API. */
authorization: string;
/** The Yext environment to send requests to. Defaults to 'PRODUCTION'. */
env?: Environment;
/** The region to send requests to. Defaults to 'US'. */
Expand Down
14 changes: 4 additions & 10 deletions src/AnalyticsEventReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ export class AnalyticsEventReporter implements AnalyticsEventService {
* @param payload - (optional) desired event values to report
*/
constructor(config: AnalyticsConfig, payload?: EventPayload) {
const apiKeyIsSet = config.key !== undefined;
const bearerTokenIsSet = config.bearer !== undefined;
const configIsValid =
(apiKeyIsSet || bearerTokenIsSet) && !(apiKeyIsSet && bearerTokenIsSet);
if (!configIsValid) {
throw new Error('Provide one and only one of API Key or Bearer Token.');
}
this.config = config;
this.payload = payload;
}
Expand Down Expand Up @@ -56,9 +49,10 @@ export class AnalyticsEventReporter implements AnalyticsEventService {
packageinfo.version)
: (finalPayload.clientSdk = { ['ANALYTICS']: packageinfo.version });

finalPayload.authorization = this.config.key
? 'KEY ' + this.config.key
: 'Bearer ' + this.config.bearer;
finalPayload.authorization =
this.config.authorizationType === 'apiKey'
? 'KEY ' + this.config.authorization
: 'Bearer ' + this.config.authorization;

const shouldUseBeacon = useBeacon(finalPayload, this.config.forceFetch);
const requestUrl = setupRequestUrl(this.config.env, this.config.region);
Expand Down
3 changes: 2 additions & 1 deletion src/convertStringToValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventPayload } from './EventPayload';
import { AnalyticsConfig } from './AnalyticsConfig';

// Define the list of possibly numerical properties to check and convert
const propertiesToCheck = [
Expand All @@ -18,7 +19,7 @@ const propertiesToCheck = [

export function convertStringToValue(
data: Record<string, unknown>
): EventPayload {
): EventPayload | AnalyticsConfig {
// Recursive function to traverse and convert nested objects
function recursiveConversion(obj: Record<string, unknown>) {
for (const property in obj) {
Expand Down
6 changes: 4 additions & 2 deletions test-site/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { analytics } from '@yext/analytics';
* We will continue investigating this make a fix if necessary.
*/
const analyticsProvider = analytics({
key: process.env.YEXT_API_KEY,
authorizationType: 'apiKey',
authorization: process.env.YEXT_API_KEY,
sessionTrackingEnabled: false
}).with({
action: 'CHAT_LINK_CLICK',
Expand Down Expand Up @@ -53,7 +54,8 @@ const analyticsProvider = analytics({
});

const analyticsProvideWithSessionTracking = analytics({
key: process.env.YEXT_API_KEY
authorizationType: 'apiKey',
authorization: process.env.YEXT_API_KEY
}).with({
action: 'CHAT_LINK_CLICK',
pageUrl: 'http://www.yext-test-pageurl.com',
Expand Down
61 changes: 26 additions & 35 deletions tests/AnalyticsEventReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,16 @@ import { AnalyticsEventService } from '../src/AnalyticsEventService';
jest.mock('../src/post');
jest.mock('../src/setupSessionId');

it('Invalid config with no authorization field', () => {
expect(() => new AnalyticsEventReporter({})).toThrowError(
'Provide one and only one of API Key or Bearer Token.'
);
const InvalidConfig: AnalyticsConfig = {
key: undefined
};
expect(() => new AnalyticsEventReporter(InvalidConfig)).toThrowError(
'Provide one and only one of API Key or Bearer Token.'
);
});

it('Invalid config with both authorization field', () => {
const InvalidConfig: AnalyticsConfig = {
key: 'mock-api-key',
bearer: 'mock-bearer-token'
};
expect(() => new AnalyticsEventReporter(InvalidConfig)).toThrowError(
'Provide one and only one of API Key or Bearer Token.'
);
});

it('Valid config will not throw error', () => {
const configwithAPI: AnalyticsConfig = {
key: 'mock-api-key'
authorizationType: 'apiKey',
authorization: 'mock-api-key'
};
expect(() => new AnalyticsEventReporter(configwithAPI)).not.toThrow();

const configwithBearer: AnalyticsConfig = {
bearer: 'mock-bearer-token'
authorizationType: 'Bearer',
authorization: 'bearerToken'
};

expect(() => new AnalyticsEventReporter(configwithBearer)).not.toThrow();
Expand Down Expand Up @@ -74,7 +54,8 @@ describe('Test report function', () => {
});

const config: AnalyticsConfig = {
key: 'validKey',
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
forceFetch: false
};
Expand Down Expand Up @@ -116,7 +97,8 @@ describe('Test report function', () => {
mockUseBeacon.mockReturnValueOnce(false);

const config: AnalyticsConfig = {
bearer: 'bearerToken',
authorizationType: 'Bearer',
authorization: 'bearerToken',
env: EnvironmentEnum.Sandbox
};
const reporter = new AnalyticsEventReporter(config).with({
Expand Down Expand Up @@ -163,7 +145,8 @@ describe('Test report function', () => {
mockUseBeacon.mockReturnValueOnce(false);

const config: AnalyticsConfig = {
bearer: 'bearerToken',
authorizationType: 'Bearer',
authorization: 'bearerToken',
sessionTrackingEnabled: true
};
const reporter = new AnalyticsEventReporter(config).with({
Expand Down Expand Up @@ -217,7 +200,8 @@ describe('Test report function', () => {
mockUseBeacon.mockReturnValueOnce(false);

const config: AnalyticsConfig = {
bearer: 'bearerToken',
authorizationType: 'Bearer',
authorization: 'bearerToken',
sessionTrackingEnabled: true
};
const reporter = new AnalyticsEventReporter(config).with({
Expand Down Expand Up @@ -268,7 +252,8 @@ describe('Test report function', () => {
mockPostWithFetch.mockResolvedValue({ id: 1111 });

const config: AnalyticsConfig = {
bearer: 'bearerToken',
authorizationType: 'Bearer',
authorization: 'bearerToken',
sessionTrackingEnabled: false
};
const reporter = new AnalyticsEventReporter(config).with({
Expand Down Expand Up @@ -327,7 +312,8 @@ describe('Test report function', () => {
});

const config: AnalyticsConfig = {
key: 'validKey',
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
forceFetch: false
};
Expand Down Expand Up @@ -372,7 +358,8 @@ describe('Test report function', () => {
});

const config: AnalyticsConfig = {
key: 'validKey',
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
forceFetch: false
};
Expand Down Expand Up @@ -419,7 +406,8 @@ describe('Test report function', () => {
});

const config: AnalyticsConfig = {
key: 'validKey',
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
forceFetch: true
};
Expand Down Expand Up @@ -456,7 +444,8 @@ describe('Test report function', () => {
});

const config: AnalyticsConfig = {
key: 'validKey',
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
forceFetch: false
};
Expand Down Expand Up @@ -494,7 +483,8 @@ describe('Test report function', () => {
});

const config: AnalyticsConfig = {
key: 'validKey',
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
forceFetch: false
};
Expand Down Expand Up @@ -569,7 +559,8 @@ describe('Test report function', () => {
});

const config: AnalyticsConfig = {
key: 'validKey',
authorizationType: 'apiKey',
authorization: 'validKey',
region: RegionEnum.EU,
forceFetch: false,
sessionTrackingEnabled: true
Expand Down

0 comments on commit ac738a8

Please sign in to comment.