Reputation: 3084
I have a SaaS solution with multiple tenants, like a website builder. I want to be able track stuff like page views, visitors, and number of online users ... for ALL of these tenants in one place. I think Google Analytics covers a scenario like this, but I am not sure how to implement it.
Some tenants has domains, other has sub-domains:
I am trying to use one Google Analytics property to cover all of these domain, what is the correct tracking code that can accomplish that? Here is what I got so far, but I am sure it doesn't work correctly, and I probably mixing gtag.js with analytics.js (I think)
<script async src="https://www.googletagmanager.com/gtag/js?id=[account id]"></script>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '[account id]']);
_gaq.push(['_setDomainName', '<?= getTenant()->getDomain() ?>']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '[account id]');
</script>
PS: No traffic is shared between these tenants, visitors do not go from domain X to domain Y. So I am not sure cross-domain tracking is the solution here, since it deals with visitors crossing between different domain. I just want the total numbers for all these tenants.
Upvotes: 1
Views: 815
Reputation: 14197
You are mixing gtag.js
code and ga.js
(Classic Analytics) code.
Classic Analytics has been deprecated for some years already.
You can measure activity across domains with gtag.js
: https://developers.google.com/analytics/devguides/collection/gtagjs/cross-domain
Or with analytics.js
(Universal Analytics): https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain
Or more simple using Google Tag Manager
with Universal Analytics tag: https://support.google.com/tagmanager/answer/6164469?hl=en
Any guide you use is important, for the correct functioning of cross-domain tracking, that these characteristics/results are respected:
All domains included in cross-domain tracking must collect data to the same Google Analytics Property
All domains that are the source of cross-domain traffic, i.e. the traffic departs from these domains, need to be in the Referral Exclusion List of the Google Analytics Property settings
When entering the target domain via a link in the source domain or an iframe, the URL of the page loaded in the web browser must have the _ga=1.234567.234567.234567 URL query parameter in place
Any Google Analytics trackers or tags firing on the target domain need to have the allowLinker field set to true
More details and how to implement cross-domain tracking via GTM you can follow this article by Simo Ahava: https://www.simoahava.com/analytics/troubleshooting-cross-domain-tracking-in-google-analytics/
Upvotes: 1