Skip to content

Commit d23a552

Browse files
Small enhancements to types, adding userAgent to GA and fixing Segment deprecated encoder
1 parent a8b918d commit d23a552

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Component, WhisperComponentType } from '@oliveai/ldk/dist/whisper';
55
import * as Transports from './transports';
66

77
class AnalyticsClient {
8-
transport?: Transports.BaseTransport;
8+
constructor(public transport?: Transports.BaseTransport) {}
99

1010
setTransport(transport: Transports.BaseTransport) {
1111
this.transport = transport;

src/transports/base/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export abstract class BaseTransport<
1515

1616
constructor(readonly transportConfig: T, readonly userConfig: U, readonly loopConfig: L) {}
1717

18-
static async send(request: network.HTTPRequest) {
18+
protected static async send(request: network.HTTPRequest) {
1919
const { statusCode } = await network.httpRequest(request);
2020
if (statusCode < 200 || statusCode >= 300) {
2121
console.error(`Request failed with status code ${statusCode}: ${request.url}`);
@@ -31,4 +31,6 @@ export abstract class BaseTransport<
3131
abstract trackComponentCopied(type: whisper.WhisperComponentType): Promise<void>;
3232

3333
abstract trackEvent(props: any): Promise<void>;
34+
35+
protected abstract buildRequest(...args: any[]): network.HTTPRequest;
3436
}

src/transports/google/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class GoogleTransport extends BaseTransport<GoogleTransportConfig> {
1515

1616
await this.trackEvent({
1717
...this.transportConfig.categoryActionMap.whisperDisplayed,
18-
label: isUpdated ? 'Updated' : 'Created',
18+
label: `${name}, ${isUpdated ? 'updated' : 'created'}`,
1919
});
2020
}
2121

@@ -44,14 +44,12 @@ export class GoogleTransport extends BaseTransport<GoogleTransportConfig> {
4444
await GoogleTransport.send(this.buildRequest(HitType.Event, props));
4545
}
4646

47-
private buildRequest(hitType: HitType, props?: EventProps) {
47+
protected buildRequest(hitType: HitType, props?: EventProps) {
4848
return {
4949
method: 'POST',
5050
url: this.baseUrl,
5151
headers: {
52-
'User-Agent': [
53-
`OliveHelps @oliveai/loop-analytics ${this.loopConfig.name.replace(/ /g, '')}`,
54-
],
52+
'User-Agent': [this.transportConfig.userAgent], // Required by GA
5553
},
5654
body: this.buildRequestBody(hitType, props),
5755
};
@@ -71,12 +69,12 @@ export class GoogleTransport extends BaseTransport<GoogleTransportConfig> {
7169
t: hitType,
7270
dh: this.loopConfig.name,
7371
dp: `/${this.loopConfig.name}/${this.currentWhisperName}`,
74-
dt: this.currentWhisperName || '',
72+
dt: this.currentWhisperName ?? '',
7573
};
7674
if (hitType === HitType.Event && props) {
7775
propStringObj.ec = props.category;
7876
propStringObj.ea = props.action;
79-
propStringObj.el = props.label;
77+
propStringObj.el = props.label ?? '';
8078
}
8179
customDimensions?.forEach(({ index, value }) => {
8280
propStringObj[`cd${index}`] = value;
@@ -86,6 +84,7 @@ export class GoogleTransport extends BaseTransport<GoogleTransportConfig> {
8684
});
8785

8886
return Object.entries(propStringObj)
87+
.filter(([, value]) => !!value)
8988
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
9089
.join('&');
9190
}

src/transports/google/types.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,31 @@ export enum HitType {
88
export interface EventProps {
99
category: string;
1010
action: string;
11-
label: string;
11+
label?: string;
1212
}
1313

14-
export interface CategoryActionObj {
15-
category: string;
16-
action: string;
14+
export interface CategoryActionMap {
15+
[key: string]: EventProps;
16+
whisperDisplayed: EventProps;
17+
whisperClosed: EventProps;
18+
componentClicked: EventProps;
19+
componentCopied: EventProps;
1720
}
1821

19-
export interface CategoryActionMap {
20-
[key: string]: CategoryActionObj;
21-
whisperDisplayed: CategoryActionObj;
22-
whisperClosed: CategoryActionObj;
23-
componentClicked: CategoryActionObj;
24-
componentCopied: CategoryActionObj;
22+
export interface CustomDimensionOrMetric {
23+
index: number;
24+
name: string;
25+
value: string;
2526
}
2627

2728
export interface GoogleTransportConfig extends TransportConfig {
2829
apiVersion?: number;
2930
trackingId: string;
31+
userAgent: string;
3032
/** Set expectations for what categories and actions are going to be used */
3133
categoryActionMap: CategoryActionMap;
3234
/** Map custom dimension index to an alias name and set its value */
33-
customDimensions?: { index: number; name: string; value: string }[];
34-
customMetrics?: { index: number; name: string; value: string }[];
35+
customDimensions?: CustomDimensionOrMetric[];
36+
/** Map custom metric index to an alias name and set its value */
37+
customMetrics?: CustomDimensionOrMetric[];
3538
}

src/transports/segment/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class SegmentTransport extends BaseTransport<SegmentTransportConfig, Segm
7272
await SegmentTransport.send(this.buildRequest('/track', props));
7373
}
7474

75-
private buildRequest(endpoint: string, props: SegmentProps) {
75+
protected buildRequest(endpoint: string, props: SegmentProps) {
7676
return {
7777
method: 'POST',
7878
url: `${this.baseUrl}${endpoint}`,
@@ -83,7 +83,9 @@ export class SegmentTransport extends BaseTransport<SegmentTransportConfig, Segm
8383

8484
private buildRequestHeaders() {
8585
return {
86-
Authorization: [`Basic ${btoa(`${this.transportConfig.writeKey}:`)}`],
86+
Authorization: [
87+
`Basic ${Buffer.from(`${this.transportConfig.writeKey}:`).toString('base64')}`,
88+
],
8789
'Content-Type': ['application/json'],
8890
};
8991
}

0 commit comments

Comments
 (0)