The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojar::Google::Analytics - Fetch Google Analytics reporting data

SYNOPSIS

  use Mojar::Google::Analytics;
  $analytics = Mojar::Google::Analytics->new(
    auth_user => q{1234@developer.gserviceaccount.com},
    private_key => $pk,
    profile_id => q{5678}
  );
  $analytics->req(
    dimensions => [qw(pagePath)],
    metrics => [qw(visitors pageviews)],
    sort => 'pagePath',
    start_index => $start,
    max_results => $max_resultset
  );
  if (my $res = $analytics->fetch) {
    # Do something with $res->rows or $res->columns
  }

DESCRIPTION

Google Analytics provide an API for retrieving reporting data and there are recommended client libraries for several languages but not Perl. This class provides an interface to v3 of the Core Reporting API.

ATTRIBUTES

api_url

Currently the only supported value is https://www.googleapis.com/analytics/v3/data/ga.

ua

An instance of the user agent to use. Defaults to a Mojo::UserAgent.

timeout
  $analytics = Mojar::Google::Analytics->new(
    auth_user => q{1234@developer.gserviceaccount.com},
    private_key => $pk,
    profile_id => q{5678},
    timeout => 120
  );

The inactivity timeout for the user agent. Any change from the default (60 sec) must be applied before the first use of the user agent, and so is best done when creating your analytics object.

profile_id

The profile within your GA account you want to use.

req

The current request object to use. First set profile_id then set req with your parameters.

  $ga->profile_id(...)->req(...);
res

The current result object.

auth_user

The user GA generated for you when you registered your application. Should end in @developer.gserviceaccount.com.

grant_type

Currently the only supported value is urn:ietf:params:oauth:grant-type:jwt-bearer.

private_key

Your account's private key.

jwt

The JWT object. Defaults to

  Mojar::Auth::Jwt->new(
    iss => $self->auth_user,
    private_key => $self->private_key
  )
validity_margin

How close (in seconds) to the expiry time should the current token be replaced. Defaults to 10 seconds.

token

The current access token.

METHODS

new

Sets the credentials for access.

  $analytics = Mojar::Google::Analytics->new(
    auth_user => q{1234@developer.gserviceaccount.com},
    private_key => $pk,
    profile_id => q{5678}
  );
fetch

Fetches first/next batch of results based on set credentials and the req object. Automatically checks/renews the access token.

  $result = $analytics->fetch  # replaces $analytics->res
has_valid_token

Check if the current token is still valid (and not too close to expiry). (See validity_margin.)

  unless ($analytics->has_valid_token) { ... }
renew_token

Force obtaining a fresh token.

  $token = $analytics->renew_token  # replaces $analytics->token

CONFIGURATION AND ENVIRONMENT

You need to create a low-privilege user within your GA account, granting them access to an appropriate profile. Then register your application for unattended access. That results in a username and private key that your application uses for access.

SUPPORT

See Mojar.

SEE ALSO

Net::Google::Analytics is similar, main differences being dependencies and means of getting tokens.