Skip to content

Instantly share code, notes, and snippets.

@chavesm
Last active November 17, 2021 15:01
Show Gist options
  • Save chavesm/4f722d548792e4beeebd333fd2f37459 to your computer and use it in GitHub Desktop.
Save chavesm/4f722d548792e4beeebd333fd2f37459 to your computer and use it in GitHub Desktop.
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");
let img948914 = document.querySelector("img");
if ( (dl948914) && (img948914) ) {
console.log('Found a link and image.');
let imgTitle = img948914.getAttribute('title');
if ( imgTitle) {
dl948914.setAttribute("data-vars-ga-label", document.title + ': ' + imgTitle);
}
}
</script>
<?php
}
} );
@chavesm
Copy link
Author

chavesm commented Sep 16, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment