mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 00:25:53 +08:00
09b8a61f65
Per Google, sites are encouraged to upgrade from Universal Analytics v3 `analytics.js` to v4 `gtag.js` for Google Analytics tracking. We're giving admins the option to stay on the v3 API or migrate to v4. Admins can change the implementation they're using via the `ga_version` site setting. Eventually Google will deprecate v3, but our implementation gives admins the choice on what to use for now. We chose this implementation to make the change less error prone, as many site admins are using custom events via the v3 UA API. With the site stetting defaulted to `v3_analytics`, site analytics won't break until the admin is ready to make the migration. Additionally, in the v4 implementation, we do not enable automatic pageview tracking (on by default in the v4 API). Instead we rely on Discourse's page change API to report pageviews on transition to avoid double-tracking.
22 lines
855 B
JavaScript
22 lines
855 B
JavaScript
// discourse-skip-module
|
|
/* eslint-disable */
|
|
// prettier-ignore
|
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
|
/* eslint-enable */
|
|
|
|
(function () {
|
|
const gaDataElement = document.getElementById("data-ga-universal-analytics");
|
|
const gaJson = JSON.parse(gaDataElement.dataset.json);
|
|
|
|
window.ga("create", gaDataElement.dataset.trackingCode, gaJson);
|
|
if (gaDataElement.dataset.autoLinkDomains.length) {
|
|
const autoLinkDomains = gaDataElement.dataset.autoLinkDomains.split("|");
|
|
|
|
window.ga("require", "linker");
|
|
window.ga("linker:autoLink", autoLinkDomains);
|
|
}
|
|
})();
|