Skip to content

Instantly share code, notes, and snippets.

@nicjansma
Created July 15, 2024 20:29
Show Gist options
  • Save nicjansma/a3857cb321ceade35283bcf90da04922 to your computer and use it in GitHub Desktop.
Save nicjansma/a3857cb321ceade35283bcf90da04922 to your computer and use it in GitHub Desktop.
Boomerang DimensionStickyPlugin
/**
* The Boomerang DimensionStickyPlugin plugin will make a Custom Dimension "sticky" --
* if it is ever set (has a value), it will be always sent on subsequent beacons,
* even if the page doesn't set it.
*
* This plugin will utilize sessionStorage to do this.
*/
(function(w) {
//
// TODO: Configure which mPulse Dimension to make sticky
//
// aka cdim.TestDimension on the beacon
var DIMENSION_STICKY = 'TestDimension';
if (!w) {
return;
}
BOOMR = w.BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
BOOMR.plugins.DimensionStickyPlugin = {
//
// Vars
//
// the beacon parameter name
beaconParameter: "cdim." + DIMENSION_STICKY,
// sessionStorage key
sessionStorageKey: "BOOMR.dim." + DIMENSION_STICKY,
//
// Boomerang functions and hooks
//
init: function() {
if (!window.sessionStorage) {
// cannot persist
return;
}
BOOMR.subscribe("beacon", function(data) {
// determine if the beacon has this dimension
var beaconValue = data[this.beaconParameter];
if (typeof beaconValue !== "undefined") {
// save for later
sessionStorage.setItem(this.sessionStorageKey, beaconValue);
} else {
var storedValue = sessionStorage.getItem(this.sessionStorageKey);
// repeat on this beacon
if (storedValue !== null) {
data[this.beaconParameter] = storedValue;
}
}
}, {}, this);
},
is_complete: function() {
return true;
}
};
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment