gtag
last update:
2020/12/19
// document https://developers.google.com/analytics/devguides/collection/gtagjs/
// html (under head start tag) <!--gtag--> <script async src="https://www.googletagmanager.com/gtag/js?id={GA_TRACKING_ID}"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '{GA_TRACKING_ID}'); </script> <!--/gtag--> code_popup
// html (dataset) <a href="~" data-gtag="{category}|{action}|{label}">~</a>
// js (function() { document.addEventListener('click', function(e) { if (e.target.tagName !== 'A' || !e.target.dataset.gtag) { return; } var data = e.target.dataset.gtag.split('|'); var category = data[0]; var action = data[1]; var label = data[2]; e.preventDefault(); window.gtag('event', action, { event_category: category, event_label: label, event_callback: function() { location.href = e.target.href; }, }); }, false); })(); code_popup