added GA event, switch to JS ISO country lookup

This commit is contained in:
Cappy Popp 2021-01-07 10:32:20 -05:00
parent 155ae8989e
commit 7db04aa163

View File

@ -15,16 +15,20 @@
newInput.setAttribute('id', tagName + '-flag'); newInput.setAttribute('id', tagName + '-flag');
newInput.style.display = 'none'; newInput.style.display = 'none';
newInput._value = newInput.value; newInput._value = newInput.value;
Object.defineProperty(newInput, "value", { Object.defineProperty(newInput, 'value', {
get: function() { return this._value; }, get: function () {
set: function(v) { return this._value;
},
set: function (v) {
this._value = v; this._value = v;
onChangeHandler(v); onChangeHandler(v);
} },
}); });
hiddenLinkedInInputs.appendChild(newInput); hiddenLinkedInInputs.appendChild(newInput);
fillScript.setAttribute('data-field-' + tagName, tagName + '-flag'); fillScript.setAttribute('data-field-' + tagName, tagName + '-flag');
}; },
linkedInAutofillClicked = false;
// style the holder div // style the holder div
iframeHolderDiv.style.textAlign = 'center'; iframeHolderDiv.style.textAlign = 'center';
iframeHolderDiv.style.margin = '10px 0px'; iframeHolderDiv.style.margin = '10px 0px';
@ -54,10 +58,15 @@
// requires // requires
hiddenInputCreator('email', function (newValue) { hiddenInputCreator('email', function (newValue) {
// fire the GA event for autofill // fire the GA event for autofill
console.log("%s value changed to %s", this.ID, newValue); console.log('%s value changed to %s', this.ID, newValue);
// this sets the flag to ID an autofill event. We are
// gating this on email since it's the only guaranteed
// field that we can't see on screen so can't be set by
// a user directly...
linkedInAutofillClicked = true;
}); });
hiddenInputCreator('country', function (newValue) { hiddenInputCreator('country', function (newValue) {
console.log("%s value changed to %s", this.ID, newValue); console.log('%s value changed to %s', this.ID, newValue);
// map to the country we have in our select dropdown // map to the country we have in our select dropdown
var countries = { var countries = {
US: 'United States', US: 'United States',
@ -260,15 +269,17 @@
ZM: 'Zambia', ZM: 'Zambia',
ZW: 'Zimbabwe', ZW: 'Zimbabwe',
}; };
var newCountry = '{{JS - ISO Country Code to EVBG Country Name}}(newValue)';
// sets the value in the form the user can see // sets the value in the form the user can see
if (typeof MktoForms2 !== 'undefined') { if (typeof MktoForms2 !== 'undefined') {
mktoForm.setValues({ mktoForm.setValues({
'Country': countries[newValue.toUpperCase()]}); Country: /*countries[newValue.toUpperCase()]*/ newCountry
});
} }
}); });
hiddenInputCreator('state', function(newValue){ hiddenInputCreator('state', function (newValue) {
mktoForm.setValues({ mktoForm.setValues({
'State': newValue.toUpperCase() State: newValue.toUpperCase()
}); });
}); });
@ -305,34 +316,31 @@
// add the elements to the DOM // add the elements to the DOM
iframeHolderDiv.appendChild(fillScript); iframeHolderDiv.appendChild(fillScript);
autoFillLocation.prepend(iframeHolderDiv); autoFillLocation.prepend(iframeHolderDiv);
firstRow.appendChild(hiddenLinkedInInputs); // fragment vaporizes leaving input elements firstRow.appendChild(hiddenLinkedInInputs); // fragment vaporizes leaving input elements
if (typeof MktoForms2 !== 'undefined') { if (typeof MktoForms2 !== 'undefined') {
MktoForms2.whenReady(function (form) { MktoForms2.whenReady(function (form) {
// need the id of the form so we can hook up autofill // need the id of the form so we can hook up autofill
// doing this way will work on any Marketo form // doing this way will work on any Marketo form
fillScript.setAttribute('data-form', 'mktoForm_' + form.getId()); fillScript.setAttribute('data-form', 'mktoForm_' + form.getId());
mktoForm = form; mktoForm = form; // so we can use it above
form.onSubmit(function (form) { form.onSubmit(function (form) {
console.log('onSubmit'); // tell GA about it, but only if we know they autofilled
console.log(form.vals()); if (linkedInAutofillClicked) {
}); var values = form.getValues(),
form.onSuccess(function (values, followUpURL) { eventData = {
console.log('onSuccess LI'); category: 'LinkedIn Form AutoFill',
// tell GA about it, but only if we set the hidden email field since we know LI should have that action: 'mktoForm_' + form.getId() + '-', // will automatically set dlv-action which we'll pass to form id to name mapper in event submit in GTM
if (input.value.length && input.value.indexOf('@') !== -1) { label: [
var eventData = { values.Company,
category: 'LinkedIn Form AutoFill', values.Industry,
action: 'mktoForm_' + form.getId() + '-', // will automatically set dlv-action which we'll pass to form id to name mapper in event submit in GTM values.Title,
label: [ values.Country,
values.Company, values.State,
values.Industry, '{{Page Path}}',
values.Country, 'gclid:' + '{{JS - GA Client ID}}',
values.Title, ].join(' | '),
'{{Page Path}}', };
'gclid:' + '{{JS - GA Client ID}}',
].join(' | '),
};
console.log(eventData); console.log(eventData);
// 'eventData' automatically maps to the GTM variables 'dlv - category', 'dlv - action', etc. // 'eventData' automatically maps to the GTM variables 'dlv - category', 'dlv - action', etc.
window.dataLayer.push({ window.dataLayer.push({