Python Operations GA API Guide

Source: Internet
Author: User

Because need to write a Blog Feature's sake, so contact the next GA Python API, found that the G home API is not so intuitive, compared around, but in the use of the process found in fact G home API design is very interesting, there may be some new design ideas, worth thinking about learning a 。 But this is not the focus of this article, this article is to introduce the use of the GA Python API V4 version, incidentally, in the end to answer a few of the problems I encountered in the process.

Getting Started with GA API

The current GA API is V4 version, it is said that the V3 version can also be used, but I did not try, in order to reduce the subsequent upgrade, directly on the V4 version. V4 version used to compare egg pain, but no harm, according to the official guide is still able to go on, just halfway through some problems, this later said.

  1. Enable API

    To get started with the Analytics Reporting API v4, you need to use the Setup tool, which guides you through creating projects in the Google Cloud Platform console, enabling APIs, and creating credentials.

    Here to spit a groove, G home seems to put its GA service and GCP bound together, do not know my understanding right, anyway I found that the project is generic. This is not very understanding, but it does not hinder my use, so follow the steps below to do just fine:

    1. Open the Service Account page. If you see a hint, select the item.
    2. Click Create service Account .
    3. In the Create Service Account window, type a name for the service account, and then select provide a new private key . If you want to grant G Suite full domain permissions to the service account, also check enable the G Suite Full domain delegation feature . Then, click Create .

    Here's a reminder to create a private key with JSON and P12 mode, it's recommended to use JSON, but I'm using p12 because I've already applied for it a long time ago. OK, to this, the creation of the account stage is finished, here need to record a few things.

    • Key_file_location: This is the local storage address of the private key that you just downloaded automatically
    • Service_account_email: Find in this page
    • VIEW_ID: View on this page

    ?

    ?

  2. Installing the GA Python package

    This is very simple, the direct use of PIP is good, it should be noted that the best even pyopenssl, because the P12 key file to use.

    ?

  3. Try an example

    You need to create a file named helloanalytics.py that will contain the specified sample code. All the code you can find in my Github Repo: GA Demo code. Here you have several constants that need to be changed:

    Because we just created a few IDs of the files, so we need to put it in the code, in the code should be modified here:

    ??

    Replace the 15, 16, and 17 lines with the above ID. So the code should be ready to run, and try to look at it. If an error occurs, take a look at my problem solving or take advantage of Google search.

Problems encountered
    1. Access error or no response

?

When I first saw the 3rd line of error, I guessed that it was the wall, so I copied the url:https://analyticsreporting.googleapis.com/$discovery/rest and found it in the browser to open it. So here I have no idea, because I think my ladder is a global, so this code should also be a ladder to go, but later found that this is the idea of face-beating.

After looking for some information and tried some DEMO code I found that the code should not go to the ladder, so set the HTTP proxy parameters, refer to the official website information: Httplib2 http Object, and then with the proxy parameters:

?

And then I found out it was all right! The akzent.

    1. User does not has any of the Google Analytics account

This question is relatively simple, it must be the first to think is not authorized, so went to search the GA user authorization, the original authorization and GA authorization is not a half-dime relationship, so still have to open on GA authorization:

?

These two places to edit a bit OK.

    1. How to construct your own API data

With GA's official guidance has been written down to find that can work, but found that the Demo only shows how the session is obtained, and no other support, so we still need to read the document ourselves, so reference to this document: GA all Metric, through this document, We can search for the data we need, as needed.

Reference
    1. HTTPLIB2:A Comprehensive HTTP Client library
    2. Analytics Google API Error 403: "User does not has any Google Analytics account

Python Operations GA API Guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.