• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yext / analytics / 14344180410

08 Apr 2025 09:43PM UTC coverage: 83.077% (-0.5%) from 83.607%
14344180410

Pull #152

github

github-actions[bot]
Automated update to repo's documentation from github action
Pull Request #152: EventsSDK: update pageUrl and referrerUrl defaults

79 of 92 branches covered (85.87%)

Branch coverage included in aggregate %.

3 of 4 new or added lines in 1 file covered. (75.0%)

83 of 103 relevant lines covered (80.58%)

7.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.08
/src/AnalyticsEventReporter.ts
1
import { AnalyticsEventService } from './AnalyticsEventService';
2
import { AnalyticsConfig } from './AnalyticsConfig';
3
import { EventPayload } from './EventPayload';
4
import { getOrSetupSessionId } from './setupSessionId';
5
import packageinfo from '../package.json';
6
import { postWithBeacon, postWithFetch, useBeacon } from './post';
7
import merge from './merge';
8
import { setupRequestUrl } from './setupRequestUrl';
9

10
/** Represents an reporter is responsible for reporting analytics events. */
11
export class AnalyticsEventReporter implements AnalyticsEventService {
12
  private config: AnalyticsConfig;
13
  private payload?: EventPayload;
14
  /**
15
   * @param config - necessary analytics config: Must provide one and only
16
   * one of API Key or Bearer Token.
17
   *
18
   * @param payload - (optional) desired event values to report
19
   */
20
  constructor(config: AnalyticsConfig, payload?: EventPayload) {
21
    if (
28✔
22
      config.authorizationType !== 'apiKey' &&
40✔
23
      config.authorizationType !== 'bearer'
24
    ) {
25
      throw new Error('Authorization type must be either apiKey or bearer.');
3✔
26
    }
27
    if (!config.authorization) {
25✔
28
      throw new Error('Authorization must be provided.');
2✔
29
    }
30
    this.config = config;
23✔
31
    this.payload = payload;
23✔
32
  }
33

34
  with(payload: EventPayload): AnalyticsEventService {
35
    const currentPayload =
36
      this.payload === undefined ? payload : merge(this.payload, payload);
9✔
37

38
    return new AnalyticsEventReporter(this.config, currentPayload);
9✔
39
  }
40

41
  public async report(newPayload?: EventPayload): Promise<string> {
42
    const finalPayload: EventPayload = merge(
12✔
43
      this.payload ?? {},
15✔
44
      newPayload ?? {}
15✔
45
    );
46

47
    /** If session tracking is disabled, set sessionId to undefined. If it is,
48
     * enabled, and sessionId was not already manually added to the event payload,
49
     * call getOrSetupSessionId to set value.
50
     */
51
    if (!this.config.sessionTrackingEnabled) {
12✔
52
      finalPayload.sessionId = undefined;
9✔
53
    } else if (!finalPayload.sessionId) {
3✔
54
      finalPayload.sessionId = getOrSetupSessionId();
1✔
55
    }
56

57
    finalPayload.clientSdk
12✔
58
      ? ((finalPayload.clientSdk as Record<string, string>)['ANALYTICS'] =
59
          packageinfo.version)
60
      : (finalPayload.clientSdk = { ['ANALYTICS']: packageinfo.version });
61

62
    finalPayload.authorization =
12✔
63
      this.config.authorizationType === 'apiKey'
12✔
64
        ? 'KEY ' + this.config.authorization
65
        : 'Bearer ' + this.config.authorization;
66

67
    const shouldUseBeacon = useBeacon(finalPayload, this.config.forceFetch);
12✔
68
    const requestUrl = setupRequestUrl(this.config.env, this.config.region);
12✔
69

70
    // If useBeacon returns true, return boolean response of postWithBeacon as string.
71
    if (shouldUseBeacon) {
12✔
72
      return new Promise((resolve, reject) => {
7✔
73
        if (postWithBeacon(requestUrl, finalPayload, this.config)) {
7✔
74
          resolve('');
6✔
75
        } else {
76
          reject('Failed Beacon Call');
1✔
77
        }
78
      });
79
    }
80

81
    /** If pageUrl is undefined, default to document.URL if it exists */
82
    if (finalPayload.pageUrl === undefined && document.URL !== '') {
5!
83
      finalPayload.pageUrl = document.URL;
5✔
84
    }
85

86
    /** If referrerUrl is undefined, default to document.referrer if it exists */
87
    if (finalPayload.referrerUrl === undefined && document.referrer !== '') {
5!
NEW
88
      finalPayload.referrerUrl = document.referrer;
×
89
    }
90

91
    /** If useBeacon returns false, use postWithFetch.
92
      If result is successful, return result json.
93
      If request fails, return errors. */
94
    return postWithFetch(requestUrl, finalPayload, this.config)
5✔
95
      .then((response) => response)
2✔
96
      .catch((e) => e);
3✔
97
  }
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc