Net::Google::Analytics Extended Example

There is an update to this post, stuff changed.

I wanted to fetch visitors and page views by month for the past year for our website, and quickly found the Net::Google::Analytics module. It and Net::Google::AuthSub installed easily. However, the snippet in the synopsis did not compile ($i was undefined) and was obviously missing a loop over the retrieved data. Anyway, here is a working snippet that retrieves and formats some data for easy loading into a spreadsheet. 

The hardest thing for me was getting the correct profile number.  I thought it was the number in the web page code that looked like UA-191234-1, but it's not.  You have to go to account settings and thenedit the profile of the web site to see the magic number - it's the "Profle ID:" at the top of the page.

# Fetch some Google Analytics data

use strict;
use warnings;

use Net::Google::Analytics;
use Net::Google::AuthSub;

my $user    = ''; # your account user id here
my $pass    = 'xxxxxx';        # your password here!

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

# Login

my $auth = Net::Google::AuthSub->new(service => 'analytics');
my $response = $auth->login($user, $pass);
if (!$response->is_success) {
    die 'Login failed: ' . $response->error . "\n";

# Datafeed request

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

my $data_feed = $analytics->data_feed;
my $req = $data_feed->new_request();


my $res = $data_feed->retrieve($req);

if (($res->{is_success} || 0) ne 1) {
    die "Lookup failed\n";

# Print tab separated header line

my $entry = $res->entries->[0];

for my $dimension (@{$entry->dimensions}) {
    my $name = $dimension->name;
    $name =~ s/^ga\://;
    print "$name\t";

for my $metric (@{$entry->metrics}) {
    my $name = $metric->name;
    $name =~ s/^ga\://;
    print "$name\t";

print "\n";

# Print tab separated values

for my $entry (@{$res->entries}) {
    for my $dimension (@{$entry->dimensions}) {
        my $value = $dimension->value;
        print "$value\t";
    for my $metric (@{$entry->metrics}) {
        my $value = $metric->value;
        print "$value\t";
    print "\n";

exit 1;


Thank you. I had the same problem with the profile number.

Can't locate object method "data_feed" via package "Net::Google::Analytics" at ./ line 30.

It seems, Net::Google::Analytics has changes since 2011 and no longer has data_feed property.

You are correct, the module was updated and I had to rewrite the program the post was based on. I'll update it as soon as I can!

The updated code has been published, see link at top of post.

Leave a comment

About this Entry

This page contains a single entry by Bill Ruppert published on October 14, 2011 10:22 PM.

Extracting Windows Zip Files - Archive::Zip was the previous entry in this blog.

Resources for Learning Perl is the next entry in this blog.

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


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