Skip to content

Commit

Permalink
feat(UserAgent): introduce addAlgoliaAgent method (#105)
Browse files Browse the repository at this point in the history
* feat(UserAgent): introduce addAlgoliaAgent method

This introduces a addAlgoliaAgent method that can be also found in the
search client.

```ts
aa('addAlgoliaAgent', 'a random string')
```

* feat(UserAgent): add space after ;
  • Loading branch information
tkrugg authored Apr 10, 2019
1 parent 965da06 commit 927e74e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
40 changes: 40 additions & 0 deletions lib/__tests__/_algoliaAgent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import AlgoliaInsights from "../insights";

jest.mock("../../package.json", () => ({ version: "1.0.1" }));

describe("algoliaAgent", () => {
beforeEach(() => {
AlgoliaInsights.init({ apiKey: "test", appId: "test" });
});

it("should initialize the client with a default algoliaAgent string", () => {
expect(AlgoliaInsights._ua).toEqual(
"Algolia insights for JavaScript (1.0.1)"
);
expect(AlgoliaInsights._uaURIEncoded).toEqual(
"Algolia%20insights%20for%20JavaScript%20(1.0.1)"
);
});

it("should allow adding a string to algoliaAgent", () => {
AlgoliaInsights.addAlgoliaAgent("other string");
expect(AlgoliaInsights._ua).toEqual(
"Algolia insights for JavaScript (1.0.1); other string"
);
expect(AlgoliaInsights._uaURIEncoded).toEqual(
"Algolia%20insights%20for%20JavaScript%20(1.0.1)%3B%20other%20string"
);
});

it("should duplicate a string when added twice", () => {
AlgoliaInsights.addAlgoliaAgent("duplicated string");
AlgoliaInsights.addAlgoliaAgent("duplicated string");

expect(AlgoliaInsights._ua).toEqual(
"Algolia insights for JavaScript (1.0.1); duplicated string"
);
expect(AlgoliaInsights._uaURIEncoded).toEqual(
"Algolia%20insights%20for%20JavaScript%20(1.0.1)%3B%20duplicated%20string"
);
});
});
10 changes: 10 additions & 0 deletions lib/_algoliaAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { version } from "../package.json";

export const DEFAULT_ALGOLIA_AGENT = `Algolia insights for JavaScript (${version})`;

export function addAlgoliaAgent(algoliaAgent) {
if (this._ua.indexOf(`; ${algoliaAgent}`) === -1) {
this._ua += `; ${algoliaAgent}`;
this._uaURIEncoded = encodeURIComponent(this._ua);
}
}
9 changes: 3 additions & 6 deletions lib/_sendEvent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isNumber, isUndefined, isString, isFunction } from "./utils";
import { version } from "../package.json";

export type InsightsEventType = "click" | "conversion" | "view";
export type InsightsEvent = {
Expand All @@ -17,9 +16,6 @@ export type InsightsEvent = {
filters?: string[];
};

const USER_AGENT = encodeURIComponent(
`Algolia insights for JavaScript (${version})`
);
/**
* Sends data to endpoint
* @param eventType InsightsEventType
Expand Down Expand Up @@ -108,17 +104,18 @@ export function sendEvent(
throw new Error("expected either `objectIDs` or `filters` to be provided");
}

bulkSendEvent(this._appId, this._apiKey, this._endpointOrigin, [event]);
bulkSendEvent(this._appId, this._apiKey, this._uaURIEncoded, this._endpointOrigin, [event]);
}

function bulkSendEvent(
appId: string,
apiKey: string,
userAgent: string,
endpointOrigin: string,
events: InsightsEvent[]
) {
// Auth query
const reportingURL = `${endpointOrigin}/1/events?X-Algolia-Application-Id=${appId}&X-Algolia-API-Key=${apiKey}&X-Algolia-Agent=${USER_AGENT}`;
const reportingURL = `${endpointOrigin}/1/events?X-Algolia-Application-Id=${appId}&X-Algolia-API-Key=${apiKey}&X-Algolia-Agent=${userAgent}`;

// Detect navigator support
const supportsNavigator = navigator && isFunction(navigator.sendBeacon);
Expand Down
5 changes: 5 additions & 0 deletions lib/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isUndefined, isString, isNumber } from "./utils";
import { DEFAULT_ALGOLIA_AGENT } from "./_algoliaAgent";

type InsightRegion = "de" | "us";
const SUPPORTED_REGIONS: InsightRegion[] = ["de", "us"];
Expand Down Expand Up @@ -66,4 +67,8 @@ export function init(options: InitParams) {
: 6 * MONTH;
// Set hasCredentials
this._hasCredentials = true;

// user agent
this._ua = DEFAULT_ALGOLIA_AGENT;
this._uaURIEncoded = encodeURIComponent(DEFAULT_ALGOLIA_AGENT);
}
10 changes: 10 additions & 0 deletions lib/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { sendEvent, InsightsEventType, InsightsEvent } from "./_sendEvent";

import { InitParams, init } from "./init";
import { initSearch, InitSearchParams } from "./_initSearch";
import { addAlgoliaAgent } from "./_algoliaAgent";

import {
InsightsSearchClickEvent,
clickedObjectIDsAfterSearch,
Expand Down Expand Up @@ -66,6 +68,10 @@ class AlgoliaAnalytics {
_userHasOptedOut: boolean;
_cookieDuration: number;

// user agent
_ua: string = "";
_uaURIEncoded: string = "";

version: string = version;

// Private methods
Expand All @@ -80,6 +86,8 @@ class AlgoliaAnalytics {
public init: (params: InitParams) => void;
public initSearch: (params: InitSearchParams) => void;

public addAlgoliaAgent: (algoliaAgent: string) => void;

public ANONYMOUS_USER_TOKEN: string;
public setUserToken: (userToken: string) => void;
public getUserToken: () => string;
Expand Down Expand Up @@ -111,6 +119,8 @@ class AlgoliaAnalytics {
this.init = init.bind(this);
this.initSearch = initSearch.bind(this);

this.addAlgoliaAgent = addAlgoliaAgent.bind(this);

this.ANONYMOUS_USER_TOKEN = ANONYMOUS_USER_TOKEN;
this.setUserToken = setUserToken.bind(this);
this.getUserToken = getUserToken.bind(this);
Expand Down

0 comments on commit 927e74e

Please sign in to comment.