如何检查网站是否有谷歌分析或不使用CURL?

时间:2011-06-13 08:52:52

标签: php curl

我需要检查网站是否使用谷歌分析或不用PHP

3 个答案:

答案 0 :(得分:2)

您可能无法可靠地执行此操作,因为它可以在站点使用的任何脚本中进行处理。

但是,大多数人都会遵循Google提供的示例代码。如果它足够接近,只需使用cURL获取HTML内容并为该代码块解析它。

答案 1 :(得分:1)

类似的东西:

curl DOMAINNAME | grep -c google-analytics.com

如果返回0以外的任何内容,则会有一个指向javascript的链接。适用于任何我刚刚快速测试过的!这假定跟踪代码不是本地托管的,并且位于Google推荐的主页上。

答案 2 :(得分:0)

创建一个PHP文件并将其命名为Ga_track.php。写下面的代码。

<?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===========//

?>

如果您有多个页面,则可以将它们存储在数据库或数组中,并通过从数据库中提取每个页面以循环方式使用前面提到的步骤。 (第2组)/。查看PHP code to check existence of Google Analytics on a webpage and extract UA-ID了解详情