2013-12-09 57 views
0

我一直試圖找出這個問題幾天,現在幾乎沒有任何進展,我感覺問題在於試圖從PHP調用JavaScript函數。我需要開始跟蹤我們公司網站上的文件下載,並且我已經在GA的常規HTML中工作,但是我們很多文件都是通過php從數據庫中提取的,我希望能夠使用這些文件處理這些標記。沒有什麼是通過遺傳算法,當我看到FireFox中的源代碼時,我可以看到有一個錯誤。感謝您的任何見解。PHP中的Google Analytics事件跟蹤onclick標註

這是讓我頭痛的代碼行。 <a target='_blank' href='/$file' onclick='_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '$filetext']);'><img alt='$filetext' src='/$image' border='0'/></a>

火狐紅色顯示代碼錯誤的這一部分,如果我拿出的onclick部分錯誤消失,頁面顯示正常,但就像我之前提到的沒有被髮送到GA:

<a target='_blank' href='/images/datasheets/IDS-66 Amphe-10G.pdf' onclick='_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', 'Amphe-10G']);'> 

以下是完整的PHP代碼:

<div class="download01"> 
<div class="download02"></div> 
<table style="width: 100%" border="0" cellpadding="0" cellspacing="0"> 
<tbody> 
<? 


// Make a MySQL Connection 
mysql_connect("localhost", "amphenol_web", "ampheweb") or die(mysql_error()); 
mysql_select_db("amphenol_sheets") or die(mysql_error()); 

// Retrieve all the data from the "distributors" table 
$query = "SELECT * FROM datasheets ORDER BY filetext"; 


$result = mysql_query($query) or die(mysql_error()); 




$cols = 6;  // Here we define the number of columns 




echo "<table>"; // The container table with $cols columns 
    do{ 
     echo "<tr>"; 
     for($i=1;$i<=$cols;$i++){ // All the rows will have $cols columns even if 
            // the records are less than $cols 
      $row=mysql_fetch_array($result); 


?> 
<? 

$file = $row['file']; 
$image = $row['image']; 
$filetext = $row['filetext']; 

if ($file == ""){echo "<td>&nbsp;</td>";} 
    else {echo 

     "<td valign='top'> 
      <table> 
       <tr valign='top'> 
<td width='120'> 
<div align='center'><a target='_blank' href='/$file' onclick='_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '$filetext']);'><img alt='$filetext' src='/$image' border='0'/></a><br /><a target='_blank' href='/$file' onclick='_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '$filetext']);'>$filetext</a></div> 
</td> 
<tr> 
       <td height='25'> </td> 
       </tr> 
      </table> 
     </td>"; 
} 
      } 
//   else{ 
//    echo "<td>&nbsp;</td>"; //If there are no more records at the end, add a blank column 
      } 
//  } 
// } 

while($row); 
    echo "</table>"; 

?> 
</tr> 
</tbody> 
</table> 
</div> 
+0

請不要鏈接到您的網站。閱讀:http://meta.stackexchange.com/questions/125997/something-in-my-web-site-or-project-doesnt-work-can-i-just-paste-a-link-to-it –

+0

請顯示您的實際呈現的HTML。這與您的PHP源代碼無關。 –

+0

對不起,我刪除了鏈接併發布了FireFox展示的內容。 – Martin

回答

0

似乎有在你的代碼中的問題,在這裏您定義的分析跟蹤:

onclick='_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '$filetext']);'> 

你使用單引號,也許是因爲你用雙引號爲您的迴音,你應該將其替換爲:

onclick=\"_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '$filetext']);\"> 

,仍然能夠使用回聲相同的雙引號。

+0

Jeppe,謝謝,我之前已經嘗試過,只是再次嘗試過,但仍然無法正常工作。它給了我舊的「解析錯誤:語法錯誤,意外的T_STRING,期待','或';'在「需要單引號解決的錯誤。 – Martin

+0

它可以工作,所以你可能在你的設置中有錯誤。無論如何,我發佈了另一個答案,做同樣的事情,但以另一種方式。 – Jeppe

0
<div align='center'><a target='_blank' href='/$file'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '<?php=$filetext ?>']);"><? echo "<img alt='$filetext' src='/$image' border='0'/></a><br /><a target='_blank' href='/$file' onclick='_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '$filetext']);'>$filetext</a></div> 

使用這個代替呢:)

只需更換你的代碼整條生產線,從「DIV ALIGN =‘中心’>」,上面提供的一個。

+0

傑普,這工作!!!!!,非常感謝你。爲了讓它正常工作,我不得不作出一些修改,但你讓我足夠接近。以下是最終的代碼。 – Martin

0

這是最終的代碼!

<div align='center'><a target='_blank' href='/$file'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '<?php echo "$filetext"?>']);"><? echo "<img alt='$filetext' src='/$image' border='0'/></a><br /><a target='_blank' href='/$file'"?> onclick="_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '<?php echo "$filetext" ?>']);"><? echo "$filetext</a></div> 
相關問題