Vote of Interest
Sometimes I wonder how much interest a certain Possibilities article has or doesn't have.
We do have email-open statistics. But that measures mostly how well the subject line resonated with the recipient.
And we have click-through statistics. Those are a better measure, but not definitive. The interested reader may have gotten all they need from the section of the article published in the newsletter and have no need for more information.
Between the two statistics, a person can arrive at a somewhat intuitive understanding of how much interest an article had.
Sometimes, however, direct feedback is best.
The system developed for that is described in this article.
But before we get to how the system works, tell me. Is this something you're interested in? Click the applicable image to let me know.
No |
Maybe |
Yes |
When you clicked one of the "vote" images, your browser was taken to a confirmation page. In this case, it's this very same article, but with a special message at the beginning. Those who arrive here without clicking a vote link don't see the special message.
Implementing the System
Implementation has three parts.
-
The vote images and links (which you see above).
-
The confirmation page.
-
The PHP software that keeps track of votes.
We'll do these in reverse order. The reason is because the first two steps require the steps following them to already be working.
The PHP software that keeps track of votes —
The PHP software is uploaded to any location on your server reachable by URL.
The source code is below. There are two places to customize, addressed below the source code box.
<?php /* Article Vote Version 1.0 September 10, 2017 Will Bontrager Software LLC https://www.willmaster.com/ This software is provided "AS IS," without any warranty of any kind, without even any implied warranty such as merchantability or fitness for a particular purpose. Will Bontrager Software LLC grants you a royalty free license to use or modify this software provided this notice appears on all copies. */ /* CUSTOMIZATION */ // // If you wish to receive an email every time a vote // is received, specify the email address and email // subject line between the quotes. Otherwise, remove // everything between the quotes. $EmailNotification = "name@example.com"; $EmailSubjectLine = "A vote!"; // // If you wish to log the votes, specify the location // and name of the log file. Otherwise, remove everything // between the quotes. (The directory where the log file // will be may need permissions 777). The log is // formatted as a CSV file. $LogFile = "/vote/articlevote.csv"; // /* End of Customization */ if( empty($_GET['vote']) ) { exit; } $_GET['vote'] = stripslashes($_GET['vote']); if( ! empty($LogFile) ) { if( strpos($LogFile,'/')===0 ) { $LogFile = "{$_SERVER['DOCUMENT_ROOT']}$LogFile"; } $logline = array(); $logline[] = str_replace('"','""',$_GET['vote']); $logline[] = str_replace('"','""',date('r')); $logline[] = str_replace('"','""',$_SERVER['REMOTE_ADDR']); file_put_contents($LogFile,'"'.implode('","',$logline)."\"\n",FILE_APPEND); } if( (!empty($EmailNotification)) and strpos($EmailNotification,'@') ) { if( empty($EmailSubjectLine) ) { $EmailSubjectLine = "A vote from {$_SERVER['PHP_SELF']}"; } $content = <<<CONTENT Vote: {$_GET['vote']} IP: {$_SERVER['REMOTE_ADDR']} CONTENT; mail($EmailNotification,$EmailSubjectLine,$content,"From: $EmailNotification"); } ?>
Customization Notes
There are two places to customize. One has to do with email when a vote occurs and the other has to do with recording the vote in a file.
-
Emailing votes:
To specify whether or not you want to receive an email whenever a vote occurs, change the lines colored blue in the source code:
$EmailNotification = "name@example.com"; $EmailSubjectLine = "A vote!";
If you do want to receive an email whenever a vote occurs, change
name@example.com
to the email address where the vote shall be sent to. TheA vote!
email subject line may also be changed.If you do not want to receive emails with vote information, remove the
name@example.com
email address so only the quotation marks are left (as in$EmailNotification = "";
). TheA vote!
subject line may be left as is. -
Logging votes:
To specify whether or not you want to log the votes, change the line colored red in the source code:
$LogFile = "/vote/articlevote.csv";
If you decide to log the votes, the log will be a CSV file ready to import into your spreadsheet.
If you do want to log the votes, change
/vote/articlevote.csv
to the location of the log file. The location is the domain directory location and file name. The directory must exist, and it might or might not need 777 permissions. Try without, first. If no log file is created, change the directory's permission to 777.If you do not want to log the votes, remove the
/vote/articlevote.csv
file location so only the quotation marks are left (as in$LogFile = "";
).If the log is updated, it contains the vote information and the server's default current date and time.
Installing
To install the PHP software, name it articlevote.php
or other file name ending with .php
and upload it to a location accessable by URL. Make a note of the URL, as you'll need it in the next step.
The confirmation page —
The confirmation page is the page people will arrive at when they vote.
Somewhere in the source code of the confirmation page, put the following line of PHP. The line can be anywhere on the page except within other PHP code.
<?php file_get_contents('http://example.com/vote/articlevote.php?vote='.$_GET['vote']); ?>
Replace http://example.com/vote/articlevote.php
with the URL of the articlevote.php
PHP script you installed in the previous step.
If you wish to present a message to those who arrive after voting, but not to those who access the page directly (like is done with this article), here's how to do it.
<?php if(!empty($_GET['vote'])): ?>
[MESSAGE]
<?php endif; ?>
Replace [MESSAGE]
with your message.
You will need to know the confirmation page's URL before you can do the next step.
The vote images and links —
The voting links in your email can be images, text, or both. Near the top of this article, you see both.
The voting link URL is the confirmation page URL with special parameter ?vote=____
appended.
The ____
part of the special parameter is replaced with the vote. (The replacement may be "yes", "no", "I like it!" or any other text that you want for the vote.)
As an example, if your confirmation page is at http://example.com/vote/voted.php
, then a "Yes" vote link URL would be http://example.com/vote/voted.php?vote=Yes
Here's example code with "Yes", "No", "Not Sure" votes with text links.
<a href="http://example.com/vote/voted.php?vote=Yes">Yes!</a> <a href="http://example.com/vote/voted.php?vote=No">No</a> <a href="http://example.com/vote/voted.php?vote=Not Sure">Not Sure :(</a>
Instead of text for clicking on, images can be used.
<a href="http://example.com/vote/voted.php?vote=Yes"> <img src="http://example.com/yes.png"> </a> <a href="http://example.com/vote/voted.php?vote=No"> <img src="http://example.com/no.png"> </a> <a href="http://example.com/vote/voted.php?vote=Not Sure"> <img src="http://example.com/notsure.png"> </a>
The votes may contain email- or article-identifying information. As an example, the email date or newsletter issue number could be appended. Example: ?vote=Yes:20241105
With identifying information, you'll have the information needed to determine what the vote pertains to when reviewing emails or a spreadsheet with the log file information.
What Happens When a Vote Link Is Clicked
Someone clicks on a vote link in your email or on your website. This is what happens:
-
The person's browser goes to the confirmation page at the vote link URL. The
?vote=____
parameter carries the vote. -
The confirmation page calls the PHP software with the information received in the
?vote=____
parameter. -
The PHP software sends an email with the vote information or logs the vote information or it does both of those actions.
Once the system is implemented, it can be used over and over. Include email- or article-identifying information with the votes so you'll know what the vote pertains to when they are received.
(This article first appeared in Possibilities newsletter.)
Will Bontrager