debuggable

 
Contact Us
 
36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44

New Google Analytics API / DataSource!

Posted on 18/12/07 by Felix Geisendörfer

Hey folks,

sorry it took me forever, but after my old Google Analytics API fell apart due to the fact that Google published a new interface that also came with new reports / exporting formats I didn't have the time to come up with a new one.

Anyway, in a very productive CakePHP session with nate a couple weeks ago in Atlanta I finally implemented some new HttpSocket stuff that was needed (minimal support for cookies / ssl) for creating the new API as a datasource. Now that its done I'm fairly happy with the outcome of the new API : ).

Without further words: Download the 1.0 version here (A SVN:HEAD version of Cake 1.2 is highly recommended).

It doesn't have pretty API comments yet, so I'm gonna make up for it with a little tutorial:

Step 1: Set up your connection credentials

After you downloaded the API and placed it in app/models/datasources/google_analytics_source.php, open up your app/config.database.php and add the following lines:

var $analytics = array(
  'datasource' => 'google_analytics',
  'user' => 'my-account@gmail.com',
  'password' => 'my-password',
);

Naming your connection $analytics is a choice, anything else will do as well.

Step 2: Set up a little test controller:


class AnalyticsController extends AppController{
    var $uses = array();
    var $Analytics = array();

    function beforeFilter() {
        App::import('ConnectionManager');
        $this->Analytics =& ConnectionManager::getDataSource('analytics');
    }

    function list_profiles() {
        // List all profiles associated with your account
        $profiles = $this->Analytics->listSources();
        debug($profiles);
        exit;
    }
   
    function show_reports() {
// The quickest way to test if the API is working for you.
        $report = $this->Analytics->report('Dashboard');
        debug($report);
       
// Use a specific profile id (see list_profiles action above), do:
        $report = $this->Analytics->report(array(
            'profile' => '290723',
            'report' => 'Dashboard'
        ));
        debug($report);

// You can also reference your profile by name instead
        $report = $this->Analytics->report(array(
            'profile' => 'www.thinkingphp.org',
            'report' => 'Dashboard'
        ));
        debug($report);

// Retrieve the raw XML instead of an array to parse it yourself (lets say using SimpleXml in PHP5):
        $report = $this->Analytics->report(array(
            'profile' => 'www.thinkingphp.org',
            'report' => 'Dashboard'
        ), true);
        debug($report);
       
// Retrieve a PDF report (make sure you set the right header before displaying):
        $report = $this->Analytics->report(array(
                'profile' => 'www.thinkingphp.org',
                'report' => 'Dashboard',
                'format' => 'pdf'
            ), true);
        debug($report);

// Set some criteria on the report:
        $report = $this->Analytics->report(array(
                'profile' => 'www.thinkingphp.org',
                'report' => 'Dashboard',
                'from' => '2007-12-17',
                'to' => '2007-12-18'
            ));
        debug($report);
       
/**
 * Small Reference of options:
 *
 * profile: The id or name of the profile (optional, default = first profile)
 * report: A report string like 'Dashboard', 'TrafficSources', 'Keywords' (optional, default = 'Dashboard')
 * from: A yyyy-mm-dd formated date (optional, default = 1 month ago)
 * to: A yyyy-mm-dd formated date (optional, default = today)
 * tab: 0-4 (optional, default = 0)
 * view: 0-9+ (optional, default = 0)
 * format: 0-3 or 'pdf', 'xml', 'csv', 'tsv' (optional, default = 'xml')
 * compute: 'average', other options unknown? (optional, default = 'average')
 * query: Here you can manually pass (and overwrite) any part of the raw HTTP GET query made to Google Analytics. (optional, default = array())
 */

    }
}

Now you might wonder how get the list of reports that are available. Well usually that is pretty simple, just check the name of the menu entry you have open when browsing through the google analytics web interface and try using it in CamelCase. For example the 'Search Engines' view under traffic sources is the same as the 'SearchEngines' report. If this does not work, then use Firebug or something else to find the destination of one of the export links on the screen you are looking at. For example on the 'Direct Traffic' screen (under traffic sources as well), the xml export link points to 'export?fmt=1&id=5879334&pdr=20071116-20071216&cmp=average&&rpt=DirectSourcesReport' which means that the name of the report you need for this API is 'DirectSources'. If I get around I might create some listReports() function, but for now this should do the trick.

Another tip: If you are overwhelmed by the information returned by the default xml reports, try using the CSV / TSV ones, they should usually contain less unneeded information, but you'll have to parse them yourself.

Ok, I hope you guys enjoy and let me know if you have any questions / suggestions / problems.

-- Felix Geisendörfer aka the_undefined

Note: This data source uses web scraping. This means that Google could easily break it by making changes to the infrastructure of Google Analytics again. I also haven't heard back from google about their legal views on this technique since Late May 2006, but so far I haven't heard about Google approaching any vendors providing similar access to their service using web scraping either, which is a good sign.

 

CakePHP 1.2 Manual

Posted on 14/11/07 by Tim Koschützki

I am glad to tell you that the temporary Cake 1.2 Manual has been set online. As you can imagine this is so cool, because now all the new cool stuff like the config class, url extensions, etc. are documented. There are still some parts missing, like caching (doh, I was responsible for it *hides behind tree*), but they should soon be online as well.

Come on hop over and get your share of the cake. :)

 

CakePHP Bleeding Edge - Google Group

Posted on 3/11/07 by Felix Geisendörfer

Hey folks,

I was thinking about creating a regular category on my blog for this first. But then I remembered how horribly I am about updating this site sometimes and decided a community effort would be better. What am I talking about? A new google group called CakePHP Bleeding Edge for those of us how keep up with the adventure of running SVN:HEAD. The idea is to share critical /useful changesets, problems and solutions that come our way when we update our applications to the latest cake version. All I would like is for people to follow some simple guidelines so its easier to discuss and use the contents of the list.

Please join and share your knowledge / questions!

-- Felix Geisendörfer aka the_undefined

 

Model::save() now returns an array!

Posted on 3/11/07 by Felix Geisendörfer

Just got bitten by this one when updating to the latest version of CakePHP. If you use code like this in your app:

if ($this->User->save() === true) {
   // Do stuff
}

Then you're in for a surprise. Because as of revision 5895 Model::save() now returns Model::data on success if its not empty. Now most of us do not use strict comparison for checking the return value of Model::save(), but I was stupid enough to do it as part of my new "fail hard fast" strategy : ). So suddenly I had stuff blowing up all over the place.

Oh well, thats the price you pay for being one of the cool kids and staying with SVN:HEAD. However, its definitely worth it because there are new goodies added to the core every day. And in fact, Model::save() returning an array is one of them. Because previously you had to make your own copy of Model::data if you needed it after a save() operation because Model::save() always clears Model::data for you.

So its all good, just be sure to check your app for strict Model::save return value comparisons to avoid surprises.

-- Felix Geisendörfer aka the_undefined

 

CakePHP Pluralize Helper

Posted on 2/11/07 by Tim Koschützki

Hey folks,

here is a small helper file for the CakePHP framework that will help you turn a subject into the pluralized form depending on a parameter. Here is the code:

  class PluralHelper extends Helper {
    function ize($s, $c) {
      if ($c != 1) {
        return $c . ' ' . Inflector::pluralize($s);
      }
      return $c . ' ' . $s;
    }
  }

Save the code into a file called "plural.php" and place it in app/views/helpers/. After you activated the helper in your controller(s), you can use it as follows in your views:

echo $plural->ize('article', count($articles));

..which will output "article" if the second parameter is 1, or "articles" if it is 0 or > 1. This is neat to display text as in "You have writte SOME_NUM article(s) already". You get the idea. It uses Cake's Infelctor class, so that special cases like "baby/babies" are taken care of as well.

One note: If you have a CommonHelper already, it would be good to copy the code into a "pluralize" method in the CommonHelper instead. Why? Because as of now the PluralHelper only has this one method and there does not justify to be a class on its own. :]

 
36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44