I need to check is given website use google analytics or not in PHP
You probably can't do it reliably because it could be bured in any script the site uses.
However, most people do follow the example code that Analytics provides. If that is close enough, just get the HTML content with cURL and parse it for that block of JavaScript code.
Something like:
curl DOMAINNAME | grep -c google-analytics.com
If that returns anything but 0, it's got a link to the javascript. Works on any I've just quickly tested, This assumes that the tracking code is not hosted locally. and is on the main page as Google recommend.
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 = "/<script\b[^>]*>([\s\S]*?)<\/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.gs and _trackPageview in all <script> tag
for ($i = 0; $i < count($inside_script[0]); $i++)
{
if (stristr($inside_script[0][$i], "ga.js"))
$flag2_ga_js = TRUE;
if (stristr($inside_script[0][$i], "_trackPageview"))
$flag1_trackpage = TRUE;
}
// Preg Match for UA ID
//5> Extract UA-ID using regular expression
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);
}
}
$ga_obj = new Ga_track();
//1> Set a url for which we have to extract Google Analytic’s UA-ID
$url = "http://www.liftsuggest.com";
//===========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===========//
$ua_id = $ga_obj->get_ga_implemented($url); //Call to a function to extract details
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)/. Check PHP code to check existence of Google Analytics on a webpage and extract UA-ID for details
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.