Updated Net::Google::Analytics Example

| 2 Comments

Since I wrote the post Net::Google::Analytics Extended Example Google updated the API and the Perl module was modifed to accomodate. That example is broken and I am providing an updated version here.

There are two major changes. First, Google started using OAuth2 for authentication so that code is different. There are plenty of details in the API docs.

Second, the Perl module has a bit smoother interface. I'm not sure if this was an API change or a module enhancement, I think the latter. In any case the new code is a lot cleaner.

# Updated Google Analytics example

use strict;
use warnings;

use Net::Google::Analytics;
use Net::Google::Analytics::OAuth2;

my $GoogleRefreshToken     = 'Your Refresh Token';
my $GoogleAnalyticsProfile = 'Your Analytics Profile';
my $GoogleClientID         = 'Your Client ID';
my $GoogleClientSecret     = 'Your Client Secret';

my $profile    = '14883391';
my $start_date = '2010-10-01';
my $end_date   = '2011-09-30';

my $analytics = Net::Google::Analytics->new();

# Authenticate

my $oauth = Net::Google::Analytics::OAuth2->new(
    client_id     => $GoogleClientID,
    client_secret => $GoogleClientSecret,
);
my $token = $oauth->refresh_access_token($GoogleRefreshToken);
$analytics->token($token);

# Setup Request
my $req = $analytics->new_request(
    ids        => "ga:$GoogleAnalyticsProfile",
    dimensions => 'ga:year,ga:month',
    sort       => '-ga:year,-ga:month',
    metrics    => 'ga:visits,ga:pageviews',
    start_date => $start_date,
    end_date   => $end_date,
);

# Run Request
my $results = $analytics->retrieve($req);
die("GA error: " . $results->error_message) if !$results->is_success;

# Get dimensions and metrics
my @dimensions = $results->dimensions;
my @metrics    = $results->metrics;

# Print tab separated header line
print join("\t", @dimensions);
print "\t";
print join("\t", @metrics);
print "\n";

# Print tab separated values
for my $row (@{$results->rows}) {
    for my $dimension (@dimensions) {
        my $accessor = "get_$dimension";
        my $value = $row->$accessor;
        print "$value\t";
    }
    for my $metric (@metrics) {
        my $accessor = "get_$metric";
        my $value = $row->$accessor;
        print "$value\t";
    }
    print "\n";
}

exit 1;

2 Comments

Hi, thanks for the script, but can you tell me where can i get Refresh Token for GoogleRefreshToken.

I was afraid someone might ask about that. It was months ago - I remember it was a pain but I don't remember the details! You'll have to Google around a bit. Try Stack Overflow.

Leave a comment

About this Entry

This page contains a single entry by Bill Ruppert published on November 5, 2012 4:51 PM.

Basic HTTP Authentication, LWP::UserAgent and the Realm was the previous entry in this blog.

Creating a .htaccess deny list from okean.com data is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.38