Last active
March 19, 2021 03:54
-
-
Save chavesm/4de3ebce54a1cb4b29591ddf0da2632f to your computer and use it in GitHub Desktop.
Add data-vars-ga link attributes to Popular Posts links
This file contains hidden or 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
/** 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) => { | |
elem.setAttribute("data-vars-ga-category", "popular-posts"); | |
elem.setAttribute("data-vars-ga-action", elem.getAttribute("href")); | |
elem.setAttribute("data-vars-ga-label", elem.innerText); | |
}); | |
} | |
//console.clear(); | |
qSels.map((elem) => { | |
let pps = document.querySelectorAll(elem); | |
//console.log(`elem = ${elem}`); | |
//console.log(pps); | |
console.log(`pps.length = ${pps.length}`); | |
if (!pps.length) return; | |
/*console.log( | |
"%cPP: Found " + pps.length + " popular posts to process.", | |
clStyle | |
);*/ | |
addDataVarsGA(pps); | |
}); | |
}, 5000); | |
/** Add this JavaScript function to the footer section. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Screengrabs
Popular Posts Widget Sending Custom Event to GA
Another Popular Posts Widget Sending Custom Event to GA
Popular Products Link Sending Custom Event to GA