Skip to content

Commit

Permalink
Update campaign logic (#93)
Browse files Browse the repository at this point in the history
* update campaign logic
  • Loading branch information
xinghengwang authored Jan 5, 2024
1 parent 4ebf471 commit fc71e33
Show file tree
Hide file tree
Showing 12 changed files with 698 additions and 609 deletions.
164 changes: 85 additions & 79 deletions build/moesif.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ define(function () { 'use strict';

var Config = {
DEBUG: false,
LIB_VERSION: '1.8.12'
LIB_VERSION: '1.8.13'
};

// since es6 imports are static and we run unit tests from the console, window won't be defined when importing this file
Expand Down Expand Up @@ -2437,8 +2437,8 @@ define(function () { 'use strict';
STORED_COMPANY_ID: 'moesif_stored_company_id',
STORED_SESSION_ID: 'moesif_stored_session_id',
STORED_ANONYMOUS_ID: 'moesif_anonymous_id',
STORED_CAMPAIGN_DATA: 'moesif_campaign_data',
STORED_INITIAL_CAMPAIGN_DATA: 'moesif_initial_campaign'
STORED_CAMPAIGN_DATA_USER: 'moesif_campaign_data',
STORED_CAMPAIGN_DATA_COMPANY: 'moesif_campaign_company'
};

function replacePrefix(key, prefix) {
Expand Down Expand Up @@ -2530,7 +2530,10 @@ define(function () { 'use strict';
_.cookie.remove(replacePrefix(STORAGE_CONSTANTS.STORED_ANONYMOUS_ID, prefix));
_.cookie.remove(replacePrefix(STORAGE_CONSTANTS.STORED_SESSION_ID, prefix));
_.cookie.remove(
replacePrefix(STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA, prefix)
replacePrefix(STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA_USER, prefix)
);
_.cookie.remove(
replacePrefix(STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA_COMPANY, prefix)
);
}

Expand All @@ -2549,7 +2552,10 @@ define(function () { 'use strict';
replacePrefix(STORAGE_CONSTANTS.STORED_SESSION_ID, prefix)
);
_.localStorage.remove(
replacePrefix(STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA, prefix)
replacePrefix(STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA_USER, prefix)
);
_.localStorage.remove(
replacePrefix(STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA_COMPANY, prefix)
);
}

Expand Down Expand Up @@ -2591,87 +2597,83 @@ define(function () { 'use strict';
}
}

// since identify company can happen a lot later
// than initial anonymous users
// persist the very initial campaign data for
// companies until company is identified.
function getStoredInitialCampaignData(opt) {
var storedCampaignData = null;
var storedCampaignString = null;
try {
storedCampaignString = getFromPersistence(STORAGE_CONSTANTS.STORED_INITIAL_CAMPAIGN_DATA, opt);
if (storedCampaignString) {
storedCampaignData = _.JSONDecode(storedCampaignString);
}
} catch (err) {
logger$4.error('failed to decode company campaign data ' + storedCampaignString);
logger$4.error(err);
function hasData(data) {
if (data && !_.isEmptyObject(data)) {
return true;
}

return storedCampaignData;
return false;
}

function mergeCampaignData(saved, current) {
if (!current) {
return saved;
}

if (!saved) {
return current;
}

var result = _.extend({}, saved, current);

// if utm source exists.
// every field of UTM will be override to be
// consistent.
if (current && current[UTMConstants.UTM_SOURCE]) {
for (var prop in UTMConstants) {
result[UTMConstants[prop]] = current[UTMConstants[prop]];
function saveEncodeIfHasData(persist, storageKey, data) {
try {
if (hasData(data)) {
var dataString = _.JSONEncode(data);
persist(storageKey, dataString);
}
} catch (err) {
logger$4.error('failed to decode campaign data');
logger$4.error(err);
}

return result;
}

function getCampaignData(persist, opt) {
var storedCampaignData = null;
var storedCampaignString = null;
function getAndDecode(storageKey, opt) {
try {
storedCampaignString = getFromPersistence(STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA, opt);
if (storedCampaignString && storedCampaignString !== 'null') {
storedCampaignData = _.JSONDecode(storedCampaignString);
var stringVal = getFromPersistence(storageKey, opt);
if (stringVal && stringVal !== 'null') {
var data = _.JSONDecode(stringVal);
if (hasData(data)) {
return data;
}
return null;
}
} catch (err) {
logger$4.error('failed to decode campaign data ' + storedCampaignString);
logger$4.error('failed to persist campaign data');
logger$4.error(err);
return null;
}
}

var currentCampaignData = getCampaignDataFromUrlOrCookie(opt);
logger$4.log('current campaignData');
logger$4.log(_.JSONEncode(currentCampaignData));
function storeForOneEntityIfNotSavedYet(
persist,
opt,
entityStorageKey,
currentCampaignData
) {
var storedData = getAndDecode(entityStorageKey, opt);
if (!storedData && persist) {
// no stored data thus store current.
saveEncodeIfHasData(persist, entityStorageKey, currentCampaignData);
}
}

var merged = mergeCampaignData(storedCampaignData, currentCampaignData);
logger$4.log('merged campaignData');
logger$4.log(_.JSONEncode(merged));
// this stores Campaign data if not saved yet.
function storeCampaignDataIfNeeded(persist, opt, currentCampaignData) {
storeForOneEntityIfNotSavedYet(persist, opt, STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA_USER, currentCampaignData);
storeForOneEntityIfNotSavedYet(persist, opt, STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA_COMPANY, currentCampaignData);
}

try {
if (persist && merged && !_.isEmptyObject(merged)) {
var mergedString = _.JSONEncode(merged);
persist(STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA, mergedString);

// UTM_SOURCE exists means that merged campaign info have data.
if (!storedCampaignData && merged[UTMConstants.UTM_SOURCE]) {
// first time we persist campaign data, and thus persis the initial data until identifyCompany is called
persist(STORAGE_CONSTANTS.STORED_INITIAL_CAMPAIGN_DATA, mergedString);
}
function popStoredCampaignData(persist, opt, storageKey) {
var storedCampaignData = getAndDecode(storageKey, opt);

if (storedCampaignData) {
// let's delete it
// we know we will use it.
// so next one can overridee
try {
persist(storageKey, '');
} catch (err) {
logger$4.error('failed to clear campaign data');
}
} catch (err) {
logger$4.error('failed to persist campaign data');
logger$4.error(err);
}
return storedCampaignData;
}

return merged;
function popStoredCampaignDataForUser(persist, opt) {
return popStoredCampaignData(persist, opt, STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA_USER);
}

function popStoredCampaignDataForCompany(persist, opt) {
return popStoredCampaignData(persist, opt, STORAGE_CONSTANTS.STORED_CAMPAIGN_DATA_COMPANY);
}

// eslint-disable-line
Expand Down Expand Up @@ -3434,16 +3436,22 @@ define(function () { 'use strict';
this._session = getFromPersistence(STORAGE_CONSTANTS.STORED_SESSION_ID, ops);
this._companyId = getFromPersistence(STORAGE_CONSTANTS.STORED_COMPANY_ID, ops);
this._anonymousId = getAnonymousId(this._persist, ops);
this._campaign = getCampaignData(this._persist, ops);
this._currentCampaign = getCampaignDataFromUrlOrCookie(ops);

if (this._currentCampaign) {
storeCampaignDataIfNeeded(this._persist, ops, this._currentCampaign);
}

// this._campaign = getCampaignData(this._persist, ops);

// try to save campaign data on anonymous id
// if there is no userId saved, means it is still anonymous.
// later on, when identifyUser is called with real user id,
// the campaigne data will be resent with that again.
if (this._campaign && !this._userId) {
if (this._currentCampaign && !this._userId) {
var anonUserObject = {};
anonUserObject['anonymous_id'] = this._anonymousId;
anonUserObject['campaign'] = this._campaign;
anonUserObject['campaign'] = this._currentCampaign;
this.updateUser(anonUserObject, this._options.applicationId, this._options.host, this._options.callback);
}
} catch(err) {
Expand Down Expand Up @@ -3701,8 +3709,10 @@ define(function () { 'use strict';
userObject['session_token'] = this._session;
}

if (this._campaign) {
userObject['campaign'] = this._campaign;
var campaignData = popStoredCampaignDataForUser(this._persist, this._options) || this._currentCampaign;

if (campaignData) {
userObject['campaign'] = campaignData;
}

if (this._companyId) {
Expand Down Expand Up @@ -3734,8 +3744,6 @@ define(function () { 'use strict';
return;
}

var hasCompanyIdentifiedBefore = !!this._companyId;

this._companyId = companyId;
if (!(this._options && this._options.applicationId)) {
throw new Error('Init needs to be called with a valid application Id before calling identify User.');
Expand All @@ -3755,9 +3763,7 @@ define(function () { 'use strict';
companyObject['session_token'] = this._session;
}

var campaignData = hasCompanyIdentifiedBefore
? this._campaign
: getStoredInitialCampaignData(this._options) || this._campaign;
var campaignData = popStoredCampaignDataForCompany(this._persist, this._options) || this._currentCampaign;

if (campaignData) {
companyObject['campaign'] = campaignData;
Expand Down Expand Up @@ -3934,7 +3940,7 @@ define(function () { 'use strict';
this._companyId = null;
this._userId = null;
this._session = null;
this._campaign = null;
this._currentCampaign = null;
}
};
}
Expand Down
Loading

0 comments on commit fc71e33

Please sign in to comment.