如何通过JQuery手动传递Google Analytics的_ga参数实现跨域追踪?
Let's work through this step by step to get your _ga parameter passing working smoothly with your jQuery-powered navigation:
1. First, properly fetch the Google Analytics Client ID
Your current code references CLIENTID directly, but that variable isn't defined anywhere. You need to pull the Client ID from Google Analytics' tracker first.
Since you're using gtag.js (evidenced by your config snippet), use the gtag('get') method to retrieve the Client ID safely:
// Fetch the Client ID from your GA tracker gtag('get', 'UA-XXXXXX-X', 'client_id', function(clientId) { // Use the retrieved clientId for your redirect window.location.replace('https://mynewdomain.com/?_ga=' + clientId); });
If you're still using the older analytics.js library, you can fetch the Client ID this way instead:
ga(function(tracker) { var clientId = tracker.get('clientId'); window.location.replace('https://mynewdomain.com/?_ga=' + clientId); });
2. Make sure your redirect waits for GA to initialize
Don't attempt to fetch the Client ID before Google Analytics has fully loaded. Wrapping your redirect logic in the gtag('get') callback (like the first example) ensures you only access the Client ID once GA is ready to provide it.
3. Confirm your linker configuration is correct
Your existing gtag setup on mynewdomain.com looks spot-on:
gtag('config', 'UA-XXXXXX-X', { 'linker': { 'domains': ['olddomain.com','mynewdomain.com'], 'accept_incoming': true } });
This tells GA to recognize incoming _ga parameters from your old domain and link sessions correctly across both sites.
4. Test the end-to-end flow
- Trigger your jQuery redirect and check the resulting URL: you should see
?_ga=XXXXXappended with a valid Client ID string. - Check your GA reports for
mynewdomain.comto confirm sessions fromolddomain.comare being attributed properly.
If you run into issues where the Client ID comes back as undefined, double-check that your GA tracker ID (UA-XXXXXX-X) matches exactly on both domains, and that the gtag/analytics.js snippet loads before your redirect code runs.
内容的提问来源于stack exchange,提问作者Marco Vinicius Castellari

