How to count banner impressions and clicks

We have a small php script that displays random advertisements on our website. Banners are served from any place that we designated.

What I really would like to know, or point in the right direction, can we somehow compare the number of impressions received by each banner and, possibly, the number of clicks on this banner.

I am sure that this can be done in php and saved in db. It just doesn't make sense. I searched on Google, and it seems that everything I could find came back to 2002 and 2003.

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 run the inclusion request)

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

Any help appreciated

+6
source share
4 answers

I see that you have already chosen the answer, so I'm not sure that you understand all this, but I wrote a short textbook for you. Finally, everything is done, hope it still helps you.

Your method seems to work great for serving banners, but if you are going to get into the databases and track clicks / impressions, I would advise you to do anything. So save the properties of your banner in the database. I am going to get ahead and assume that your server / web host allows several free MySql databases.

What you need to do is create a database as well as User / Admin for the database. You will then access the database using the MySql manager; most web hosts provide phpMyAdmin . Once you get into the database, you need to configure table to write your data.

This is how I want you to install it:

 |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 have the database, here comes the tricky part, PHP. I pretty much did it for you, but it has not been tested, so I'm sure there will be errors that you will have to solve. But he 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 URL if(!isset($_GET['Clicked'])){ // if the Clicked parameter is not set, we came to the page normally // Let 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 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 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 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 get the ID of the banner that was clicked from the Clicked parameter $id = (int)$_GET['Clicked']; // now let increment the Clicks field for the banner mysql_query("UPDATE banners SET Clicks = Clicks + 1 WHERE ID = '" . $id . "'") or die(mysql_error()); // now let select the banner from the database so we can redirect to the URL that 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); ?> 

Luck

+11
source

why don't you just let Google Analytics do this for you? Turn off an event when a link is clicked and let Google capture it?

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

You can store $num in the database quite easily to get the number of readings. Clicks require action on the client side. The easiest way is to call the javascript function, which is taken into account when you click on the banner through AJAX:

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

Then your javascript ( countClick() ) function executes AJAX, which tells the server about the banner click.

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

+1
source

Here are my 2 cents if you have analytics on our website:

Use the following code to display the 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> 

Explain:

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

The classic href link with the class declaration, links to the site, the target opens in a new tab. Easy.

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

This is the latest Google Analytics.js event tracking, the onclick event code, which basically says hey you clicked this link, so "send" this "event" to my analytic "Events" (which can be checked in the "Events" section in real time "or" Behavior> Events "). "Block-3-Ads" is the area on my website that I personally know as the area in which I place ads, in particular its right side area, but that may be all you want, so make your type a broad category in which you will have different links that you want to track. "Click" is just the type of event you want to track, it can be anything. "The-Cat-Is-Ginger-Ad" is a special ad that I want to track and have information about it.

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

Then you have img with src. Easy.

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

Then you have the onload event, which fires when the image is loaded, it says: “send” this “event”, classify it as the “Block-3-Ads” event, basically loading the image is considered as 'Impression' before it clicks but not since this little "onload" is called upon loading, it’s not a click, but loading / displaying, and again, in particular, the loaded ad - "The-Cat-Is-Ginger" -A ', and, finally, the parameter 'nonInteraction' is passed as 1, which simply tells Google that you are tracking the interaction event.

His pretty explanator, maybe I scored too much.

WARNING. This is not ideal for loading a page, and the ad may be below the viewing area, out of the user's field of view, and it still seems that it’s inaccurate, so in the next I’ll work on the first run of the impression code, when the user actually scrolls to the ad itself and looks at it, but does not start it every time the page loads this image.

+1
source

Source: https://habr.com/ru/post/907358/


All Articles