Google Analytics

Management API - Daily Upload Developer Guide

This document explains how to use the Management API v3 to access and modify Daily Upload data.

  1. Introduction
  2. Before You Begin
  1. Supported Methods
    1. List
    2. Delete
    3. Upload
  1. Sample Application
More

Introduction

A Google Analytics Daily Upload is at the fourth and lowest level in the account hierarchy; daily uploads have no child entities. Each Daily Upload resource has a single Custom Data Source resource as its parent in the hierarchy.

The Daily Upload resource provided by the Management API represents data uploaded to Google Analytics for a particular date. The resource also includes metadata that identifies how a daily upload is configured: the custom data source it is associated with, date, appendCount, and a history of recent changes. This resource is used for cost data uploads.

Before You Begin

This guide demonstrates how to access the Google Analytics API using the following programming languages:
  • Java
  • Python

  • Read the client libraries page for a complete list of programming language specific client libraries that work with the Management API.
  • Read the Reference Guide to access the API without a client library

Each client library provides a single Analytics service object to access all Management API data. To create the service object you generally have to go through the following steps:

  1. Register your application in the Google Developers Console.
  2. Authorize access to Google Analytics data.
  3. Create an Analytics service object.

If you haven't completed these steps, please stop and read the Hello Google Analytics API Tutorial. This tutorial will walk you through the initial steps of building a Google Analytics API application. Once complete, you will be able to use this guide to perform real-world tasks.

The following code snippet continues from step 3: Create an Analytics service object and contains a variable to store an authorized service object.

Java

Analytics analytics = // Read Hello Analytics Tutorial for details.

Python

analytics = # Read Hello Analytics Tutorial for details.

Once you create an Analytics service object, you are ready to make requests to the Management API.

Note: The same Analytics service object can also be used to access the Core Reporting API.

Overview

The Daily Upload collection and resource allows you to list and delete daily uploads for an authorized user. You can also upload data to a daily upload, which will be associated with a custom data source. To use any of these methods, you first need to create an Analytics service object to work with the API.

Supported Methods

  • List a user's daily uploads.
  • Delete cost data from a daily upload resource.
  • Upload cost data to a daily upload resource.

List

An application can request a list of a user's daily uploads for a given date range by calling the list method for Daily Uploads on the Analytics service object. The date range can have a maximum interval of 12 months. The method requires an accountId, a webPropertyId, a customDataSourceId, a start-date, and an end-date as parameters.

For example, the following code requests a list of all Daily Uploads for the date range 2012-10-01 to 2012-10-31, account ID 1234, web property ID UA-1234-1, and custom data source ID 123456789.

Java

try {
  DailyUploads dailyUploads = analytics.management().dailyUploads().list('1234',
        'UA-1234-1', '123456789', '2012-10-01', '2012-10-31').execute();

} catch (GoogleJsonResponseException e) {
  System.err.println("There was a service error: "
      + e.getDetails().getCode() + " : "
      + e.getDetails().getMessage());
}

Python

try:
  daily_uploads = analytics.management().dailyUploads().list(
      accountId='1234',
      webPropertyId='UA-1234-1',
      customDataSourceId='123456789',
      start_date='2012-10-01',
      end_date='2012-10-31).execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print ('There was an error in constructing your query : %s' % error)

except HttpError, error:
  # Handle API errors.
  print ('Arg, there was an API error : %s : %s' %
         (error.resp.status, error._get_reason()))
  • If the query was successful, the API will return a 200 status code along with the requested data.
  • If there was an error, the API will return with status code other than 200, as well as a descriptive message describing what caused the error. Read the Error Response section in the reference guide for a full list of all the status codes and what they mean.

The results of the list method for daily uploads are stored in the dailyUploads object. The following code shows how to iterate through them. Read the DailyUploads reference for an explanation of all the properties of a DailyUpload.

Java

for (DailyUploads dailyUpload : dailyUploads.getItems()) {
  System.out.println("Account ID: " + dailyUpload.getAccountId());
  System.out.println("Web Property ID: " + dailyUpload.getWebPropertyId());
  System.out.println("Custom Data Source ID: " + dailyUpload.getCustomDataSourceId());
  System.out.println("Date: " + dailyUpload.getDate());
  System.out.println("Append Count:" + dailyUpload.getAppendCount());
  System.out.println("Created Time:" + dailyUpload.getCreatedTime());
  System.out.println("Modified Time:" + dailyUpload.getModifiedTime());

  System.out.println("Recent Changes:");
  for (RecentChanges recentChanges : dailyUpload.getRecentChanges()) {
    System.out.println(" Change Type: " + recentChanges.getChange());
    System.out.println(" Change Time: " + recentChanges.getTime());
  }

}

Python

for daily_upload in daily_uploads.get('items', []):
  print 'Account ID               = %s' % daily_upload.get('accountId')
  print 'Web Property ID          = %s' % daily_upload.get('webPropertyId')
  print 'Custom Data Source ID    = %s' % daily_upload.get('customDataSourceId')
  print 'Date                     = %s' % daily_upload.get('date')
  print 'Append Count             = %s' % daily_upload.get('appendCount')
  print 'Created                  = %s' % daily_upload.get('createdTime')
  print 'Modified                 = %s' % daily_upload.get('modifiedTime')

  print ' Recent Changes:  '
  for recent_change in daily_upload.get('recentChanges'):
    print ' Change Type     = %s' % recent_change.get('change')
    print ' Change Time     = %s' % recent_change.get('time')

Delete

An application can delete a user's daily upload for a given date by calling the delete method for Daily Uploads on the Analytics service object. The method requires an accountId, a webPropertyId, a customDataSourceId, and a date as parameters. You'll need to make an HTTP DELETE request to delete a user's daily upload data.

To delete, the user must have authorized the auth scope https://www.googleapis.com/auth/analytics.

For example, the following code deletes a daily upload for 2012-10-31, for account ID 1234, web property ID UA-1234-1, and custom data source ID 123456789.

Java

try {
  Delete delete = analytics.management().dailyUploads().delete("1234",
      "UA-1234-1", "123456789", "2012-10-31", "cost").execute();

} catch (GoogleJsonResponseException e) {
  System.err.println("There was a service error: "
      + e.getDetails().getCode() + " : "
      + e.getDetails().getMessage());
}

Python

try:
  delete = analytics.management().dailyUploads().delete(
      accountId='1234',
      webPropertyId='UA-1234-1',
      customDataSourceId='123456789',
      type='cost',
      date='2012-10-31').execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print ('There was an error in constructing your query : %s' % error)

except HttpError, error:
  # Handle API errors.
  print ('Arg, there was an API error : %s : %s' %
         (error.resp.status, error._get_reason()))
  • If the query was successful, the API will return a 200 status code. The delete method returns an empty response body.
  • If there was an error, the API will return with status code other than 200, as well as a descriptive message describing what caused the error. Read the Error Response section in the reference guide for a full list of all the status codes and what they mean.

Read the DailyUploads reference for an explanation of all the properties of a DailyUpload.

Upload

Each upload is associated with a specific Custom Data Source and date. An upload can either append to an existing set of data or reset and overwrite all previous appends with new data.

An application can upload to a user's custom data source for a given date by calling the upload method for Daily Uploads on the Analytics service object. The method requires an accountId, a webPropertyId, a customDataSourceId, a type, a date, an appendNumber as parameters. An optional reset can also be specified to overwrite existing data. This method expects you to upload a CSV file, which means you'll need to make an HTTP POST request, with the Mime-Type set to application/octet-stream.

To upload, the user must have authorized the auth scope https://www.googleapis.com/auth/analytics.

For example, the following code uploads a CSV file data.csv, for 2012-10-31, for account ID 1234, web property ID UA-1234-1, custom data source ID 123456789, a type set to cost, and any previous data will be overwritten by setting reset to true and the appendNumber to 1.

Java

try {
  File file = new File("data.csv");
  InputStreamContent mediaContent = new InputStreamContent('application/octet-stream', new FileInputStream(file));
  mediaContent.setLength(file.length());
  Upload upload = analytics.management().dailyUploads().upload("1234",
      "UA-1234-1", "123456789", "2012-10-31", 1, "cost", mediaContent);

  upload.setReset(true);
  DailyUploadAppend append = upload.execute();

} catch (GoogleJsonResponseException e) {
  System.err.println("There was a service error: "
      + e.getDetails().getCode() + " : "
      + e.getDetails().getMessage());
}

Python

try:
  media = MediaFileUpload('data.csv', mimetype='application/octet-stream', resumable=False)

  daily_upload = analytics.management().dailyUploads().upload(
      accountId='1234',
      webPropertyId='UA-1234-1',
      customDataSourceId='123456789',
      date='2012-10-31',
      appendNumber=1,
      reset=true,
      type='cost',
      media_body=media).execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print ('There was an error in constructing your query : %s' % error)

except HttpError, error:
  # Handle API errors.
  print ('Arg, there was an API error : %s : %s' %
         (error.resp.status, error._get_reason()))
  • If the query was successful, the API will return a 200 status code along with a dailyUpload resource in the response body.
  • If there was an error, the API will return with status code other than 200, as well as a descriptive message describing what caused the error. Read the Error Response section in the reference guide for a full list of all the status codes and what they mean.

The result of the upload method for daily uploads are stored in the dailyUploads object. The following code shows how to iterate through them.

Java

  System.out.println("Account ID:" + append.getAccountId());
  System.out.println("Web Property ID:" + append.getWebPropertyId());
  System.out.println("Custom Data Source ID:" + append.getCustomDataSourceId());
  System.out.println("Date:" + append.getDate());
  System.out.println("Append Number:" + append.getAppendNumber());
  System.out.println("Next Append Link:" + append.getNextAppendLink());

Python

  print 'Account ID               = %s' % daily_upload.get('accountId')
  print 'Web Property ID          = %s' % daily_upload.get('webPropertyId')
  print 'Custom Data Source ID    = %s' % daily_upload.get('customDataSourceId')
  print 'Date                     = %s' % daily_upload.get('date')
  print 'Append Number            = %s' % daily_upload.get('appendNumber')
  print 'Next Append Link         = %s' % daily_upload.get('nextAppendLink')

Read the DailyUploads reference for an explanation of all the properties of a DailyUpload.




Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.