Skip to content

Commit

Permalink
fix: do not store anonymous token in cookie when user has opted out (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunjae Lee authored Oct 29, 2020
1 parent abae6fd commit 8669b67
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/__tests__/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import AlgoliaAnalytics from "../insights";
import * as utils from "../utils";
import { getCookie } from "../_cookieUtils";

describe("init", () => {
let analyticsInstance;
beforeEach(() => {
analyticsInstance = new AlgoliaAnalytics({ requestFn: () => {} });
document.cookie = `_ALGOLIA=;${new Date().toUTCString()};path=/`;
});

it("should throw if no parameters is passed", () => {
Expand Down Expand Up @@ -63,6 +65,15 @@ describe("init", () => {
});
expect(analyticsInstance._userHasOptedOut).toBe(true);
});
it("should not set anonymous user token when _userHasOptedOut is true", () => {
analyticsInstance.init({
apiKey: "***",
appId: "XXX",
userHasOptedOut: true
});
expect(analyticsInstance._userToken).toBeUndefined();
expect(getCookie("_ALGOLIA")).toBe("");
});
it("should use 6 months cookieDuration by default", () => {
analyticsInstance.init({ apiKey: "***", appId: "XXX" });
const month = 30 * 24 * 60 * 60 * 1000;
Expand Down
2 changes: 1 addition & 1 deletion lib/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function init(options: InitParams) {
this._ua = DEFAULT_ALGOLIA_AGENT;
this._uaURIEncoded = encodeURIComponent(DEFAULT_ALGOLIA_AGENT);

if (supportsCookies()) {
if (!this._userHasOptedOut && supportsCookies()) {
this.setUserToken(this.ANONYMOUS_USER_TOKEN);
}
}

0 comments on commit 8669b67

Please sign in to comment.