Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
feat(insights): add getInsightsAnonymousUserToken helper (#2887)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrugg committed Jan 10, 2020
1 parent 1cb1b33 commit b5fe4f7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@
},
{
"path": "packages/react-instantsearch/dist/umd/Dom.min.js",
"maxSize": "33.5 kB"
"maxSize": "33.75 kB"
},
{
"path": "packages/react-instantsearch-core/dist/umd/ReactInstantSearchCore.min.js",
"maxSize": "25 kB"
},
{
"path": "packages/react-instantsearch-dom/dist/umd/ReactInstantSearchDOM.min.js",
"maxSize": "36 kB"
"maxSize": "36.25 kB"
},
{
"path": "packages/react-instantsearch-dom-maps/dist/umd/ReactInstantSearchDOMMaps.min.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import getInsightsAnonymousUserToken, {
ANONYMOUS_TOKEN_COOKIE_KEY,
} from '../getInsightsAnonymousUserToken';

const DAY = 86400000; /* 1 day in ms*/
const DATE_TOMORROW = new Date(Date.now() + DAY).toUTCString();
const DATE_YESTERDAY = new Date(Date.now() - DAY).toUTCString();

const resetCookie = cookieKey => {
document.cookie = `${cookieKey}=;expires=Thu, 01-Jan-1970 00:00:01 GMT;`;
};

describe('getInsightsAnonymousUserToken', () => {
beforeEach(() => {
resetCookie(ANONYMOUS_TOKEN_COOKIE_KEY);
});

it('should return undefined when no cookies', () => {
expect(getInsightsAnonymousUserToken()).toBe(undefined);
});

it('should return undefined when cookie present but expired', () => {
document.cookie = `${ANONYMOUS_TOKEN_COOKIE_KEY}=anonymous-uuid;expires=${DATE_YESTERDAY};`;
expect(getInsightsAnonymousUserToken()).toBe(undefined);
});

it('should return the anonymous uuid when cookie present and valid', () => {
document.cookie = `${ANONYMOUS_TOKEN_COOKIE_KEY}=anonymous-uuid;expires=${DATE_TOMORROW};`;
expect(getInsightsAnonymousUserToken()).toBe('anonymous-uuid');
});

it('should return the anonymous uuid when other cookies are invalid', () => {
document.cookie = `${ANONYMOUS_TOKEN_COOKIE_KEY}=anonymous-uuid;expires=${DATE_TOMORROW};`;
document.cookie = `BAD_COOKIE=val%ue;expires=${DATE_TOMORROW};path=/`;
expect(getInsightsAnonymousUserToken()).toBe('anonymous-uuid');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const ANONYMOUS_TOKEN_COOKIE_KEY = '_ALGOLIA';

function getCookie(name: string): string | undefined {
const prefix = `${name}=`;
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
let cookie = cookies[i];
while (cookie.charAt(0) === ' ') {
cookie = cookie.substring(1);
}
if (cookie.indexOf(prefix) === 0) {
return cookie.substring(prefix.length, cookie.length);
}
}
return undefined;
}

export default function getInsightsAnonymousUserToken(): string | undefined {
return getCookie(ANONYMOUS_TOKEN_COOKIE_KEY);
}
5 changes: 5 additions & 0 deletions packages/react-instantsearch-dom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ export { createClassNames } from './core/utils';

// voiceSearchHelper
export { default as createVoiceSearchHelper } from './lib/voiceSearchHelper';

// insights
export {
default as getInsightsAnonymousUserToken,
} from './core/getInsightsAnonymousUserToken';
1 change: 1 addition & 0 deletions packages/react-instantsearch/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export { Snippet } from 'react-instantsearch-dom';
export { SortBy } from 'react-instantsearch-dom';
export { Stats } from 'react-instantsearch-dom';
export { ToggleRefinement } from 'react-instantsearch-dom';
export { getInsightsAnonymousUserToken } from 'react-instantsearch-dom';

0 comments on commit b5fe4f7

Please sign in to comment.