Skip to content

Commit 09141a1

Browse files
authored
feat(UserAgent): add user agent string (#100)
* feat(UserAgent): add user agent string * fix: remove vanilla from UA string Co-Authored-By: tkrugg <tkrugg@users.noreply.github.com> * feedback: remove destructuring
1 parent acaa613 commit 09141a1

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

lib/__tests__/_sendEvent.test.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import AlgoliaInsights from "../insights";
22
import * as url from "url";
3+
import * as querystring from "querystring";
4+
5+
jest.mock("../../package.json", () => ({
6+
version: "1.0.1"
7+
}));
38

49
const credentials = {
5-
apiKey: "test",
6-
appId: "test"
10+
apiKey: "testKey",
11+
appId: "testId"
712
};
813

914
describe("sendEvent", () => {
@@ -54,6 +59,20 @@ describe("sendEvent", () => {
5459
]
5560
});
5661
});
62+
it("should include X-Algolia-* query parameters", () => {
63+
(AlgoliaInsights as any).sendEvent("click", {
64+
eventName: "my-event",
65+
index: "my-index",
66+
objectIDs: ["1"]
67+
});
68+
const requestUrl = XMLHttpRequest.open.mock.calls[0][1];
69+
const { query } = url.parse(requestUrl);
70+
expect(querystring.parse(query)).toEqual({
71+
"X-Algolia-API-Key": "testKey",
72+
"X-Algolia-Agent": "Algolia insights for JavaScript (1.0.1)",
73+
"X-Algolia-Application-Id": "testId"
74+
});
75+
});
5776
});
5877

5978
describe("with sendBeacon", () => {
@@ -100,6 +119,20 @@ describe("sendEvent", () => {
100119
]
101120
});
102121
});
122+
it("should include X-Algolia-* query parameters", () => {
123+
(AlgoliaInsights as any).sendEvent("click", {
124+
eventName: "my-event",
125+
index: "my-index",
126+
objectIDs: ["1"]
127+
});
128+
const requestUrl = sendBeacon.mock.calls[0][0];
129+
const { query } = url.parse(requestUrl);
130+
expect(querystring.parse(query)).toEqual({
131+
"X-Algolia-API-Key": "testKey",
132+
"X-Algolia-Agent": "Algolia insights for JavaScript (1.0.1)",
133+
"X-Algolia-Application-Id": "testId"
134+
});
135+
});
103136
});
104137

105138
describe("init", () => {
@@ -277,6 +310,7 @@ describe("sendEvent", () => {
277310
});
278311
});
279312
});
313+
280314
describe("filters", () => {
281315
it("should pass over provided filters", () => {
282316
(AlgoliaInsights as any).sendEvent("click", {

lib/_sendEvent.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isNumber, isUndefined, isString, isFunction } from "./utils/index";
2+
import { version } from "../package.json";
23

34
export type InsightsEventType = "click" | "conversion" | "view";
45
export type InsightsEvent = {
@@ -16,6 +17,9 @@ export type InsightsEvent = {
1617
filters?: string[];
1718
};
1819

20+
const USER_AGENT = encodeURIComponent(
21+
`Algolia insights for JavaScript (${version})`
22+
);
1923
/**
2024
* Sends data to endpoint
2125
* @param eventType InsightsEventType
@@ -104,9 +108,7 @@ export function sendEvent(
104108
throw new Error("expected either `objectIDs` or `filters` to be provided");
105109
}
106110

107-
bulkSendEvent(this._appId, this._apiKey, this._endpointOrigin, [
108-
event
109-
]);
111+
bulkSendEvent(this._appId, this._apiKey, this._endpointOrigin, [event]);
110112
}
111113

112114
function bulkSendEvent(
@@ -116,7 +118,7 @@ function bulkSendEvent(
116118
events: InsightsEvent[]
117119
) {
118120
// Auth query
119-
const reportingURL = `${endpointOrigin}/1/events?X-Algolia-Application-Id=${appId}&X-Algolia-API-Key=${apiKey}`;
121+
const reportingURL = `${endpointOrigin}/1/events?X-Algolia-Application-Id=${appId}&X-Algolia-API-Key=${apiKey}&X-Algolia-Agent=${USER_AGENT}`;
120122

121123
// Detect navigator support
122124
const supportsNavigator = navigator && isFunction(navigator.sendBeacon);

0 commit comments

Comments
 (0)