Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventsSDK: Add debug mode #130

Merged
merged 8 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions docs/analytics.analyticsconfig.debug.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; [debug](./analytics.analyticsconfig.debug.md)

## AnalyticsConfig.debug property

Used to enable debug mode, which is false by default. When enabled the SDK will not send requests to the Events API, but will log the request with other useful debug information instead.

**Signature:**

```typescript
debug?: boolean;
```
1 change: 1 addition & 0 deletions docs/analytics.analyticsconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface AnalyticsConfig
| --- | --- | --- | --- |
| [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. |
| [debug?](./analytics.analyticsconfig.debug.md) | | boolean | _(Optional)_ Used to enable debug mode, which is false by default. When enabled the SDK will not send requests to the Events API, but will log the request with other useful debug information instead. |
| [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. |
| [region?](./analytics.analyticsconfig.region.md) | | [Region](./analytics.region.md) | _(Optional)_ The region to send requests to. Defaults to 'US'. |
Expand Down
1 change: 1 addition & 0 deletions etc/analytics.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function analytics(config: AnalyticsConfig): AnalyticsEventService;
export interface AnalyticsConfig {
authorization: string;
authorizationType: 'apiKey' | 'bearer';
debug?: boolean;
ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
env?: Environment;
forceFetch?: boolean;
region?: Region;
Expand Down
7 changes: 7 additions & 0 deletions src/AnalyticsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ export interface AnalyticsConfig {
* does not support fetch with the keepalive flag (like Firefox).
* If the browser does support it, fetch is used by default. */
forceFetch?: boolean;

/**
* Used to enable debug mode, which is false by default.
* When enabled the SDK will not send requests to the Events API, but will log the request
* with other useful debug information instead.
*/
debug?: boolean;
}
31 changes: 31 additions & 0 deletions src/AnalyticsEventReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,27 @@ export class AnalyticsEventReporter implements AnalyticsEventService {
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);
ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
console.log(
'[DEBUG] Payload following call to `with`:\n',
ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
currentPayload
);
}
return new AnalyticsEventReporter(this.config, currentPayload);
}

public async report(newPayload?: EventPayload): Promise<string> {
if (this.config.debug) {
console.log(
ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
'[DEBUG] Merging the following payloads:\n' +
'\nOriginal Payload from call to `with`:\n' +
`\n${this.payload ? JSON.stringify(this.payload) : '{}'}\n` +
'\nNew Payload from call to `report:`\n' +
ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
`\n${newPayload ? JSON.stringify(newPayload) : '{}'}\n`
);
ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
}

const finalPayload: EventPayload = merge(
this.payload ?? {},
newPayload ?? {}
Expand Down Expand Up @@ -66,6 +83,20 @@ export class AnalyticsEventReporter implements AnalyticsEventService {
const shouldUseBeacon = useBeacon(finalPayload, this.config.forceFetch);
const requestUrl = setupRequestUrl(this.config.env, this.config.region);

if (this.config.debug) {
console.log(
'[DEBUG] The following Payload would be sent to the Yext Events API using ' +
(shouldUseBeacon ? 'Beacon. ' : 'fetch(). ') +
'Session Tracking is ' +
(this.config.sessionTrackingEnabled ? 'enabled.\n' : 'disabled.\n') +
'Request URL: ' +
ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
requestUrl +
'\n' +
'To send the event to the Yext Events API, disable the debug flag in your AnalyticsConfig.\n',
ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
finalPayload
);
}

ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
// If useBeacon returns true, return boolean response of postWithBeacon as string.
if (shouldUseBeacon) {
return new Promise((resolve, reject) => {
ejaffee01 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading