Skip to content

Instantly share code, notes, and snippets.

@jlipke-s24
Created February 2, 2016 14:17
Show Gist options
  • Save jlipke-s24/9585d4b3c7e88e416cce to your computer and use it in GitHub Desktop.
Save jlipke-s24/9585d4b3c7e88e416cce to your computer and use it in GitHub Desktop.
window.s24.modules.pushnotifications = (function (w, s24js) {
'use strict';
/**
* Currently only needed for smatch's _gaq
* in future could take more in account like `ga` or `dataLayer`
*/
function pushToGoogleAnalytics(eventname, value) {
if (_gaq && _gaq.push) {
_gaq.push(['_trackEvent', 'pushnoti', eventname, value]);
}
}
/**
* Displays the one signal browser prompt
*/
function _showBrowserPrompt(){
OneSignal.push(['registerForPushNotifications']);
pushToGoogleAnalytics('auto', 'display-browser-prompt');
}
/**
* The following checks if notifications are supported.
* also it checks if it is NOT enabled yet for the portal.
* and finally displays the browser prompt after 3 seconds
*/
function _oneSignalFunctionality() {
// Check if Browser Push is supported and not yet enabled for this website
OneSignal.push(function () {
if (!OneSignal.isPushNotificationsSupported()) {
return;
}
OneSignal.push(['isPushNotificationsEnabled', function(enabled) {
if (enabled) {
return;
}
setTimeout(_showBrowserPrompt, 3000);
}]);
});
}
/**
* Initiate the push notifications module
*/
function init() {
var amountImpressions;
amountImpressions = s24js.getCookie('totalpageviewcounter') || 0;
s24js.setCookie({
name: 'totalpageviewcounter',
value: ++amountImpressions,
path: '/',
expires: 14
});
// Show banner in case the amount impressions are more than 2 and less than 10
// Also show it for 20 to 29 and 40 to 49 and 60 to 69 etc
// I do not understand why amountImpressions -> 1 also is true..
// that is why the first condition checks for 3 instead of 2
if ((amountImpressions > 3 && amountImpressions < 10) || amountImpressions % 20 < 10){
_oneSignalFunctionality();
}
}
return {
'init': init
};
}(
window,
window.s24.modules.s24js
)).init();
// invoking the init method of the module right away, because this is
// an A/B Test and this way it is easier to clean up after ourselfs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment