Last active
November 17, 2021 15:01
-
-
Save chavesm/4f722d548792e4beeebd333fd2f37459 to your computer and use it in GitHub Desktop.
Custom Event Link Label For Image Links
This file contains 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
<?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 | |
} | |
} ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live demo on CodePen: https://codepen.io/marklchaves/pen/wvGxjQm