Skip to content

Instantly share code, notes, and snippets.

@fredriccliver
Created August 16, 2021 12:04
Show Gist options
  • Save fredriccliver/d6dd3e8d93e625c89dc77769d81b6483 to your computer and use it in GitHub Desktop.
Save fredriccliver/d6dd3e8d93e625c89dc77769d81b6483 to your computer and use it in GitHub Desktop.
window.onload = function () {
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener("click", function (e) {
// Get href link value
const targetUrl = this.getAttribute("href");
// Track the event name as "anchor_click" and custom field name is "target", value is the href value.
firebase.analytics().logEvent('anchor_click', { target: targetUrl});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment