Search code examples
htmlformsgoogle-analyticsreal-timeanalytics

How to graph specific page views in real-time?


I'd like to link to three pages (a, b, c) in an email. When one of those pages are clicked/viewed, then I'd like for that page to display a thank you message and a chart that displays the actual page views to the moment for those three pages.

In essence, I'm trying to create a super-simple form-less poll.

It seems like vast overkill to create a mysql table, etc to record and display the comparative count.

What's the simplest, lightest way of accomplishing what I want to do?


Solution

  • So I figured out a method using PHP. Posting it here for others, and in case somebody can make it better.

    You will need to create a file for tracking each vote. You will also need to create a landing page for each vote type.

    So in the example -- landing on a.php causes a.txt to increment up by 1.

    A cookie is set in order to limit duplicate votes that would otherwise happen if the page reloaded. (Recognized flaw = some people/browsers block cookies.)

    At the top of the PHP page:

        <?php
        $cookie_name = "poll1234";   // a cookie is used to prevent duplicate votes
        $cookie_value = "completed"; 
        $myFile = "a.txt";   // you'll need to create this file & populate with a 0
    
        $voteA = file_get_contents($myFile);
        $voteB = file_get_contents("b.txt");
        $voteC = file_get_contents("c.txt");
        setcookie($cookie_name, $cookie_value, time() + 86400, "/"); // 86400 = 1 day
        if(!isset($_COOKIE[$cookie_name])) {
            $voteA += 1;
            $changedFile = fopen($myFile, "w") or die();
            fwrite($changedFile, $voteA);
            fclose($changedFile);
        }
        ?>
    

    This is the code for a.php -- it would need to be tweaked for use on b.php or c.php

    In the body of the PHP page:

    Use the below code to display the vote count for option A -- change in order to display B or C.

        <?php echo $voteA; ?>