9

We have a small php script, that displays random adverts on our site . The banners are served from any location we designate.

What I really would like to know, or be pointed in the right direction is, can we somehow collate the number of impressions each banner gets and possibly the click count on that banner.

I am sure this can be done in php, and stored in a db. Just havent got a clue. I searched on Google, and seemingly everything I could find harks back to 2002 and 2003 lol.

Here is our script:

<?php
$Img1 = "path to image folder/banner.png";
$Alt1 = "alt text for banner";
$Url1 = "http://www.externaldomain.com";

$Img2 ="path to image folder/banner.png";
$Alt2 = "alt text for banner";
$Url2 = "http://www.externaldomain.com";

$Img3 ="path to image folder/banner.png";
$Alt3 = "alt text for banner";
$Url3 = "http://www.externaldomain.com";

$Img4 ="path to image folder/banner.png";
$Alt4 = "alt text for banner";
$Url4 = "http://www.externaldomain.com";

$Img5 ="path to image folder/banner.png";
$Alt5 = "alt text for banner";
$Url5 = "http://www.externaldomain.com";

$num = rand (1,5);

$Image = ${'Img'.$num};
$Alt = ${'Alt' .$num};
$URL = ${'Url'.$num};

Print "<a href=\"".$URL."\"><img src=\"".$Image."\" alt=\"".$Alt."\" /</a>"; ?>

To initiate the above code ( we fire an include request )

<?php include ("../adserver/thescriptabove.php"); ?>
4

5 回答 5

14

我看到你已经选择了一个答案,所以我不确定你是否完全理解,但我正在为你写一个小教程。终于搞定了,希望对你有帮助。

您的方法似乎适用于提供横幅广告,但如果您要进入数据库并跟踪点击/印象,我建议您全力以赴。因此,也将您的横幅属性存储在数据库中。我将继续前进并假设您的服务器/网络主机允许一些免费的 MySql 数据库。

您需要做的是创建一个数据库,以及该数据库的用户/管理员。然后您将使用 MySql 管理器访问数据库,大多数网络主机都提供phpMyAdmin. 进入数据库后,您需要设置一个table来记录您的数据。

以下是我希望您设置的方式:

|Table Name: Banners      |
|-------------------------|
|Field:    | Type:        |
|-------------------------|
|ID        | int(5)       | The Primary Key and Autoincrement attributes should be set on the ID field as well
|Image     | varchar(100) |
|Url       | varchar(100) |
|Caption   | varchar(100) |
|Views     | int(10)      |
|Clicks    | int(10)      |

现在您已经完成了数据库,接下来是困难的部分,PHP。我已经为你做了很多,但它未经测试,所以我相信会有错误,你必须解决。但它应该为您指明正确的方向,并帮助您学习。

<?PHP

// First of all, we need to connect to the MySql server
// For more info, check out: http://php.net/manual/en/function.mysql-select-db.php
$conn = mysql_connect("localhost", "username", "password");
if(!$conn){
    die('Could not connect to the MySql Server ' . mysql_error());
}

// Now that we've connected to the MySql sever, we need to select the database
// More info can be found on the same link as above
$db_selected = mysql_select_db('my_database', $conn);
if(!$db_selected) {
    die ('Could not select the MySql Database: ' . mysql_error());
}

// Now we need to check the URL parameters to see, if we came to this page normally or because a banner was clicked
// If normally, we serve a random banner and increment the Views field in the database
// Otherwise, we increment the Clicks field and direct the user to the banner's URL


if(!isset($_GET['Clicked'])){
    // if the Clicked parameter is not set, we came to the page normally

    // Let's select a random banner from the database
    $result = mysql_query("SELECT * FROM banners ORDER BY RAND() LIMIT 1") or die(mysql_error());
    $row = mysql_fetch_array(result);   

    // Now let's increment the Views field for the banner we selected
    mysql_query("UPDATE banners SET Views = Views + 1 WHERE ID = '" . $row['ID'] . "'") or die(mysql_error());

    // let's set the URL to this same page but with the Clicked parameter set
    $url = "banner_server.php?Clicked=" . $row['ID'];

    // Last but not least, we have to output the banner HTML
    // Notice, I set the caption on both the 'alt' and 'title' attributes of the 'img' tag,
    // that's because IE shows the value of the 'alt' tag when an image is hovered,
    // whereas Firefox shows the value of the 'title' tag, just thought you might want that!
    echo "<a href=\"" . $url . "\"><img src=\"" . $row['Image'] . "\" alt=\"" . $row['Caption'] . "\" title=\"" . $row['Caption'] . "\" /></a>";

}else{
    // Otherwise, increment the Clicks field and redirect

    // First, let's get the ID of the banner that was clicked from the Clicked parameter
    $id = (int)$_GET['Clicked'];

    // now let's increment the Clicks field for the banner
    mysql_query("UPDATE banners SET Clicks = Clicks + 1 WHERE ID = '" . $id . "'") or die(mysql_error());

    // now let's select the banner from the database so we can redirect to the URL that's associated with it
    $result = mysql_query("SELECT * FROM banners WHERE ID = '" . $id . "'") or die(mysql_error());
    $row = mysql_fetch_array(result);

    // redirect to the URL
    header("location: " . $row['Url']);
}


// Close the MySql connection
mysql_close($conn);

?>

祝你好运

于 2012-02-01T00:45:36.967 回答
6

你为什么不让谷歌分析为你做呢?单击链接时触发事件并让谷歌捕获它?

onclick="_gaq.push(['_trackEvent', 'Event Name', 'click', 'Button title/name']);"
于 2012-02-01T00:55:40.953 回答
1

这是我的 2 美分,假设您在我们的网站上有分析:

输出链接时使用以下代码:

<a class="ad" href="http://thecatisginger.com/" target="_blank" onclick="ga('send', 'event', 'Block-3-Ads', 'Click', 'The-Cat-Is-Ginger-Ad');"><img src="http://citybymouth.com/wp-content/uploads/2015/02/TCIG-Advert.jpg" onload="ga('send', 'event', 'Block-3-Ads', 'Impression', 'The-Cat-Is-Ginger-Ad', {'nonInteraction': 1});" /></a>

解释:

<a class="ad" href="http://thecatisginger.com/" target="_blank"

经典链接带有“广告”类的 href 链接,指向网站的链接,目标在新选项卡中打开。简单的。

onclick="ga('send', 'event', 'Block-3-Ads', 'Click', 'The-Cat-Is-Ginger-Ad');">

这是较新的“analytics.js”谷歌事件跟踪,onclick 事件代码,基本上说,嘿,你点击了这个链接,所以“发送”这个“事件”到我的分析“事件”(可以在下面检查“实时 > 事件”或“行为 > 事件”)。“Block-3-Ads”是我网站上的区域,我个人知道是我放置广告的区域,特别是它的右侧边栏区域,但它可以是任何你想要的,所以让你的成为一个广泛的类别类型的东西,比如框,在其中您将拥有要跟踪的不同链接。“点击”只是您要跟踪的事件类型,可以是任何东西。“The-Cat-Is-Ginger-Ad”是我想要跟踪并获取相关信息的特定广告。

<img src="http://citybymouth.com/wp-content/uploads/2015/02/TCIG-Advert.jpg" 

然后你有一个带有 src 的 img。简单的。

onload="ga('send', 'event', 'Block-3-Ads', 'Impression', 'The-Cat-Is-Ginger-Ad', {'nonInteraction': 1});" />

然后你有一个加载图像时触发的 onload 事件,它说“发送”这个“事件”,将其归类为“Block-3-Ads”事件,基本上图像加载被计为“印象”,在点击之前,但不是因为在加载时调用了这个小“onload”,它不是点击,而是加载/展示,同样,加载的广告是“The-Cat-Is-Ginger-Ad” ,最后,将“nonInteraction”参数传递为 1,这只是告诉 google 您正在跟踪非交互事件。

它非常不言自明,尽管我可能输入了太多。

警告:这并不完美,因为页面可能会加载,并且广告可能在视口下方,用户看不到,但仍然会产生印象,这是不准确的,所以接下来我将努力触发当用户实际滚动到广告本身并查看它时,只展示一次展示代码,而不是在每次页面加载该图像时触发它。

于 2015-04-06T09:58:02.397 回答
1

您可以$num很容易地将其存储在数据库中以获取展示次数。点击需要客户端操作。最简单的方法是调用一个 javascript 函数,该函数在通过 AJAX 单击横幅时计数:

print "<a href=\"".$URL."\" onclick=\"countClick($num);\"><img src=\"".$Image."\" alt=\"".$Alt."\" /</a>";

然后让您的 javascript 函数 ( countClick()) 执行 AJAX,告诉服务器横幅已被点击。

另一种方法是有一个 passthru 页面:mysite.com/linkout.php?q=www.google.com然后linkout.php计算该链接并更新数据库,然后将它们重定向到 URL 中传递的变量。

于 2012-01-31T23:33:51.117 回答
0

感谢@Saad Imran 的代码,也非常感谢问题海报

如果其他人需要它以供以后使用,请在 php 7 中更新代码

注意:相同的数据库表,然后将此代码存储在banner.php文件中

<?php
    // to only count views

    // select a random banner from the database to show
    $rows = $db->QueryFetchArrayAll("SELECT * FROM banners ORDER BY RAND() LIMIT 1");
foreach ($rows as $row) {

    // Now let's increment the Views field for the banner we selected

    $url = "/banner.php?clicked=" . $row['ID'];

    echo "<a href=\"" . $url . "\"><img src=\"" . $row['Image'] . "\" alt=\"" . $row['Caption'] . "\" title=\"" . $row['Caption'] . "\" /></a>";
}
    $db->Query("UPDATE banners SET Views = '".round($row['Views'] + 1)."' WHERE id = '" . $row['ID'] . "'");

// to count clicks need an if statement 
if(isset($_GET['clicked'])){
        $db->Query("UPDATE banners SET Clicks = '".round($row['Clicks'] + 1)."' WHERE id = '" . $row['ID'] . "'");

header("location: " . $row['Url']);


}
?>

大家好运 :)

于 2020-04-13T11:59:19.920 回答