sajari-website
Advanced tools
Comparing version 0.5.11 to 0.6.0
{ | ||
"name": "sajari-website", | ||
"version": "0.5.11", | ||
"version": "0.6.0", | ||
"description": "Website extensions for the Sajari API. Automatically index site content, add user profiles, render search and recommendations, etc.", | ||
@@ -57,5 +57,6 @@ "author": { | ||
"sajari": "0.9.3", | ||
"superagent": "^1.6.1" | ||
"superagent": "^1.6.1", | ||
"js-base64": "^2.1.9" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -5,2 +5,5 @@ /* | ||
var cookie = require("./utils/cookie"); | ||
var encoder = require("./utils/encode"); | ||
var unique = require("./utils/unique"); | ||
var enc = new encoder(); | ||
var isArray = require('sajari/src/js/utils/isArray.js'); | ||
@@ -11,5 +14,10 @@ | ||
pname: 'sjPR', | ||
ename: 'sjPE', | ||
sname: 'sjSE', | ||
clicked: 'sjCL', | ||
expires: 365, // days | ||
maxStrings: 10, // max weighted profile strings to keep | ||
maxURLs: 10, // maximum number of url clicks to track | ||
weight: 0.05, // default contextual profile weight | ||
session: false, // if true, profiles are session based and not stored beyond | ||
domain: window.location.hostname.toLowerCase().replace(/^www(.)/, '') | ||
@@ -26,5 +34,14 @@ }; | ||
} | ||
if (options.meta === undefined) { | ||
this.meta = {}; | ||
if (options.data === undefined) { | ||
this.data = { | ||
meta: {}, | ||
weighted: [] | ||
}; | ||
} | ||
if (this.data.meta === undefined) { | ||
this.data.meta = {}; | ||
} | ||
if (this.data.weighted === undefined) { | ||
this.data.weighted = []; | ||
} | ||
this.gaId = getGAID(); | ||
@@ -48,6 +65,22 @@ | ||
load: function() { | ||
var p = getSession(opts.pname); | ||
if (p !== null) { | ||
this.meta = p; | ||
// MIGRATE OLD - REMOVE | ||
var p = getLegacySession(opts.pname); | ||
if (p) { | ||
this.data.meta = p; | ||
this.save(); | ||
} | ||
// END MIGRATE OLD | ||
var e; | ||
if (opts.session) { | ||
e = getSession(opts.ename); | ||
if (e) { | ||
this.data = e; | ||
} | ||
return; | ||
} | ||
e = getLocal(opts.ename); | ||
if (e) { | ||
this.data = e; | ||
} | ||
}, | ||
@@ -59,14 +92,36 @@ | ||
save: function() { | ||
setSession(opts.pname, this.meta); | ||
if (opts.session) { | ||
setSession(opts.ename, this.data); | ||
return; | ||
} | ||
setLocal(opts.ename, this.data); | ||
}, | ||
/** | ||
* Set a config option post creation | ||
*/ | ||
option: function(option, value) { | ||
if (option && value) { | ||
opts[option] = value; | ||
} | ||
}, | ||
/** | ||
* Set profile meta data | ||
* @param data - object | ||
*/ | ||
set: function(data) { | ||
for (var k in data) { | ||
this.meta[k] = data[k]; | ||
set: function(text, meta) { | ||
if (meta) { | ||
for (var k in meta) { | ||
this.data.meta[k] = meta[k]; | ||
} | ||
this.save(); | ||
} | ||
this.save(); | ||
if (text) { | ||
this.data.weighted.push(text); | ||
this.data.weighted = unique(this.data.weighted); | ||
if (this.data.weighted.length > this.maxStrings) { | ||
this.data.weighted.shift(); | ||
} | ||
this.save(); | ||
} | ||
}, | ||
@@ -77,4 +132,4 @@ | ||
*/ | ||
get: function(key) { | ||
return this.meta.key; | ||
get: function() { | ||
return this.data; | ||
}, | ||
@@ -88,3 +143,3 @@ | ||
var arr = getSession(opts.clicked); | ||
if (arr === null) { | ||
if (!arr) { | ||
arr = []; | ||
@@ -98,2 +153,5 @@ } | ||
arr.push(u[0]); | ||
if (arr.length > this.maxURLs) { | ||
arr.shift(); | ||
} | ||
setSession(opts.clicked, arr); | ||
@@ -107,3 +165,3 @@ }, | ||
var arr = getSession(opts.clicked); | ||
if (arr !== null) { | ||
if (arr) { | ||
return arr.slice(Math.max(arr.length - num, 1)); | ||
@@ -121,4 +179,4 @@ } | ||
} | ||
for (var key in this.meta) { | ||
var val = this.meta[key]; | ||
for (var key in this.data.meta) { | ||
var val = this.data.meta[key]; | ||
if (isArray(val)) { | ||
@@ -131,2 +189,7 @@ val = val.join(';'); | ||
} | ||
var n = this.data.weighted.length; | ||
for (var i = 0; i < n; i++) { | ||
var w = ((i + 1) / (n / 0.05)).toFixed(4); | ||
data['q[' + w + ']'] = this.data.weighted[i]; | ||
} | ||
return data; | ||
@@ -187,3 +250,4 @@ }, | ||
if (typeof(Storage) !== "undefined") { | ||
localStorage.setItem(name, JSON.stringify(data)); | ||
var e = enc.encode(data); | ||
localStorage.setItem(name, e); | ||
return true; | ||
@@ -198,3 +262,6 @@ } | ||
if (typeof(Storage) !== "undefined") { | ||
return JSON.parse(localStorage.getItem(name)); | ||
var d = localStorage.getItem(name); | ||
if (d) { | ||
return enc.decode(d); | ||
} | ||
} | ||
@@ -208,3 +275,4 @@ } | ||
if (typeof(Storage) !== "undefined") { | ||
sessionStorage.setItem(name, JSON.stringify(data)); | ||
var e = enc.encode(data); | ||
sessionStorage.setItem(name, e); | ||
return true; | ||
@@ -219,6 +287,21 @@ } | ||
if (typeof(Storage) !== "undefined") { | ||
return JSON.parse(sessionStorage.getItem(name)); | ||
var d = sessionStorage.getItem(name); | ||
if (d) { | ||
return enc.decode(d); | ||
} | ||
} | ||
} | ||
/* | ||
* Retrieve legacy data in session storage safely if it exists | ||
*/ | ||
function getLegacySession(name) { | ||
if (typeof(Storage) !== "undefined") { | ||
var d = sessionStorage.getItem(name); | ||
if (d) { | ||
return JSON.parse(d); | ||
} | ||
} | ||
} | ||
module.exports = profile; |
@@ -468,2 +468,12 @@ require("./utils/polyfills"); | ||
query.prototype.mergeProfile = function() { | ||
var data = profile.get(); | ||
log(data); | ||
// encode meta | ||
if (data) { | ||
this.attrs(profile.toArgs()); | ||
} | ||
return this; | ||
}; | ||
/** | ||
@@ -478,3 +488,3 @@ * Methods is a collection of functions that can be pushed onto the stack queue | ||
// Add new meta to the user profile | ||
profile.set(meta); | ||
profile.set(text, meta); | ||
@@ -499,2 +509,7 @@ var data = { | ||
// Set a new visitor ID | ||
profileOption: function(option, value) { | ||
profile.option(option, value); | ||
}, | ||
// Send a conversion with the given type and value | ||
@@ -640,6 +655,2 @@ conversion: function(type, value) { | ||
}); | ||
if (dom.hasNode('personalization')) { | ||
ongoing.attr('profile.query', 'true').attr('personalization', 'true'); | ||
} | ||
if (dom.hasNode('search-recent')) { | ||
@@ -708,2 +719,3 @@ ongoing.attr('recent', true); | ||
q.filterClicks(); | ||
q.mergeProfile(); | ||
q.attrs(dynamicArgs); | ||
@@ -739,3 +751,3 @@ | ||
el.parentNode.removeChild(el); | ||
}) | ||
}); | ||
@@ -758,4 +770,3 @@ processOptions(options); | ||
// Deprecated TODO: remove once token usage is 100% | ||
// Send click throughs | ||
// Store click throughs locally for filtering | ||
var qid = url.getURLParameter("q.id"); | ||
@@ -765,3 +776,2 @@ if (qid) { | ||
} | ||
// Deprecated TODO: remove once token usage is 100% | ||
@@ -921,6 +931,6 @@ // Profile user automatically unless we specify not to | ||
} | ||
profile.addClickedUrl(u); | ||
if (injected === "undefined") { | ||
injected = ""; | ||
} | ||
profile.addClickedUrl(u); | ||
api.pixel({ | ||
@@ -927,0 +937,0 @@ 'p.ga': profile.gaId, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67481
21
1847
6
+ Addedjs-base64@^2.1.9
+ Addedjs-base64@2.6.4(transitive)