Skip to content

Commit

Permalink
Merge pull request #313 from lytics/issue/support-jstag-3
Browse files Browse the repository at this point in the history
update references to lio to fallback to jstag when not found
  • Loading branch information
markhayden authored Jan 21, 2019
2 parents ab0e2e9 + e4c5bda commit b9a84b2
Show file tree
Hide file tree
Showing 31 changed files with 819 additions and 227 deletions.
74 changes: 60 additions & 14 deletions dist/pathfora.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,10 +912,20 @@
*/
function addCallback (cb) {
if (window.lio && window.lio.loaded) {
// legacy
cb(window.lio.data);
} else {
this.callbacks.push(cb);
return;
} else if (window.jstag && typeof window.jstag.getEntity === 'function') {
// > jstag 3.0.0
var entity = window.jstag.getEntity();
if (entity.data && entity.data.user) {
cb(entity.data.user);
return;
}
}

// fallback
this.callbacks.push(cb);
}

/** @module pathfora/display-conditions/pageviews/init-pageviews */
Expand Down Expand Up @@ -2998,8 +3008,13 @@
*/
function getUserSegments () {
if (window.lio && window.lio.data && window.lio.data.segments) {
// legacy
return window.lio.data.segments;
} else if (window.jstag && typeof window.jstag.getSegments === 'function') {
// > jstag 3.0.0
return window.jstag.getSegments();
} else {
// fallback
return ['all'];
}
}
Expand Down Expand Up @@ -3038,19 +3053,34 @@
* @params {object} pf
*/
function validateAccountId (pf) {
var acctid;

// in the legacy javascript tag < 2.0, there is an lio object surfaced that holds the account id.
// in > 3.0 this lio object is only available for backwards compatibility and not the main source
// of truth. we should be getting the cid that is passed to the config, which is an array, by default
// we can assume the first cid in the array is the one to be used for personalization and such.
if (typeof pf.acctid === 'undefined' || pf.acctid === '') {
if (window.lio && window.lio.account) {
if (
typeof window.lio.account.id === 'undefined' ||
window.lio.account.id === ''
) {
throw new Error('Lytics Javascript tag returned an empty account id.');
}

pf.acctid = window.lio.account.id;
// tag is legacy
acctid = window.lio.account.id;
} else if (
// tag is current gen
window.jstag &&
window.jstag.config &&
window.jstag.config.cid &&
window.jstag.config.cid.length > 0
) {
acctid = window.jstag.config.cid[0];
} else {
throw new Error('Could not get account id from Lytics Javascript tag.');
}

// make sure we have a valid acctid before setting
if (!!acctid) {
pf.acctid = acctid;
} else {
throw new Error('Lytics Javascript tag returned an empty account id.');
}
}
}

Expand Down Expand Up @@ -3619,8 +3649,8 @@
// for each template found...
for (var f = 0; f < found.length; f++) {
// parse the field name
var dataval = found[f].slice(2).slice(0, -2),
parts = dataval.split('|'),
var foundval = found[f].slice(2).slice(0, -2),
parts = foundval.split('|'),
def = '';

// get the default (fallback) value
Expand All @@ -3631,9 +3661,25 @@
// check for subfields if the value is an object
var split = parts[0].trim().split('.');

dataval = window.lio.data;
var s;
// get entity data from tag
var dataval;

// for the legacy tag < 3.0, there is a lio object surfaced. within this object lives the personalization
// data. however, in current gen tag > 3.0 we have a getEntity() method that should be used as the source
// of truth, the returned data model is slightly different in that it supports the full personalization
// api vs the legacy entity api that only returns segment and user field info.
if (window.lio && window.lio.data) {
dataval = window.lio.data;
// tag is legacy
} else if (window.jstag && typeof window.jstag.getEntity === 'function') {
// tag is current gen
var entity = window.jstag.getEntity();
if (entity && entity.data && entity.data.user) {
dataval = entity.data.user;
}
}

var s;
for (s = 0; s < split.length; s++) {
if (typeof dataval !== 'undefined') {
dataval = dataval[split[s]];
Expand Down
2 changes: 1 addition & 1 deletion dist/pathfora.min.js

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions src/rollup/callbacks/add-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ import window from '../dom/window';
*/
export default function addCallback (cb) {
if (window.lio && window.lio.loaded) {
// legacy
cb(window.lio.data);
} else {
this.callbacks.push(cb);
return;
} else if (window.jstag && typeof window.jstag.getEntity === 'function') {
// > jstag 3.0.0
var entity = window.jstag.getEntity();
if (entity.data && entity.data.user) {
cb(entity.data.user);
return;
}
}

// fallback
this.callbacks.push(cb);
}
5 changes: 5 additions & 0 deletions src/rollup/data/segments/get-user-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import window from '../../dom/window';
*/
export default function getUserSegments () {
if (window.lio && window.lio.data && window.lio.data.segments) {
// legacy
return window.lio.data.segments;
} else if (window.jstag && typeof window.jstag.getSegments === 'function') {
// > jstag 3.0.0
return window.jstag.getSegments();
} else {
// fallback
return ['all'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export default function replaceEntityField (
// for each template found...
for (var f = 0; f < found.length; f++) {
// parse the field name
var dataval = found[f].slice(2).slice(0, -2),
parts = dataval.split('|'),
var foundval = found[f].slice(2).slice(0, -2),
parts = foundval.split('|'),
def = '';

// get the default (fallback) value
Expand All @@ -55,9 +55,25 @@ export default function replaceEntityField (
// check for subfields if the value is an object
var split = parts[0].trim().split('.');

dataval = window.lio.data;
var s;
// get entity data from tag
var dataval;

// for the legacy tag < 3.0, there is a lio object surfaced. within this object lives the personalization
// data. however, in current gen tag > 3.0 we have a getEntity() method that should be used as the source
// of truth, the returned data model is slightly different in that it supports the full personalization
// api vs the legacy entity api that only returns segment and user field info.
if (window.lio && window.lio.data) {
dataval = window.lio.data;
// tag is legacy
} else if (window.jstag && typeof window.jstag.getEntity === 'function') {
// tag is current gen
var entity = window.jstag.getEntity();
if (entity && entity.data && entity.data.user) {
dataval = entity.data.user;
}
}

var s;
for (s = 0; s < split.length; s++) {
if (typeof dataval !== 'undefined') {
dataval = dataval[split[s]];
Expand Down
31 changes: 23 additions & 8 deletions src/rollup/validation/validate-account-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,33 @@ import window from '../dom/window';
* @params {object} pf
*/
export default function validateAccountId (pf) {
var acctid;

// in the legacy javascript tag < 2.0, there is an lio object surfaced that holds the account id.
// in > 3.0 this lio object is only available for backwards compatibility and not the main source
// of truth. we should be getting the cid that is passed to the config, which is an array, by default
// we can assume the first cid in the array is the one to be used for personalization and such.
if (typeof pf.acctid === 'undefined' || pf.acctid === '') {
if (window.lio && window.lio.account) {
if (
typeof window.lio.account.id === 'undefined' ||
window.lio.account.id === ''
) {
throw new Error('Lytics Javascript tag returned an empty account id.');
}

pf.acctid = window.lio.account.id;
// tag is legacy
acctid = window.lio.account.id;
} else if (
// tag is current gen
window.jstag &&
window.jstag.config &&
window.jstag.config.cid &&
window.jstag.config.cid.length > 0
) {
acctid = window.jstag.config.cid[0];
} else {
throw new Error('Could not get account id from Lytics Javascript tag.');
}

// make sure we have a valid acctid before setting
if (!!acctid) {
pf.acctid = acctid;
} else {
throw new Error('Lytics Javascript tag returned an empty account id.');
}
}
}
Loading

0 comments on commit b9a84b2

Please sign in to comment.