Skip to content

Instantly share code, notes, and snippets.

View chavesm's full-sized avatar

Mark Chaves chavesm

View GitHub Profile
@chavesm
chavesm / ga-mi-adjust-bounce-rate.html
Last active November 17, 2021 14:54
MonsterInsights adjust GA bounce rate gtag version.
<script>
// Adjust the bounce rate for GA.
setTimeout(function(){__gtagTracker('event', 'page visit 20 seconds or more', {
'event_category' : 'adjusted bounce rate',
})},20000);
</script>
<!-- Add to head section of page. -->
@chavesm
chavesm / ga-send-event-gtag.html
Last active March 5, 2021 08:36
Send an event to GA via MonsterInsights gtag version.
<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. -->
@chavesm
chavesm / monsterinsights_custom_add_ga4_tracking.php
Last active March 17, 2021 09:03
MonsterInsights Add GA4 Measurement ID for Dual Tracking (UA and GA4)
<?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. */
@chavesm
chavesm / crisp-ga-send-event-gtag.html
Last active March 5, 2021 15:04
Use the Crisp Chat JavaScript API to send an event to GA via MonsterInsights gtag version.
<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>
@chavesm
chavesm / 1162239-form-submit-send-event-to-ga.js
Last active November 17, 2021 14:44
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.
@chavesm
chavesm / mi-cookie-notice-no-consent.js
Last active March 13, 2021 08:29
Disable Google Analytics tracking via MonsterInsights (gtag version) when cookie consent in Cookie Notice plugin is rejected.
@chavesm
chavesm / mi-cookie-banner-no-consent.js
Created March 13, 2021 09:09
Disable Google Analytics tracking via MonsterInsights (gtag version) when cookie consent in Cookie Banner plugin is rejected.
@chavesm
chavesm / chrome-dev-tools-snippets-README.md
Last active March 16, 2021 14:53
Find Links and Forms Using JavaScript and Chrome Dev Tools (snippets and console)
@chavesm
chavesm / clean-up-url-search-query-parameters-in-ga-README.md
Last active November 17, 2021 14:20
ow to clean up URL search query parameters for Google Analytics reporting

How to clean up URL search query parameters for Google Analytics reporting

These instructions are for Universal Analytics properties.


Here's a screengrab of what we'd like to clean up.

Query parameters in site content report

@chavesm
chavesm / add-data-vars-ga-to-popular-posts-link.js
Last active March 19, 2021 03:54
Add data-vars-ga link attributes to Popular Posts links