Skip to content

Instantly share code, notes, and snippets.

@peterjaffray
Created September 4, 2021 18:39
Show Gist options
  • Save peterjaffray/6f05e2a50f4c62b5c0a32ec5a5910e09 to your computer and use it in GitHub Desktop.
Save peterjaffray/6f05e2a50f4c62b5c0a32ec5a5910e09 to your computer and use it in GitHub Desktop.
GA Connector for Elementor Pro Forms
<script data-cfasync="false" type="text/javascript" src="https://tracker.gaconnector.com/gaconnector.js"></script>
<script>
function setGaconnectorHiddenFields() {
const gaFields = gaconnector.getCookieValues()
const field_names = Object.keys(gaFields) || []
const form_name = document.forms["ga_connector"]
const number_of_fields = field_names.length;
//****************************************************************
// First, let's grab everything from the GA Connector cookies and
// add all the hidden fields with name = "parameter"
//****************************************************************
if (typeof form_name !== "undefined") {
for (let i = 0; i < number_of_fields; i++) {
const field_name = field_names[i]
let input = document.createElement("input");
input.setAttribute("size", "0");
input.setAttribute("type", "hidden");
input.setAttribute("name", field_name);
form_name.appendChild(input);
}
}
//****************************************************************
// Second, let GA connector add all the values for us. It pulls the
// values from non-http only cookies.
//****************************************************************
for (const fieldName in gaFields) {
const selector = `form input[name="${fieldName}"]` //generate names
const input = document.querySelectorAll(selector);
if (input === null) {
continue;
}
else if (typeof input.length === 'undefined') {
input.value = gaFields[fieldName];
} else {
// Overcomplex? I'm leaving this the way they have it.
for (let i = 0; i < input.length; i++) {
input[i].value = gaFields[fieldName];
}
}
}
}
//****************************************************************
// This controls the timing of the execution.
// Neither of these functions will happen until GA load is successful.
//****************************************************************
if(gaconnector) {
gaconnector.setCallback(setGaconnectorHiddenFields);
setInterval(setGaconnectorHiddenFields, 1000);
} else {
console.log("No GA Connector")
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment