Skip to content

Commit

Permalink
addin cookie support
Browse files Browse the repository at this point in the history
  • Loading branch information
sayan-rudder committed Dec 6, 2019
1 parent d55e56c commit 80adbb4
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 48 deletions.
81 changes: 79 additions & 2 deletions rudder-client-javascript/analytics/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions rudder-client-javascript/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
"author": "",
"license": "ISC",
"dependencies": {
"@ndhoule/clone": "^1.0.0",
"@ndhoule/defaults": "^2.0.1",
"@segment/localstorage-retry": "^1.2.2",
"@segment/store": "^1.3.20",
"@segment/top-domain": "^3.0.0",
"btoa": "^1.2.1",
"component-cookie": "^1.1.4",
"json3": "^3.3.3",
"universal-analytics": "^0.4.20",
"xmlhttprequest": "^1.8.0"
},
Expand Down
38 changes: 18 additions & 20 deletions rudder-client-javascript/analytics/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
output: [
{
file:
process.env.ENV == "prod" ? "dist/browser.min.js" : "dist/browser.js",
process.env.ENV == "prod" ? "dist/rudder.min.js" : "dist/browser.js",
format: "iife",
name: "rudderanalytics",
sourceMap: true,
Expand Down Expand Up @@ -55,25 +55,23 @@ export default {
}),
process.env.uglify === "true" && terser(),
process.env.ENV == "prod" &&
obfuscatorPlugin(
{
compact: true,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false,
disableConsoleOutput: false,
identifierNamesGenerator: 'mangled',
log: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: false,
sourceMap: false,
stringArray: false,
stringArrayEncoding: false,
transformObjectKeys: false,
unicodeEscapeSequence: false
}
)
obfuscatorPlugin({
compact: true,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false,
disableConsoleOutput: false,
identifierNamesGenerator: "mangled",
log: false,
renameGlobals: false,
rotateStringArray: true,
selfDefending: false,
sourceMap: false,
stringArray: false,
stringArrayEncoding: false,
transformObjectKeys: false,
unicodeEscapeSequence: false
})
//process.env.uglify === "true" && uglify()
]
};
87 changes: 87 additions & 0 deletions rudder-client-javascript/analytics/utils/storage/cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import clone from "@ndhoule/clone";
import cookie from "component-cookie";
import defaults from "@ndhoule/defaults";
import json from "json3";
import topDomain from "@segment/top-domain";

/**
* An object utility to persist values in cookies
*/
class CookieLocal {
constructor(options) {
this._options = {};
this.options(options);
}

/**
*
* @param {*} options
*/
options(options = {}) {
if (arguments.length === 0) return this._options;

let domain = "." + topDomain(window.location.href);
if (domain === ".") domain = null;

// the default maxage and path
this._options = defaults(options, {
maxage: 31536000000,
path: "/",
domain: domain
});

//try setting a cookie first
this.set("test_rudder", true);
if (!this.get("test_rudder")) {
this._options.domain = null;
}
this.remove("test_rudder");
}

/**
*
* @param {*} key
* @param {*} value
*/
set(key, value) {
try {
value = json.stringify(value);
cookie(key, value, clone(this._options));
return true;
} catch (e) {
return false;
}
}

/**
*
* @param {*} key
*/
get(key) {
try {
let value = cookie(key);
value = value ? json.parse(value) : null;
return value;
} catch (e) {
return null;
}
}

/**
*
* @param {*} key
*/
remove(key) {
try {
cookie(key, null, clone(this._options));
return true;
} catch (e) {
return false;
}
}
}

// Exporting only the instance
let Cookie = new CookieLocal({});

export { Cookie };

0 comments on commit 80adbb4

Please sign in to comment.