Skip to content

Commit 40a32b8

Browse files
committed
added storage of split-test data
1 parent 8394a41 commit 40a32b8

File tree

5 files changed

+117
-52
lines changed

5 files changed

+117
-52
lines changed

dist/breinify-api.js

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14780,15 +14780,10 @@ dependencyScope.jQuery = $;;
1478014780
storage: {
1478114781
instance: null,
1478214782

14783-
init: function (entries, reset, callback) {
14783+
init: function (entries, callback) {
1478414784
if (!$.isPlainObject(entries)) {
1478514785
return;
1478614786
}
14787-
// we allow also to not specify reset
14788-
else if ($.isFunction(reset) && typeof callback === 'undefined') {
14789-
callback = reset;
14790-
reset = false;
14791-
}
1479214787

1479314788
// check if we already have an instance (init may be called multiple times)
1479414789
if (this.instance === null) {
@@ -14816,10 +14811,6 @@ dependencyScope.jQuery = $;;
1481614811
}
1481714812
}
1481814813

14819-
if (reset) {
14820-
this.instance.clear();
14821-
}
14822-
1482314814
var _self = this;
1482414815
var toBeLoaded = {};
1482514816
var loadingStatus = [];
@@ -15587,7 +15578,7 @@ dependencyScope.jQuery = $;;
1558715578
} else {
1558815579
instance.set(attribute, value);
1558915580
}
15590-
})
15581+
});
1559115582
},
1559215583

1559315584
validate: function () {
@@ -15720,8 +15711,34 @@ dependencyScope.jQuery = $;;
1572015711

1572115712
var _privates = {
1572215713
ready: false,
15714+
splitTestData: null,
15715+
15716+
getSplitTestData: function () {
15717+
if (this.splitTestData !== null) {
15718+
return this.splitTestData;
15719+
}
1572315720

15724-
storeSplitTest: function(data) {
15721+
this.splitTestData = Breinify.UTL.storage.get('splitTestData');
15722+
if (this.splitTestData === null || !$.isPlainObject(this.splitTestData)) {
15723+
this.splitTestData = {};
15724+
return this.splitTestData;
15725+
}
15726+
15727+
// clean-up old split-test information
15728+
for (var key in this.splitTestData) {
15729+
if (!this.splitTestData.hasOwnProperty(key)) {
15730+
continue;
15731+
}
15732+
15733+
var lastUpdated = this.splitTestData[key].lastUpdated;
15734+
if (typeof lastUpdated !== number || lastUpdated < -1) {
15735+
delete this.splitTestData[key];
15736+
}
15737+
}
15738+
return this.splitTestData;
15739+
},
15740+
15741+
storeAdditionalData: function (data) {
1572515742
var additionalData;
1572615743
if (!$.isPlainObject(data)) {
1572715744
return;
@@ -15734,22 +15751,43 @@ dependencyScope.jQuery = $;;
1573415751
}
1573515752
}
1573615753
} else if ($.isPlainObject(data.additionalData)) {
15737-
additionalData = [ data.additionalData ];
15754+
additionalData = [data.additionalData];
1573815755
} else {
1573915756
return;
1574015757
}
1574115758

15742-
// iterate over the additionalData
15759+
// iterate over the additionalData instances and collect the split-test information
15760+
var splitTestData = this.getSplitTestData();
15761+
if (!$.isPlainObject(splitTestData)) {
15762+
splitTestData = {};
15763+
}
15764+
15765+
// add the new split-test information
1574315766
for (var k = 0; k < additionalData.length; k++) {
15744-
// console.log(additionalData);
15767+
var ad = additionalData[k];
15768+
if (!$.isPlainObject(ad) || !$.isPlainObject(ad.splitTestData) ||
15769+
Breinify.UTL.isEmpty(ad.splitTestData.groupDecision) ||
15770+
Breinify.UTL.isEmpty(ad.splitTestData.testName)) {
15771+
continue;
15772+
} else if (!$.isPlainObject(ad.splitTestData)) {
15773+
continue;
15774+
}
15775+
15776+
splitTestData[ad.splitTestData.testName] = $.extend({}, ad.splitTestData, {
15777+
'lastUpdated': new Date().getTime()
15778+
});
1574515779
}
15780+
15781+
// store the updated information and set it, it can only be modified here
15782+
Breinify.UTL.storage.update('splitTestData', 30 * 24 * 60, splitTestData);
15783+
this.splitTestData = splitTestData;
1574615784
},
1574715785

15748-
handleRecommendationResponse: function(data, errorText, callback) {
15786+
handleRecommendationResponse: function (data, errorText, callback) {
1574915787

1575015788
// we check for split-tests and store the results in the localStorage
1575115789
try {
15752-
this.storeSplitTest(data);
15790+
this.storeAdditionalData(data);
1575315791
} catch (e) {
1575415792
// ignore the exception, we still want to handle the response
1575515793
}
@@ -16067,10 +16105,10 @@ dependencyScope.jQuery = $;;
1606716105
Breinify.recommendation = function () {
1606816106
var url = _config.get(ATTR_CONFIG.URL) + _config.get(ATTR_CONFIG.RECOMMENDATION_ENDPOINT);
1606916107

16070-
var recHandler = function(url, data, callback) {
16108+
var recHandler = function (url, data, callback) {
1607116109

1607216110
// we utilize an internal callback to do some internal data-handling with the response
16073-
var internalCallback = function(data, errorText) {
16111+
var internalCallback = function (data, errorText) {
1607416112
_privates.handleRecommendationResponse(data, errorText, callback);
1607516113
};
1607616114

dist/breinify-api.min.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Breinify.js

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,34 @@
120120

121121
var _privates = {
122122
ready: false,
123+
splitTestData: null,
123124

124-
storeAdditionalData: function(data) {
125+
getSplitTestData: function () {
126+
if (this.splitTestData !== null) {
127+
return this.splitTestData;
128+
}
129+
130+
this.splitTestData = Breinify.UTL.storage.get('splitTestData');
131+
if (this.splitTestData === null || !$.isPlainObject(this.splitTestData)) {
132+
this.splitTestData = {};
133+
return this.splitTestData;
134+
}
135+
136+
// clean-up old split-test information
137+
for (var key in this.splitTestData) {
138+
if (!this.splitTestData.hasOwnProperty(key)) {
139+
continue;
140+
}
141+
142+
var lastUpdated = this.splitTestData[key].lastUpdated;
143+
if (typeof lastUpdated !== number || lastUpdated < -1) {
144+
delete this.splitTestData[key];
145+
}
146+
}
147+
return this.splitTestData;
148+
},
149+
150+
storeAdditionalData: function (data) {
125151
var additionalData;
126152
if (!$.isPlainObject(data)) {
127153
return;
@@ -134,29 +160,39 @@
134160
}
135161
}
136162
} else if ($.isPlainObject(data.additionalData)) {
137-
additionalData = [ data.additionalData ];
163+
additionalData = [data.additionalData];
138164
} else {
139165
return;
140166
}
141167

142-
// iterate over the additionalData instances
143-
for (var k = 0; k < additionalData.length; k++) {
144-
this.storeSplitTest(additionalData[k]);
168+
// iterate over the additionalData instances and collect the split-test information
169+
var splitTestData = this.getSplitTestData();
170+
if (!$.isPlainObject(splitTestData)) {
171+
splitTestData = {};
145172
}
146-
},
147173

148-
storeSplitTest: function(additionalData) {
149-
if (!$.isPlainObject(additionalData)) {
150-
return;
151-
} else if (!$.isPlainObject(additionalData.splitTestData)) {
152-
return;
174+
// add the new split-test information
175+
for (var k = 0; k < additionalData.length; k++) {
176+
var ad = additionalData[k];
177+
if (!$.isPlainObject(ad) || !$.isPlainObject(ad.splitTestData) ||
178+
Breinify.UTL.isEmpty(ad.splitTestData.groupDecision) ||
179+
Breinify.UTL.isEmpty(ad.splitTestData.testName)) {
180+
continue;
181+
} else if (!$.isPlainObject(ad.splitTestData)) {
182+
continue;
183+
}
184+
185+
splitTestData[ad.splitTestData.testName] = $.extend({}, ad.splitTestData, {
186+
'lastUpdated': new Date().getTime()
187+
});
153188
}
154189

155-
var splitTestData = additionalData.splitTestData;
156-
// console.log(splitTestData);
190+
// store the updated information and set it, it can only be modified here
191+
Breinify.UTL.storage.update('splitTestData', 30 * 24 * 60, splitTestData);
192+
this.splitTestData = splitTestData;
157193
},
158194

159-
handleRecommendationResponse: function(data, errorText, callback) {
195+
handleRecommendationResponse: function (data, errorText, callback) {
160196

161197
// we check for split-tests and store the results in the localStorage
162198
try {
@@ -478,10 +514,10 @@
478514
Breinify.recommendation = function () {
479515
var url = _config.get(ATTR_CONFIG.URL) + _config.get(ATTR_CONFIG.RECOMMENDATION_ENDPOINT);
480516

481-
var recHandler = function(url, data, callback) {
517+
var recHandler = function (url, data, callback) {
482518

483519
// we utilize an internal callback to do some internal data-handling with the response
484-
var internalCallback = function(data, errorText) {
520+
var internalCallback = function (data, errorText) {
485521
_privates.handleRecommendationResponse(data, errorText, callback);
486522
};
487523

src/BreinifyUser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@
352352
} else {
353353
instance.set(attribute, value);
354354
}
355-
})
355+
});
356356
},
357357

358358
validate: function () {

src/BreinifyUtil.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,15 +1413,10 @@
14131413
storage: {
14141414
instance: null,
14151415

1416-
init: function (entries, reset, callback) {
1416+
init: function (entries, callback) {
14171417
if (!$.isPlainObject(entries)) {
14181418
return;
14191419
}
1420-
// we allow also to not specify reset
1421-
else if ($.isFunction(reset) && typeof callback === 'undefined') {
1422-
callback = reset;
1423-
reset = false;
1424-
}
14251420

14261421
// check if we already have an instance (init may be called multiple times)
14271422
if (this.instance === null) {
@@ -1449,10 +1444,6 @@
14491444
}
14501445
}
14511446

1452-
if (reset) {
1453-
this.instance.clear();
1454-
}
1455-
14561447
var _self = this;
14571448
var toBeLoaded = {};
14581449
var loadingStatus = [];

0 commit comments

Comments
 (0)