mirror of
https://github.com/discourse/discourse.git
synced 2024-12-14 08:23:43 +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.
27 lines
678 B
JavaScript
27 lines
678 B
JavaScript
// discourse-skip-module
|
|
(function () {
|
|
const gaDataElement = document.getElementById("data-ga-universal-analytics");
|
|
window.dataLayer = window.dataLayer || [];
|
|
|
|
window.gtag = function () {
|
|
window.dataLayer.push(arguments);
|
|
};
|
|
window.gtag("js", new Date());
|
|
|
|
let autoLinkConfig = {};
|
|
|
|
if (gaDataElement.dataset.autoLinkDomains.length) {
|
|
const autoLinkDomains = gaDataElement.dataset.autoLinkDomains.split("|");
|
|
autoLinkConfig = {
|
|
linker: {
|
|
accept_incoming: true,
|
|
domains: autoLinkDomains,
|
|
},
|
|
};
|
|
}
|
|
window.gtag("config", gaDataElement.dataset.trackingCode, {
|
|
send_page_view: false,
|
|
autoLinkConfig,
|
|
});
|
|
})();
|