Skip to content

Instantly share code, notes, and snippets.

@chavesm
Last active November 17, 2021 14:44
Show Gist options
  • Save chavesm/5607b36f9a01ef7bc955897771123232 to your computer and use it in GitHub Desktop.
Save chavesm/5607b36f9a01ef7bc955897771123232 to your computer and use it in GitHub Desktop.
Send a gtag.js event to GA via MonsterInsights when a form is submitted.
(function () {
const searchForm = document.getElementById("search_form_map1881");
const searchInput = document.getElementById("cspm_address_map1881");
function sendEvent(event) {
// DEBUG
console.log(`Search city entered for searchForm is: ${searchInput.value}`);
// GA code goes here. Below is an example using gtag.js API
// via MonsterInsights.
__gtagTracker("event", "conversion", {
event_category: "search_form_map1881",
event_action: searchInput.value,
event_label: "cspm_address_map1881",
});
// Don't submit the form
event.preventDefault();
}
searchInput.onchange = sendEvent;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment