Skip to content

Instantly share code, notes, and snippets.

@tomfuertes
Last active February 7, 2022 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomfuertes/f6527fd1977400ffc2d2561b660341ec to your computer and use it in GitHub Desktop.
Save tomfuertes/f6527fd1977400ffc2d2561b660341ec to your computer and use it in GitHub Desktop.

window.dfc

window.dfc is a function that allows you to pass a string and returns an int. This string is persisted via localStorage and available to send to reporting solutions like google optimize server side.

Installation

  1. Paste the inline js function into template.liquid as close to the <head> as possible
  2. Paste the ga additional code into shopify admin -> online store -> prefs -> Google Analytics for tracking experiments

Questions? Email TomFuertes at google's email domain dot com

<!-- Unminified / Docs: https://main.dgjvwr5wei7tc.amplifyapp.com/dfc.inline.js -->
<script>"use strict";!function(){var n,s,h="dfc-exp-";try{var t=new URLSearchParams(location.search),c=new RegExp("^".concat(h));t.forEach(function(t,r){c.test(r)&&localStorage.setItem(r,t)})}catch(t){console.error(t)}window.dfc||(n=(t=window.dfc=function(r){var t,c,e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:2;try{var o="".concat(h).concat(r),a=parseInt((c=localStorage.getItem(o))||(t=e-1,c=Math.ceil(0),t=Math.floor(t),Math.floor(Math.random()*(t-c+1))+c),10);localStorage.setItem(o,a),n.push([r,a]);try{s.forEach(function(t){return t(r,a)})}catch(t){console.error(t)}return a}catch(t){return 0}}).experiments=[],s=t.trackers=[],t.version="2022-01")}();</script>
// grab string for current send
if (window.dfc && dfc.experiments) {
var str = dfc.experiments.map(function(exp) {
return exp[0] + '.' + exp[1];
}).join('!');
if (str) {
ga('set', 'exp', str);
}
}
// queue up an event future sends
if (window.dfc && dfc.trackers) {
dfc.trackers.push(function (id, idx) {
ga('set', 'exp', id + '.' + idx);
ga('send', 'event', 'window.dfc', id, id + '.' + idx, {
nonInteraction: true
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment