Data Connector for Google Analytics reporting

The Data Connector for Google Analytics enables import of the your reports.

Table of Contents

Prerequisites

  • Basic knowledge of Treasure Data

Step 0: Install ‘td’ command v0.11.9 or later

Install the newest Treasure Data Toolbelt.

$ td --version
0.11.10

Step 1: Prepare service account on Google

First, you need to create Service accunt on Google.

If you already have service account, choose account and generate a private key for it and skip this section.

Setup account, quoted from Using OAuth 2.0 for Server to Server Applications:

  1. Open the Service accounts page. If prompted, select a project.
  2. Click Create service account.
  3. In the Create service account window, type a name for the service account, and select Furnish a new private key. If you want to grant Google Apps domain-wide authority to the service account, also select Enable Google Apps Domain-wide Delegation. Then click Create.

    Screenshot:

  4. Enable “Analytics API” and “Analytics Reporting API V4” for that project (if necessary).

    Screenshot:

  5. Grant “Read & Analyze” permission to target account from Analytics Admin –> User Management page.

    Screenshots:

Then you will get key file. It will be used in a config file.

Step 2: Create Config File (config.yml)

Second, prepare config.yml as below, with your JSON keyfile. You must also specify bucket name and target file name (or prefix for multiple files).

in:
  type: google_analytics
  json_key_content: |
    {
      ... paste here you got key file content on Step 1 ...
    }
  view_id: 123111111
  time_series: "ga:dateHour"

  dimensions:
    - "ga:browser"
  metrics:
    - "ga:visits"
    - "ga:pageviews"
out:
  mode: append

view_id is a target data. You can find it on Google Analytics page (you need a permission to access Admin page on Google Analytics

time_series defines report data will be grouped by hourly or daily. ga:dateHour for hourly, ga:date for daily.

dimensions and metrics are the Analytics data properties. You will find available names on Dimensions & Metrics Explorer. Note that some combinations are invalid for example ga:1dayUsers and ga:7dayUsers doesn’t used in same config.

For more details on available out modes, see Appendix.

Untitled-3
You don't need to call `connector:guess`. Above config has complete information to do a job.

Step 3: Test your config with preview

Then, you can preview how the system will parse the file by using the preview command.

$ td connector:preview config.yml
+---------------------------+---------------------+-------------+----------------+
| date_hour:timestamp       | browser:string      | visits:long | pageviews:long |
+---------------------------+---------------------+-------------+----------------+
| "2016-06-01 07:00:00 UTC" | "Chrome"            | 17          | 134            |
| "2016-06-01 07:00:00 UTC" | "Firefox"           | 8           | 62             |
| "2016-06-01 07:00:00 UTC" | "Internet Explorer" | 2           | 11             |
| "2016-06-01 07:00:00 UTC" | "Safari"            | 6           | 23             |
| "2016-06-01 08:00:00 UTC" | "Chrome"            | 18          | 27             |
| "2016-06-01 08:00:00 UTC" | "Firefox"           | 13          | 26             |
| "2016-06-01 08:00:00 UTC" | "Internet Explorer" | 4           | 9              |
| "2016-06-01 08:00:00 UTC" | "Opera"             | 1           | 3              |
| "2016-06-01 08:00:00 UTC" | "Safari"            | 12          | 27             |
| "2016-06-01 08:00:00 UTC" | "Safari (in-app)"   | 1           | 1              |
+---------------------------+---------------------+-------------+----------------+
Untitled-3
If the system detects your column name or column type unexpectedly, modify `config.yml` directly and preview again.
Untitled-3
Currently, the Data Connector supports parsing of "boolean", "long", "double", "string", and "timestamp" types.

Step 4: Execute Load Job

Finally, submit the load job. It may take a couple of hours depending on the size of the data. Users need to specify the database and table where their data is stored.

It’s also recommended to specify --time-column option, since Treasure Data’s storage is partitioned by time (see also architecture). If the option is not given, the Data Connector will choose the first long or timestamp column as the partitioning time. The type of the column specified by --time-column must be either of long or timestamp type.

$ td connector:issue config.yml --database td_sample_db --table td_sample_table \
  --time-column date_hour

The above command assumes you have already created database(td_sample_db) and table(td_sample_table). If the database or the table do not exist in TD this command will not succeed, so create the database and table manually or use --auto-create-table option with td connector:issue command to auto create the database and table:

$ td connector:issue config.yml --database td_sample_db --table td_sample_table --time-column date_hour --auto-create-table
Untitled-3
At present, the Data Connector does not sort records server-side. To use time-based partitioning effectively, please sort records in files beforehand. This limitation will be resolved in the near future.

Scheduled execution

You can schedule periodic Data Connector execution for incremental Google Cloud Storage file imports. We take great care in distributing and operating our scheduler in order to achieve high availability. By using this feature, you no longer need a cron daemon on your local datacenter.

For the scheduled import, the Data Connector for Google Analytics imports all data at first.

On the second and subsequent runs, it will only imports data that comes after the last execution got.

Create the schedule

A new schedule can be created using the td connector:create command. The following are required: the name of the schedule, the cron-style schedule, the database and table where their data will be stored, and the Data Connector configuration file.

$ td connector:create \
    daily_import \
    "10 0 * * *" \
    td_sample_db \
    td_sample_table \
    config.yml

It’s also recommended to specify the --time-column option, since Treasure Data’s storage is partitioned by time (see also architecture)

$ td connector:create \
    daily_import \
    "10 0 * * *" \
    td_sample_db \
    td_sample_table \
    load.yml \
Untitled-3
The `cron` parameter also accepts three special options: `@hourly`, `@daily` and `@monthly`.
Untitled-3
By default, schedule is setup in UTC timezone. You can set the schedule in a timezone using -t or --timezone option. Please note that `--timezone` option only supports extended timezone formats like 'Asia/Tokyo', 'America/Los_Angeles' etc. Timezone abbreviations like PST, CST are *not* supported and may lead to unexpected schedules.

List the Schedules

You can see the list of currently scheduled entries by running the command td connector:list.

$ td connector:list
+--------------+------------+----------+-------+--------------+-----------------+----------------------------+
| Name         | Cron       | Timezone | Delay | Database     | Table           | Config                     |
+--------------+------------+----------+-------+--------------+-----------------+----------------------------+
| daily_import | 10 0 * * * | UTC      | 0     | td_sample_db | td_sample_table | {"in"=>{"type"=>"goo", ... |
+--------------+------------+----------+-------+--------------+-----------------+----------------------------+

Show the Settings and Schedule History

td connector:show shows the execution settings of a schedule entry.

% td connector:show daily_import
Name     : daily_import
Cron     : 10 0 * * *
Timezone : UTC
Delay    : 0
Database : td_sample_db
Table    : td_sample_table
Config
---
in:
  type: google_analytics
  json_key_content: |
    {
      ... paste here you got key file content on Step 1 ...
    }
  view_id: 123111111
  time_series: "ga:dateHour"

  dimensions:
    - "ga:browser"
  metrics:
    - "ga:visits"
    - "ga:pageviews"

td connector:history shows the execution history of a schedule entry. To investigate the results of each individual run, please use td job <jobid>.

% td connector:history daily_import
+--------+---------+---------+--------------+-----------------+----------+---------------------------+----------+
| JobID  | Status  | Records | Database     | Table           | Priority | Started                   | Duration |
+--------+---------+---------+--------------+-----------------+----------+---------------------------+----------+
| 578066 | success | 10000   | td_sample_db | td_sample_table | 0        | 2015-04-18 00:10:05 +0000 | 160      |
| 577968 | success | 10000   | td_sample_db | td_sample_table | 0        | 2015-04-17 00:10:07 +0000 | 161      |
| 577914 | success | 10000   | td_sample_db | td_sample_table | 0        | 2015-04-16 00:10:03 +0000 | 152      |
| 577872 | success | 10000   | td_sample_db | td_sample_table | 0        | 2015-04-15 00:10:04 +0000 | 163      |
| 577810 | success | 10000   | td_sample_db | td_sample_table | 0        | 2015-04-14 00:10:04 +0000 | 164      |
| 577766 | success | 10000   | td_sample_db | td_sample_table | 0        | 2015-04-13 00:10:04 +0000 | 155      |
| 577710 | success | 10000   | td_sample_db | td_sample_table | 0        | 2015-04-12 00:10:05 +0000 | 156      |
| 577610 | success | 10000   | td_sample_db | td_sample_table | 0        | 2015-04-11 00:10:04 +0000 | 157      |
+--------+---------+---------+--------------+-----------------+----------+---------------------------+----------+
8 rows in set

Delete the Schedule

td connector:delete will remove the schedule.

$ td connector:delete daily_import

Appendix

A) Modes for out plugin

You can specify file import mode in out section of seed.yml.

append (default)

This is the default mode and records are appended to the target table.

in:
  ...
out:
  mode: append

replace (In td 0.11.10 and later)

This mode replaces data in the target table. Please note that any manual schema changes made to the target table will remain intact with this mode.

in:
  ...
out:
  mode: replace

Last modified: Feb 24 2017 09:27:52 UTC

If this article is incorrect or outdated, or omits critical information, please let us know. For all other issues, please see our support channels.