2011-06-13 51 views

回答

2

您可能無法可靠地做到這一點,因爲它可以在網站使用的任何腳本中進行操作。

但是,大多數人確實遵循Analytics提供的示例代碼。如果足夠接近,只需使用cURL獲取HTML內容並解析該代碼塊即可。

1

喜歡的東西:

curl DOMAINNAME | grep -c google-analytics.com 

如果返回任何東西,但0,它有一個鏈接的JavaScript。適用於我剛剛測試過的任何產品!這假定跟蹤代碼不是本地託管的,並且位於Google推薦的主頁面上。

+0

顯然,你需要反編譯這個讓它運行在PHP中。 – 2011-06-13 16:20:20

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的詳細信息