微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何检查网站是否有Google Analytics分析或不使用CURL?

| 我需要检查网站是否使用了Google Analytics(分析)     

解决方法

        您可能无法可靠地做到这一点,因为它可能会在网站使用的任何脚本中被掩埋。 但是,大多数人确实遵循Google Analytics(分析)提供的示例代码。如果足够接近,只需使用cURL获取HTML内容,然后为该JavaScript代码块进行解析。     ,        就像是:
curl DOMAINNAME | grep -c google-analytics.com
如果返回的值不是0,则指向javascript。适用于我刚刚经过快速测试的任何内容!假设跟踪代码不在本地托管,并且按照Google的建议位于主页上。     ,        创建一个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,$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代码以检查网页上是否存在Google Analytics(分析),并提取UA-ID以获取详细信息     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。