0

Background: I have two pages on my site a Referring Page and the End Page. After users click a button on the Referring Page they will be directed to different versions of the End Page based a variable stored called pagex. I need to append parameters to the End Page for tracking purposes.

Code I use on End Page

if (localStorage.getItem('pagex') === 'page_1') {
 window.history.replaceState(null, null, "/error-test/?1");
}
else if (localStorage.getItem('pagex') === 'page_2') {
  window.history.replaceState(null, null, "/error-test/?2");
}
else {
window.history.replaceState(null, null, "/error-test/");
}

This was based on the solution here: Appending parameter to URL without refresh

Problem: While the above solution appends the correct parameter, it somehow creates two sessions in Google Analytics (eg. the first to /error-test/ and the second to /error-test/?1). This messes up analytics for tracking purposes.

Another solution I have considered but instead on the Referring Page include this: How to change button URL through JS?. However, the issue with such solutions is that I cannot amend the button behaviour (as this is determined by the plugin Elementor). The button basically redirects users to /error-test/ so all I need to do is append the right parameters behind the url.

Question: I think the way to solve this involves appending parameters but from the Referring Page so that URLs that users are redirected to already have the required parameters (and hence no need for a refresh)? That way there will exist only one session in Google Analytics. Any advice on how I can implement this?

Thank you.

1 Answers1

0

You are looking for cross domain tracking as google analytics currently doesn’t recognize the pages as joined so it created one for site A and one for site B.

See below reference for cross domain tracking

https://developers.google.com/analytics/devguides/collection/gtagjs/cross-domain

Jesse
  • 136
  • 5