📢 Tatvic is now a global partner of Usercentrics, world’s leading consent management platform

PHP code to check existence of Google Analytics on a webpage and extract UA-ID

Last Updated:

Services:    

Integration of Google Analytics (GA) code to a website is very easy. But what if you want to know whether a page has Google Analytics implemented or not?

One should have a clear understanding about how a Google analytics code looks like if it is implemented on any webpage.  Below is an example of GA script on www.liftsuggest.com

snapshot_ga

If you will observe few more pages, you will come to know that there are few common word phrases in GA’s script and it is obvious.   We will take advantage of it and presence of such words on a webpage will let us know the existence of Google Analytics. We will consider some of these words and they are,

  • ga.js
  • _trackPageview
  • UA-ID

If all these three words are present on a webpage, we can say that GA is implemented.

Why do we need to check whether GA is implemented or not?

Let’s think of a scenario. In most of the cases, if GA is implemented on 1 or 2 pages of a website, then the owner is aware about it.   But what if a website has many pages (more than hundreds or thousands) and they all must have GA implemented? How to ensure that every page is having GA implementation script? It is very important to get proper analytics data.

To track UA-ID and to know existence of GA for a single page, we have to follow some steps technically. Overview of such steps is as below.

  1. Set a url for which we have to extract Google Analytic’s UA-ID
  2. Grab content of the url using CURL
  3. Extract all the script tags of the content using regular expression (Because GA script is always implemented between <script> and </script> tags.)
  4.  Check for ga.js and _trackPageview in all <script> tag
  5. Extract UA-ID using regular expression
  6. Check whether all 3 word phrases are present or not.

From the overview, you will come to know that Knowing presence of GA is nothing but grabbing content of a webpage and firing some regular expression on the content. However, which regular expression should be used is very important.

How to do this using PHP code?

Code which follows previously mentioned steps is as below.

Create one PHP file and name it Ga_track.php. Write following code.

<?php

class Ga_track
{
    function get_ga_implemented($url)
    {
    $options = array(
        CURLOPT_RETURNTRANSFER => TRUE, // return web page
        CURLOPT_HEADER => TRUE, // don't return headers
        CURLOPT_ENCODING => "", // handle all encodings
        CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64)", // who am i
        CURLOPT_SSL_VERIFYHOST => FALSE, //ssl verify host
        CURLOPT_SSL_VERIFYPEER => FALSE, //ssl verify peer
        CURLOPT_NOBODY => FALSE,
    );

    $ch = curl_init($url);
    curl_setopt_array($ch, $options);

        //2> Grab content of the url using CURL
    $content = curl_exec($ch); 
   
    $flag1_trackpage = false; //FLag for the phrase '_trackPageview'
    $flag2_ga_js = false; //FLag for the phrase 'ga.js'

    // Script Regex
    $script_regex = "/<scriptb[^>]*>([sS]*?)</script>/i"; 

    // UA_ID Regex
    $ua_regex = "/UA-[0-9]{5,}-[0-9]{1,}/";

    // Preg Match for Script
    //3> Extract all the script tags of the content    
        preg_match_all($script_regex, $content, $inside_script); 
    //4> Check for ga.js, analytics.js, gtag() and _trackPageview in all <script> tag
    try{
        for ($i = 0; $i < count($inside_script[0]); $i++)
    {                               
        if ((stristr($inside_script[0][$i], "ga.js") || stristr($inside_script[0][$i], "function gtag()") || stristr($inside_script[0][$i], "analytics.js")))
            $flag2_ga_js = TRUE;
            
        if (stristr($inside_script[0][$i],"_trackPageview")||stristr($inside_script[0][$i],"gtag('js', new Date())")        
        || stristr($inside_script[0][$i], "ga('send', 'pageview')"))
            $flag1_trackpage = TRUE;
            
    }
    }catch(Exception $e) {
        echo 'Message: ' .$e->getMessage();
        print "error in identifying the snippet";
      }

    // Preg Match for UA ID
    //5> Extract UA-ID using regular expression
    try {
        preg_match_all($ua_regex, $content, $ua_id);
        //6> Check whether all 3 word phrases are present or not.       
        if ($flag2_ga_js && $flag1_trackpage && count($ua_id > 0))
            return($ua_id);
        else
            return(NULL);
    } catch(Exception $e) {
        echo 'Message: ' .$e->getMessage();
        print "error in pregmatch";
      }
    }

}

$ga_obj = new Ga_track();
//1> Set a url for which we have to extract  UA-ID
$url = "url for which we have to extract ID "; 


//===========Block 2===========//
/*You can also make array here from database as below,
set_time_limit(0);
$urls=array();
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

$result = mysql_query("SELECT url_field FROM table");

while($row = mysql_fetch_array($result))
  {
  $urls[]=$row['url_field'];
  }
mysql_close($con);

foreach ($urls as $url)
{
    Copy block 1 here.
}
*/
//===========Block 2 over===========//

//===========Block 1===========//
try{
    $ua_id = $ga_obj->get_ga_implemented($url); //Call to a function to extract details
}catch(Exception $e) {
    echo 'Message: ' .$e->getMessage();
    print "error in the function to extract details";
  }
print_r($ua_id);
if ($ua_id == NULL)
{
    echo "<br/>Google Analytics is not implemented";
}
else
{
    echo "<pre>";
    print_r($ua_id);
    echo "</pre>";
    echo "<br/>Google Analytics is implemented.";
}
//===========Block 1 over===========//

?>
If you have multiple pages, you can store them in database or in an array and use previously mentioned steps in a looping manner by extracting each page from database. (Block 2)

You can suggest more cases where we can use this solution or Please feel free to reach out to me if you have faced such case and want solution related to this.

 

Dikesh Jariwala

Dikesh Jariwala

Dikesh is a software developer and has been a pioneer in building some cool and fun codes at Tatvic. Dikesh loves gaming and has been a champion of Counter Strikes at least at Tatvic.

Sign up for
our monthly newsletter

This field is for validation purposes and should be left unchanged.

Other Blog

Mobile App UX Design Process
ios app design guidelines
Scroll to Top

Leverage Tatvic's comprehensive approach to 

Enquire Now

This field is for validation purposes and should be left unchanged.

Leverage Tatvic's comprehensive approach to 

Contact Us

Checkbox
This field is for validation purposes and should be left unchanged.