Skip to content

Commit

Permalink
Merge pull request #178 from keen/akasprowicz-localstorage-fix
Browse files Browse the repository at this point in the history
fix for #177
  • Loading branch information
adamkasprowicz authored Jul 1, 2019
2 parents a00969a + 6998b18 commit 0483814
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { deepExtend } from './utils/deepExtend';
import { serializeForm } from './utils/serializeForm';
import { timer } from './utils/timer';
import { setOptOut } from './utils/optOut';
import { isLocalStorageAvailable } from './utils/localStorage';

// ------------------------
// Methods
Expand Down Expand Up @@ -101,7 +102,7 @@ if (typeof webpackKeenGlobals !== 'undefined') {
keenGlobals = webpackKeenGlobals;
}

if (localStorage && localStorage.getItem('optout')) {
if (isLocalStorageAvailable && localStorage.getItem('optout')) {
KeenCore.optedOut = true;
}

Expand Down
12 changes: 12 additions & 0 deletions lib/utils/localStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function lsTest(){
const test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch(e) {
return false;
}
}

export const isLocalStorageAvailable = lsTest();
4 changes: 4 additions & 0 deletions lib/utils/optOut.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { isLocalStorageAvailable } from './localStorage';

export function setOptOut(optOut = true) {
if (!isLocalStorageAvailable) return;

if (optOut) {
localStorage.setItem('optout', optOut);
return;
Expand Down
22 changes: 21 additions & 1 deletion test/demo/demo-tests.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
const demoTests = (demoConfig, Keen) => {

//demoConfig.requestType = 'beaconAPI';
// demoConfig.requestType = 'beaconAPI';

// Keen.debug = true;
const client = new Keen(demoConfig);

client
.recordEvent('beacon_purchases', {
item: 'Avocado',
number_of_items: 10,
user: {
name: 'John Smith'
}
});
/*
.then((response) => {
console.log(response);
// handle successful responses
})
.catch(error => {
console.log(error);
// handle errors
});
*/
return;

client.recordEvent({
collection: 'abc',
event: {
Expand Down

0 comments on commit 0483814

Please sign in to comment.