2

I have a php shorturl website like bitly and i want to track users accessing the short urls. Is there anyway i can get the google analytics code in here and working someone? just adding it wont work, the code will not run at all. I tried to add above the code to make it work. The site is running php 7.3

<head>
                <!-- Global site tag (gtag.js) - Google Analytics -->
                <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXX-1"></script>
                <script>
                window.dataLayer = window.dataLayer || [];
                function gtag(){dataLayer.push(arguments);}
                gtag('js', new Date());
</head>

    <?php
    require_once('master.php');
    
    if ($shortUrlObj->status != "active")
    {
        $errorMsg = t("longer_active");
        redirect(ROOT . "/404." . EXTENSION . "?e=" . urlencode($errorMsg));
    }
    
    $redirectType = SITE_CONFIG_REDIRECT_TYPE;
    
    switch ($redirectType)
    {
        default:
        header('HTTP/1.1 302 Moved Temporarily');
            redirect($Url->Url);    
        break;
    }
    
    ?>
             
  • You can add a delay before redirect so the Google Analytics can process the traffic – adampweb Feb 20 '21 at 17:47
  • Does this answer your question? [How to track with Google Analytics on a redirection page with PHP?](https://stackoverflow.com/questions/27453830/how-to-track-with-google-analytics-on-a-redirection-page-with-php) – adampweb Feb 20 '21 at 17:50
  • how long of a delay must it be ? – Erisksson J Feb 20 '21 at 19:34

1 Answers1

1

You have to add UTM parameters in the URL queryString, so you will be able to recognize the source and the medium. Something like this:

/your-page.php?utm_source=YOURSHORTURLWEBSITE&utm_medium=YOURSHORTURL

Where YOURSHORTURLWEBSITE could be the domain name of the site containing the shorturls and YOURSHORTURL the shorturl link.

Michele Pisani
  • 13,567
  • 3
  • 25
  • 42