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.