如何计算横幅展示次数和点击次数

发布于 2025-01-01 09:03:31 字数 1136 浏览 3 评论 0原文

我们有一个小的 php 脚本,可以在我们的网站上显示随机广告。横幅可以从我们指定的任何地点投放。

我真正想知道的,或者被指出正确的方向是,我们能否以某种方式整理每个横幅获得的展示次数以及该横幅上可能的点击次数。

我确信这可以在 php 中完成,并存储在数据库中。只是还没有得到任何线索。我在 Google 上搜索,似乎我能找到的所有内容都可以追溯到 2002 年和 2003 年,哈哈。

这是我们的脚本:

<?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>"; ?>

启动上述代码(我们触发包含请求)

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

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"); ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

油焖大侠 2025-01-08 09:03:31

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

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

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

我希望您按照以下方式进行设置:

|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);

?>

祝你好运

I see that you already selected an answer, so I'm not sure if you figured it all out, but I was writing out a little tutorial for you. Finally got it done, hope it still helps you out.

Your method seems to be working fine for serving banners, but if you're going to get into databases and tracking clicks/impressions, I would suggest that you go all in. So store your banner properties in the database as well. I'm going to get ahead and assume that your server/web host allows for a few free MySql databases.

What you need to do is create a database, as well as a User/Admin for the database. Then you're going to access the database with a MySql manager, most web hosts provide phpMyAdmin. Once you're inside the database, you need to set up a table to record your data.

Here's how I want you to set it up:

|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)      |

Now that you've got the database done, here comes the hard part, the PHP. I've pretty much done it for you, but it's untested, so I'm sure there will be bugs, that you will have to work out. But it should point you in the right direction, and help you learn.

<?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);

?>

Good luck

陪你搞怪i 2025-01-08 09:03:31

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

onclick="_gaq.push(['_trackEvent', 'Event Name', 'click', 'Button title/name']);"

why dont you just let google analytics do it for you? Fire off an event when the link is clicked and let google capture it?

onclick="_gaq.push(['_trackEvent', 'Event Name', 'click', 'Button title/name']);"
蝶…霜飞 2025-01-08 09:03:31

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

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

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

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

You can store the $num in the database pretty easy to get your impression count. Clicks require client side action. The easiest way is to call a javascript function that counts when the banner is clicked via AJAX:

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

Then have your javascript function (countClick()) execute the AJAX that will tell the server the banner has been clicked.

Another way is to have a passthru page: mysite.com/linkout.php?q=www.google.com and then have linkout.php count that link and update the database, and then redirect them to the variable passed in the URL.

小伙你站住 2025-01-08 09:03:31

这是我的 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"

经典链接带有“ad”类的 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 您正在跟踪非交互事件。

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

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

Here are my 2 cents, assuming you have analytics on our site:

Use the following code when outputting a link:

<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>

To explain:

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

Classic link a href link with class 'ad', links to a site, target opens in new tab. Easy.

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

This is the newer 'analytics.js' google event tracking, onclick event code, that bascially says, hey, you've clicked this link, so 'send' this 'event' to my analytics 'Events' (which can be checked under "Realtime > Events" or "Behaviour > Events"). "Block-3-Ads" is the area on my website I personally know as the area I put ads, specifically its a right hand sidebar area, but it can be anything you want, so make yours a broad category type thing, like a box, inside which you will have different links you want to track. "Click" is simply the type of event you want to track, can be anything. "The-Cat-Is-Ginger-Ad" is this specific ad I want to track and have the information about.

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

Then you have an img with a src. Easy.

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

Then you have an onload event that fires when the image is loaded, it says, 'send' this 'event', categorise it as a 'Block-3-Ads' event, basically the image loading is counted as an 'Impression', before it was click, but not since this little 'onload' is called when it loads, its not a click, but a load / impression, and again, specifically, the ad loaded is 'The-Cat-Is-Ginger-Ad', and finally, there is passing the 'nonInteraction' parameter as 1, which is just telling google you are tracking a non interaction event.

Its pretty self explanatory, tho I may have typed too much.

WARNING: This is not perfect in that the page may load, and the ad may be below the viewport, out of sight of the user, and still get an impression, which is not accurate, so next I'll be working on firing the impression code just once when the user actually scrolls to the ad itself, and views it, instead of firing it every time a page loads that image.

醉梦枕江山 2025-01-08 09:03:31

感谢@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']);


}
?>

好祝大家好运:)

Thanks to @Saad Imran for the code and also big thanks to the question poster

Bellow the update of the code in php 7 if someone else need it for later use

Note : The same Database table and then store this code in banner.php file

<?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']);


}
?>

Good luck everyone :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文