These instructions are for Universal Analytics properties.
Here's a screengrab of what we'd like to clean up.
| /** Exit - intent event demo */ | |
| (function () { | |
| if (!document.URL.includes("simple-form")) return; | |
| const beforeUnloadListener = (event) => { | |
| event.preventDefault(); | |
| // GA code goes here. | |
| // Below is an example using gtag.js API. |
| /** Add data-vars-ga for MI Popular Posts Links */ | |
| setTimeout(function() { | |
| //const clStyle = "font-size: 20px; color: limegreen"; | |
| const qSels = [ | |
| ".monsterinsights-inline-popular-posts-post a", | |
| ".monsterinsights-widget-popular-posts-list a", | |
| ".monsterinsights-products-popular-posts-list a", | |
| ]; | |
| function addDataVarsGA(pps) { | |
| pps.forEach((elem) => { |
| (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. |
| <script> | |
| // Send an Crisp Chat event via gtag. | |
| $crisp.push(["on", "chat:initiated", function() { | |
| __gtagTracker('event', 'Test Event', { | |
| 'event_category' : 'test_event', | |
| 'event_action' : 'test_action', | |
| 'event_label' : 'This is a test event.' | |
| }); | |
| }]); | |
| </script> |
| <?php | |
| function monsterinsights_custom_add_ga4_tracking() { | |
| echo "__gtagTracker( 'config', 'G-12345678' );"; // Change the measurement ID to yours. | |
| } | |
| add_action( 'monsterinsights_frontend_tracking_gtag_after_pageview', 'monsterinsights_custom_add_ga4_tracking', 15 ); | |
| /** Add to child theme's functions.php file. */ |
| <script> | |
| // Send an event via gtag. | |
| __gtagTracker('event', 'Test Event', { | |
| 'event_category' : 'test_event', | |
| 'event_action' : 'test_action', | |
| 'event_label' : 'This is a test event.' | |
| }); | |
| </script> | |
| <!-- Add to head section of page using proper enqueue | |
| techniques or a plugin like Insert Headers and Footers. --> |