Can I use Google Analytics on URL shortener redirects powered by PHP?

Can I use Google Analytics on URL shortener redirects powered by PHP? - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the Can I use Google Analytics on URL shortener redirects powered by PHP? error on your web browser. Problem :


I have a short URL website like Bitly which currently redirect users with PHP from my site to a site of their choice. I want to add Google Analytics to the redirect pages but I am stuck on the problem where Javascript is run after the PHP code is executed so it wont work.


Bellow is the last part of the PHP code that redirects users from the short URL to the URL they shortend. I wonder if there is anyway to make this work OR change my code somehow so that Google Analytics can be executed either with PHP or something else that can do the "redirection"?


$shortUrlObj contains the short url code. If the short url is https://example.com/1234 $shortUrlObj contains 1234. fullUrl is the website destination they choose to shorten. So if the user shortened https://stackexchange.com, fullUrl contains https://stackexchange.com


switch ($redirect)

default:
header('HTTP/1.1 302 Moved Temporarily');
redirect($shortUrlObj->fullUrl);
break;


Solution :

I got it working by using Javascript to do the redirecting. However the redirect became extremely slow when loading. Does anyone know why and what can be done about that part?


switch ($redirect)
[];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-xxxxxx-1');
</script>

<script>
window.location = '
<? echo $shortUrlObj->fullUrl; ?>'
</script>

<?php
break;


We hope that this article has helped you resolve the google-analytics, redirects, php error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

"302 found" for index page bad for SEO?

Create redirect from url like www.example.us/?p=100&option=