Skip to content

Instantly share code, notes, and snippets.

View chavesm's full-sized avatar

Mark Chaves chavesm

View GitHub Profile
@chavesm
chavesm / 974747-dg-divi-carousel-frontend-bundle.js
Last active January 15, 2024 15:13
A mockup for sending an impression to GA for every slide displayed by Swiper JS from the DG Divi Slider Carousel Plugin
@chavesm
chavesm / mi-email-filter-hooks.php
Created December 22, 2020 12:24
PHP filter hooks to supply a custom from email address and name for MonsterInsights email summary reports.
<?php
/** MonsterInsights Email Hooks */
// Sender email address filter hook.
function my_custom_sender_email_address( $original_sender_email_address ) {
return 'YOURSENDEREMAILADDRESS';
}
add_filter( 'monsterinsights_email_from_address', 'my_custom_sender_email_address' );
// Sender name filter hook.
<?php // Ignore this line when copying/pasting this code to your child theme's functions.php file.
/**
* This example sets custom dimension #9 to be the WordPress
* post ID of the page/post being visited.
*
* This is compatible with MonsterInsights gtag code. This filter supercedes
* the monsterinsights_frontend_tracking_options_analytics_before_pageview
* filter.
*
@chavesm
chavesm / send-exit-intent-event-to-ga.js
Last active October 8, 2022 07:16
Send an exit-intent event to GA
/** 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.
@chavesm
chavesm / mi-set-data-vars-ga-label-for-images.php
Last active November 17, 2021 15:01
Custom Event Link Label For Image Links
<?php
/**
* Add to Child Theme's functions.php.
*/
add_action( 'wp_footer', function () {
if (is_page ('2')) {
?>
<script>// Custom Label Snippet
let dl948914 = document.querySelector("a");
@chavesm
chavesm / 949478-form-conversion-event.js
Last active November 17, 2021 14:58
Form Conversion Event (949478)
if (document.URL === "https://myverycool.site/form-page/") {
const subBtn949478 = document.querySelector(
"form#avia_contact_6935_contact-us-about input.button"
);
if (subBtn949478) {
subBtn949478.addEventListener(
"click",
function (evt) {
__gaTracker(
"send",
@chavesm
chavesm / codeless_remove_type_attr.php
Created February 24, 2021 07:16
WordPress filter hook that tries to remove unnecessary "text/javascript" and "text/css" from HTML source. Add to Child theme's functions.php.
<?php
/** Try to Remove Unnecessary "text/javascript" and "text/css" */
function codeless_remove_type_attr($tag, $handle) {
return preg_replace( "/type=['\"]text\/(javascript|css)['\"]/", '', $tag );
}
add_filter('style_loader_tag', 'codeless_remove_type_attr', 10, 2);
add_filter('script_loader_tag', 'codeless_remove_type_attr', 10, 2);
@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 / 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 / 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