Skip to content

Instantly share code, notes, and snippets.

@kylefdoherty
Last active September 19, 2019 19:23
Show Gist options
  • Save kylefdoherty/61c96482c2b2c5caef02b0d04066849e to your computer and use it in GitHub Desktop.
Save kylefdoherty/61c96482c2b2c5caef02b0d04066849e to your computer and use it in GitHub Desktop.
const setHiddenInputValues = function(form, params) {
const inputs = form.elements;
inputsArr = Array.apply(null, inputs);
hiddenInputs = inputsArr.filter(function(i) {
return i.type === 'hidden';
});
// set value of inputs to the params passed in
hiddenInputs.forEach(function(i) {
key = i.name;
val = params[key];
if (val) {
i.value = val;
}
});
};
analytics.ready(function() {
console.log("SEGMENT READY")
const params = {
path: window.location.pathname,
url: window.location.href,
search: window.location.search,
referrer: document.referrer,
title: document.title,
anonymousId: analytics.user().anonymousId(),
sessionId: amplitude.getSessionId(),
userId: analytics.user().id()
};
// Grab all forms on the page, loop through them,
// and set their hidden fields to the corresponding params.
const $forms = document.getElementsByTagName("form");
const formsArr = Array.apply(null, $forms);
formsArr.forEach(function(f) {
setHiddenInputValues(f, params)
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment