Skip to content

Commit

Permalink
Add more comments, rename analyticsGTM to reportBrowserAnalytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Jaffee committed Nov 13, 2023
1 parent c86cd83 commit be90b35
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ export function analytics(config: AnalyticsConfig): AnalyticsEventService {
}

/**
* The exported function of Yext Analytics Events SDK used for Google Tag Manager.
* An alternative entry point for the Yext Analytics Events SDK, currently used by Google Tag Manager.
* This method reads the config and payload from the variable analyticsEventPayload stored
* in the global window object. The config and payload are then passed to the report function to be sent
* to the Yext Analytics Events API.
* @public
*/
export function analyticsGTM(): Promise<string> {
export function reportBrowserAnalytics(): Promise<string> {
const gtmPayload = window['analyticsEventPayload'];
let response: Promise<string>;
if (gtmPayload) {
const config = gtmPayload[0][1] as Record<string, unknown>;
const data = gtmPayload[1][1] as Record<string, unknown>;
if (config) {
// User text input inside of Google Tag Manager defaults to a String type for all fields.
// However, the Events API expects true boolean and number types for certain fields.
// The below convertStringToValue method calls take care of converting the String types
// to the correct one's for the config and payload objects.
const correctedConfig = convertStringToValue(config);
const correctedData = convertStringToValue(data);
const reporter = new AnalyticsEventReporter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// analyticsGTM.test.ts
import { analyticsGTM } from '../src/index';
import { reportBrowserAnalytics } from '../src/index';
import { AnalyticsEventReporter } from '../src/AnalyticsEventReporter'; // replace with actual import path

describe('analyticsGTM', () => {
describe('reportBrowserAnalytics', () => {
let reportSpy: jest.SpyInstance;

beforeEach(() => {
Expand All @@ -25,7 +24,7 @@ describe('analyticsGTM', () => {
];
(global as any).window['analyticsEventPayload'] = mockPayload;

Check warning on line 25 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / test-e2e

Unexpected any. Specify a different type

Check warning on line 25 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (14.x)

Unexpected any. Specify a different type

Check warning on line 25 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

Unexpected any. Specify a different type

Check warning on line 25 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / test-e2e

Unexpected any. Specify a different type

Check warning on line 25 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (14.x)

Unexpected any. Specify a different type

Check warning on line 25 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / call_update_docs / update-docs

Unexpected any. Specify a different type

Check warning on line 25 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

Unexpected any. Specify a different type

const result = await analyticsGTM();
const result = await reportBrowserAnalytics();

expect(result).toBe('report');
expect(reportSpy).toHaveBeenCalled();
Expand All @@ -34,6 +33,6 @@ describe('analyticsGTM', () => {
it('should return a promise that rejects when analyticeEvenPayload is not present in window', async () => {
(global as any).window['analyticsEventPayload'] = undefined;

Check warning on line 34 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / test-e2e

Unexpected any. Specify a different type

Check warning on line 34 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (14.x)

Unexpected any. Specify a different type

Check warning on line 34 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

Unexpected any. Specify a different type

Check warning on line 34 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / test-e2e

Unexpected any. Specify a different type

Check warning on line 34 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (14.x)

Unexpected any. Specify a different type

Check warning on line 34 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / call_update_docs / update-docs

Unexpected any. Specify a different type

Check warning on line 34 in tests/reportBrowserAnalytics.test.ts

View workflow job for this annotation

GitHub Actions / call_run_tests / tests (16.x)

Unexpected any. Specify a different type

await expect(analyticsGTM()).rejects.toEqual('No payload found.');
await expect(reportBrowserAnalytics()).rejects.toEqual('No payload found.');
});
});

0 comments on commit be90b35

Please sign in to comment.