Last active
November 17, 2021 14:44
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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