Problem
I have forms that are hosted by a 3rd party service. We’ll call this www.3rdpartyform.com.
I have my site, www.mysite.com.
I want to be able to track traffic using google analytics campaigns going to www.3rdpartyform.com.
My Solution
I’ve created a landing page www.mysite.com/redirected.
It’s used like so:
www.mysite.com/redirected/?redirected_url=www.3rdpartyform.com/theform&utm_googlestuff=stuff
The page is essentially this:
<?php $redirection_link = htmlspecialchars($_GET['redirected_url']); ?>
<html>
<head>
(google analytics script)
<!-- Should address any possible negative impact on SEO caused by redirect. -->
<meta name="robots" content="noindex, nofollow, noarchive">
</head>
<body>
<script>
var retryAttempts = 0;
function checkIfAnalyticsLoaded() {
console.log('Checking if GA loaded.');
if (window.ga && ga.create) {
console.log('GA loaded.');
redirect();
} else if (window.urchinTracker) {
console.log('Old GA loaded.');
redirect();
} else if (retryAttempts < 10) {
retryAttempts += 1;
setTimeout(checkIfAnalyticsLoaded, 500);
} else {
console.log('GA not loaded')
redirect();
}
}
function redirect() {
console.log('Redirecting');
setTimeout(function () {
window.location.href= '<?php echo $redirection_link; ?>';
}, <?php echo $time_to_redirect * 1000 ?>);
}
checkIfAnalyticsLoaded();
</script>
</body>
</html>
How I think it will work
Someone clicks the link, the page loads my google analytics and sees the campaign URL parameters and logs the hit, then the redirect takes the user to the form at www.3rdpartyform.com/theform.
My Question
I tried searching for a solution but could not think of what to search for that gave relevant results. Is there a better solution to my current problem than what I’m attempting? Would google analytics even register a view/campaign (even if it’s a bounce) with this solution?
Thanks!
Updated Solution
This is my updated solution so far based off of Max‘s response.
To negate possible negative impacts that a redirect would have on my SEO, I’ve added nofollow, noindex metadata. Not 100% on this solution but seems logical.
To address the Race Condition I’ve added a function to check if GA is loaded.
As for the Poor UX/Speed issue, I don’t see this as a problem with my current implementation/target audience configuration at this time.
I’ve updated my solution above.
Issues Remaining
Would like to use GA events as suggested by SMX to prevent hits to this page being counted as bounces. Haven’t messed with GA events yet though and would need to learn about them, but have to move on to next project for now.
2
Answers
Yes, this is probably the most reasonable solution for your analytics. GA will register hits on your landing page and attribute campaign information to them, as intended. Of course, note that what you’ll be measuring is landing page visits: there’s no way you can track actions people perform on your third-party forms site (pageviews, clicks, etc.). The way you currently have it set up, all visits to your landing page will likely count as bounces, since the user will touch your site only once. To prevent this, you could add a line of javascript in your header to send another hit (e.g. event) to GA.
This type of intermediate page with JavaScript redirects may be bad for SEO but my SEO days are behind me and I’m not sure there so going to leave this point out.
Other problems you didn’t mention:
Race condition: since the GA snippet is async, there is actually no guarantee it will load and track the pageview before the redirect occurs. Increasing the timeout is not a solution because of the below problem.
Poor UX/Speed: before your users get a chance to load the 3rd party website, they will be subjected to 1 additional page load + 1 sec timeout, potentially several seconds of unnecessary wait, not great. Facebook do something similar, but they have a super fast infrastructure, yours is probably significantly slower.
The solutions:
Measurement Protocol: this would allow you to track the redirect via
PHP
, thus guaranteeing tracking, and once the API call has been made by PHP, you could issue a302
redirect, thus removing the timeout, the ugly redirect and potentially also the additional page load (you could have the client send an AJAX request to /redirected/ to make that call). This solution is quite some work however.Hit callback: this solution is probably the best “value” (close to 100% reliability, not much implementation work). The idea is to disable the default behaviour of links (eg
return false;
) and use GA’shitcallback
functionality to track the click, then allow the link redirect to proceed.