Skip to content

Instantly share code, notes, and snippets.

@ryancoughlin
Created January 16, 2018 17:26
Show Gist options
  • Save ryancoughlin/ce8beb51f97124e225a8c6d9aa1adbb8 to your computer and use it in GitHub Desktop.
Save ryancoughlin/ce8beb51f97124e225a8c6d9aa1adbb8 to your computer and use it in GitHub Desktop.
window.addEventListener('load', function() {
var REFERRER_COOKIE = '_rbn_referral',
noop = function() {};
var $freeTrialForm = document.querySelector('.hbspt-form > form');
if ($freeTrialForm) {
console.log(`We have the free trial form`);
$freeTrialForm.addEventListener('hsvalidatedsubmit', function(event) {
console.log(`We are attempting to submit`);
var calendar,
company,
email,
contactSubmit,
context,
name,
payload,
referrer,
rooms,
phone,
_this = this,
goog_report_conversion = window.goog_report_conversion || noop;
ga = window.ga || noop;
ga('send', 'event', 'Free Trial', 'Clicked Submit', 'Office interest');
email = $freeTrialForm.querySelector('input[name="email"]').value;
phone = $freeTrialForm.querySelector('input[name="phone"]').value;
name =
$freeTrialForm.querySelector('input[name="firstname"]').value +
' ' +
$freeTrialForm.querySelector('input[name="lastname"]').value;
calendar = $freeTrialForm.querySelector('select[name="calendar_system"]')
.value;
company = $freeTrialForm.querySelector('input[name="company"]').value;
rooms = $freeTrialForm.querySelector('select[name="number_of_rooms"]')
.value;
demoInterest = $freeTrialForm.querySelector(
'input[name="form_next_step"]'
).checked;
referrer = Cookies.get(REFERRER_COOKIE);
payload = new FormData();
// @TODO [ct] convert into formdata
payload.append('name', name);
payload.append('calendar', calendar);
payload.append('company', company);
payload.append('phone', phone);
payload.append('email', email);
payload.append('rooms', rooms);
payload.append('demoInterest', demoInterest);
payload.append('referrer', referrer);
post('/slackbot-free-trial', payload)
.then(function(response) {
goog_report_conversion();
ga(
'send',
'event',
'Free Trial',
'Submission Received',
'Office interest'
);
window.location =
'https://dashboard.robinpowered.com/register?name=' +
encodeURIComponent(name) +
'&email=' +
encodeURIComponent(email);
})
.catch(function(error) {
console.error(error);
})
.then(function(response) {
document.querySelector('.free-trial-redirect').style.display =
'block';
});
});
}
});
function post(url, payload) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.onload = function() {
// This is called even on 404 etc
// so check the status
if (xhr.status == 200) {
// Resolve the promise with the response text
resolve();
} else {
// Otherwise reject with the status text
// which will hopefully be a meaningful error
reject(Error(xhr.statusText));
}
};
// Handle network errors
xhr.onerror = function() {
reject(Error('Network Error'));
};
// Make the request
xhr.send(payload);
});
}
@ColeTownsend
Copy link

ColeTownsend commented Jan 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment