I have been trying to figure out this problem for a few days now with little to no progress, I have a feeling the problem resides in trying to call a javascript function from php. I need to start tracking file downloads on our company website and I've got this working in regular HTML for GA but a lot of our files pull from a database through php and I would like to get this tag working with those files. Nothing is passing through to GA and I can see there is an error when I look at the source in FireFox. Thanks for any insight.
Here is the line of code that's giving me headaches. <a target='_blank' href='/$file' onclick='_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '$filetext']);'><img alt='$filetext' src='/$image' border='0'/></a>
FireFox shows this part of the code in red as an error, if I take out the onclick portion the error goes away, the page displays fine but nothing is being sent to GA like I mentioned before:
<a target='_blank' href='/images/datasheets/IDS-66 Amphe-10G.pdf' onclick='_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', 'Amphe-10G']);'>
Here is the full PHP code:
<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> </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> </td>"; //If there are no more records at the end, add a blank column
}
// }
// }
while($row);
echo "</table>";
?>
</tr>
</tbody>
</table>
</div>
There seem to be a problem in your code, where you define analytics tracking:
onclick='_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '$filetext']);'>
Your using single quotes, maybe because you use double quotes for your echo, you should replace it with:
onclick=\"_gaq.push(['_trackEvent', 'Downloads', 'Datasheet', '$filetext']);\">
And still be able to use the same double quotes on echo.
<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>
Use this instead then :)
Just replace you whole line of code, starting with "div align='center'>", with the one provided above.
这是有效的最终代码!
<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>
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.