Hello There!

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

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

snapshot_ga

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.

 

How to do Shopping Cart Analysis for E-commerce Websites?

item_revenue_in_rs

Of late we are working a lot on predictive modeling for e-commerce data and implementing interesting ideas using GA. Lot of our analysis leads to one conclusion that the lower end of the funnel is almost always presents lot of opportunities for improvement.

One of the other interesting problems that we encountered was to connect with the product manager who is interesting in optimizing the inventory and making sure that he learns usage patterns at a lower end of the funnel for different product. To tackle this interesting challenge, we had to get lot of data for products SKUs & have user action as metadata for these SKUs.

Data collection

We added following four interesting user actions on product page of an e-commerce stores, with the purpose to get a uniform metadata around product. The way we used the Event tracking here was

_trackevent(category, action, opt_label, opt_value,opt_nointeraction)
nomenclature : _trackevent(pp-product view, gender,product code+brand,price,true)

which can be converted into sample code that can potentially look like this ;

_gaq.push([‘_trackEvent’,’pp - product view’,’women’,’abc12332423’,’1999’,true])
  • Product view
  • Add to Cart
  • Feature selection
  • Wishlist addition

This can easily become a standard to use when user adds product to cart, or selects a color or adds a product to wishlist.

This way we extracted metadata of products along with additional metrics for the product to help product manager gauge what’s going on with the lower end of the funnel, which typically comprises of cart & check out pages.

If you have done several e-commerce implementations you can clearly realize that this method can provide very effective metadata for each product, in form of categorical variables like ,which gender it is aimed towards, brand & action occurring on this product (i.e. view, add to cart, feature selection, wishlist addition). The other additional ordinal variable data that is possible to beget is time spent on product , views, unique views, etc.

We also added similar type of event tracking implementation on cart page, which effectively also helped us provide product metadata

_trackevent(cp-product removed, gender, product code + size+brand,product proce,true)

That eventually translates into values like  below:

_gaq.push([‘_trackEvent’,’cp - product removed’,’men’,’xyz10234’,’1500’,true])

At the end of this implementation you would stand gaining such a beautiful set of data that you can do wonders with. Click on the image below to see the larger version of data yourself.

Product revenue table[click on image for full-size image]

Above data clearly will help to establish a relationship of each individual products revenue with its # of instances of cart addition, # of instances of removal, # of instances of view as well.

Using this dataset, we can create a visual representation of how the relationship between product views  & product revenue exists as below, thanks to ggplot2 & R

item_revenue_in_rs

All the products which falls in the area of above the curve, ie. High product views but low on revenue, needs work to be done. For such products a different strategy may be required, may be a specific campaign or a discount or something, but these are those products which are getting viewed but not bought.

Additional research can also be done to check what the relationship with product views is & cart add like below:

 

Cart add unique view

Dots in above chart represents # of people viewed the product vs. # of people added it to cart. Clearly, there are products which people are not adding to cart and require further research as to whether it is due to price, or shipping or competitive pressure that people are not considering to add this product to cart.

In the next post, I’d describe a linear relationship model that is able to predict the quantity of product sold based on factors such as views, alerts set, wishlists etc.

Bot Icon
Bot Icon

Tatvic Bot

Explore About Tatvic and Services