Skip to main content
Easton's stuff

Main navigation

  • Code
  • SnackLinux
  • Home

Breadcrumb

  1. Home

Google Analytics to Twitter bot

Google Analytics to Twitter bot

By Easton , 5 December, 2011

The last week, I've been tinkering with Google's Analytics API. What I wanted to do was have daily reports on my website which then  tweets the difference from yesterday to today. The ideal situation was I would have it SMS me the results. The problem is that my phone carrier (Rogers) complicates things. For most carriers, you can email a carrier-supplied address (5555555@att.com for example) which in turn would send an SMS to that address. But, what Rogers does is once you receive a text, you have to respond back with REPLY to view it. Stupid huh.

To get around this, I subscribe to my bots account via text message and for every new tweet, I get a text message.

What you need: Google Analyrtics API class [download] and twitteroauth [download]

For this to work, you need to create an app on Twitter Developers which creates your consumer key, consumer secret, OAuth secret and token. You also need to create gabot.txt for the bot to cache to.

See the code below for the bot or download at the bottom.


 

 

           setProfile('ga:XXXXXXX');
    $website_info = $ga->getWebsiteProfiles();
    // set the date range we want for the report - format is YYYY-MM-DD

    $to = date("Y-m-d", strtotime("now"));
    $from = date("Y-m-d", strtotime('Yesterday'));
    $ga->setDateRange($from, $to);

    // get the report for date, showing pageviews and visits
    $report = $ga->getReport(
            array('dimensions' => urlencode('ga:date'),
                'metrics' => urlencode('ga:pageviews,ga:visits'),
                #'filters'=>urlencode('ga:country=@Canada'),
                'sort' => '-ga:pageviews'
            )
    );

    //convert the associative array to a numeric array so we dont have to use the $from date as a key
    $report_n = array_values($report);
    //what we're going to tweet
    //0 = today 1 = yesterday
    $stats = $website_info[0]['title'] . " today: pageviews: " . $report_n[0]['ga:pageviews'] . " - visits: " . $report_n[0]['ga:visits'];

    //string to write in file
    $string = $report_n[0]['ga:pageviews'] . '#' . $report_n[0]['ga:visits'];


    //difference from yesterday to today
    $visits_difference = $report_n[1]['ga:visits'] - $report_n[0]['ga:visits'];
    $pageviews_difference = $report_n[1]['ga:pageviews'] - $report_n[0]['ga:pageviews'];


    //open the file for the old saved data
    $fh = fopen($file, 'r');
    $data_read = fgets($fh);
    fclose($fh);


    //compare read data to latest data
    if ($data_read != $string) {

        $fh = fopen($file, 'w') or die("can't open file");
        fwrite($fh, $string);
        fclose($fh);

        //tweet!
        $connection = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
        $connection->get('account/verify_credentials');
        $connection->post('statuses/update', array('status' => $website_info[0]['title'] . " difference: visits: $visits_difference pageviews: $pageviews_difference"));
        
        var_export($connection->http_info);

        echo $stats;
    }
} catch (Exception $e) {
    print 'Error: ' . $e->getMessage();
}

?>
           

 

Tags
php
script
analytics
Attachment Size
googlebot.txt (2.58 KB) 2.58 KB
  • Add new comment

Navigation

  • GitHub Profile
  • SnackLinux
  • Popular pages
    • Restoring a Macintosh Plus
    • Getting root access on a $10 Aliexpress Wifi repeater
    • Remote code execution on no-name wifi repeaters: Part 2
    • Building your own handheld GPS with an ESP32
    • Remote code execution with Hitron CGNM-2250
    • NES controller and a RaspberryPi
    • 4000 series CMOS 24 hour clock
  • Toyota Overland
RSS feed



 

Powered by Drupal