-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path_sendEvent.ts
111 lines (101 loc) · 3.34 KB
/
_sendEvent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { addQueryId } from "./_addQueryId";
import type AlgoliaAnalytics from "./insights";
import type { InsightsAdditionalEventParams, InsightsEvent } from "./types";
import { isPromise, isUndefined, removeQueryForObjects } from "./utils";
import type { RequestFnType } from "./utils/request";
export function makeSendEvents(requestFn: RequestFnType) {
return function sendEvents(
this: AlgoliaAnalytics,
eventData: InsightsEvent[],
additionalParams?: InsightsAdditionalEventParams
): Promise<boolean> {
if (this._userHasOptedOut) {
return Promise.resolve(false);
}
const hasCredentials =
(!isUndefined(this._apiKey) && !isUndefined(this._appId)) ||
(additionalParams?.headers?.["X-Algolia-Application-Id"] &&
additionalParams?.headers?.["X-Algolia-API-Key"]);
if (!hasCredentials) {
throw new Error(
"Before calling any methods on the analytics, you first need to call the 'init' function with appId and apiKey parameters or provide custom credentials in additional parameters."
);
}
if (!this._userToken && this._anonymousUserToken) {
this.setAnonymousUserToken(true);
}
const events: InsightsEvent[] = (
additionalParams?.inferQueryID ? addQueryId(eventData) : eventData
).map((data) => {
const { filters, ...rest } = data;
const payload: InsightsEvent = {
...rest,
userToken: data?.userToken ?? this._userToken,
authenticatedUserToken:
data?.authenticatedUserToken ?? this._authenticatedUserToken
};
if (!isUndefined(filters)) {
payload.filters = filters.map(encodeURIComponent);
}
return payload;
});
if (events.length === 0) {
return Promise.resolve(false);
}
const send = sendRequest(
requestFn,
this._ua,
this._endpointOrigin,
events,
this._appId,
this._apiKey,
additionalParams?.headers
);
return isPromise(send) ? send.then(purgePurchased(events)) : send;
};
}
function purgePurchased(events: InsightsEvent[]): (value: boolean) => boolean {
return (sent) => {
if (sent) {
events
.filter(
({ eventType, eventSubtype, objectIDs }) =>
eventType === "conversion" &&
eventSubtype === "purchase" &&
objectIDs?.length
)
.forEach(({ index, objectIDs }) =>
removeQueryForObjects(index, objectIDs!)
);
}
return sent;
};
}
// eslint-disable-next-line max-params
function sendRequest(
requestFn: RequestFnType,
userAgents: string[],
endpointOrigin: string,
events: InsightsEvent[],
appId?: string,
apiKey?: string,
additionalHeaders: InsightsAdditionalEventParams["headers"] = {}
): Promise<boolean> {
const {
"X-Algolia-Application-Id": providedAppId,
"X-Algolia-API-Key": providedApiKey,
...restHeaders
} = additionalHeaders;
// Auth query
const headers: Record<string, string> = {
"X-Algolia-Application-Id": providedAppId ?? appId,
"X-Algolia-API-Key": providedApiKey ?? apiKey,
"X-Algolia-Agent": encodeURIComponent(userAgents.join("; ")),
...restHeaders
};
const queryParameters = Object.keys(headers)
.map((key) => `${key}=${headers[key]}`)
.join("&");
const reportingURL = `${endpointOrigin}/1/events?${queryParameters}`;
return requestFn(reportingURL, { events });
}