如何获取访问令牌以获取Google Analytics(分析)数据?

Kartikeya米斯拉

我正在使用Google Analytics(分析)来获取我的网站的数据。我已经在查询资源管理器上测试了查询。但是我无法使用OAuth在代码中实现它。我需要访问令牌才能运行查询,但是我的问题是我无法获取访问令牌。任何人都可以通过这个指导我。谁能解释Google开发人员控制台与分析帐户之间的关系。请参考一些实施文件。

戴姆敦

假设您要访问的是您自己的数据,并且可以访问Google Analytics(分析)帐户网站。我建议您使用服务帐户。Hello Analytics API Java

您可以在Google Developer Console上向Google注册应用程序,它与您的Google Analytics(分析)帐户没有任何关系。

再次建议您使用服务帐户,并在Google Developer控制台上创建服务帐户凭据。将服务帐户的电子邮件地址添加为帐户级别的Google Analytics(分析)部分中的用户,以使其具有读取权限,该访问权限必须在帐户级别。这将允许服务帐户读取您的Google Analytics(分析)数据。

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;

import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.AnalyticsScopes;
import com.google.api.services.analytics.model.Accounts;
import com.google.api.services.analytics.model.GaData;
import com.google.api.services.analytics.model.Profiles;
import com.google.api.services.analytics.model.Webproperties;

import java.io.File;
import java.io.IOException;


/**
 * A simple example of how to access the Google Analytics API using a service
 * account.
 */
public class HelloAnalytics {


  private static final String APPLICATION_NAME = "Hello Analytics";
  private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
  private static final String KEY_FILE_LOCATION = "/path/to/your.p12";
  private static final String SERVICE_ACCOUNT_EMAIL = "<SERVICE_ACCOUNT_EMAIL>@developer.gserviceaccount.com";
  public static void main(String[] args) {
    try {
      Analytics analytics = initializeAnalytics();

      String profile = getFirstProfileId(analytics);
      System.out.println("First Profile Id: "+ profile);
      printResults(getResults(analytics, profile));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static Analytics initializeAnalytics() throws Exception {
    // Initializes an authorized analytics service object.

    // Construct a GoogleCredential object with the service account email
    // and p12 file downloaded from the developer console.
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(httpTransport)
        .setJsonFactory(JSON_FACTORY)
        .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
        .setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_LOCATION))
        .setServiceAccountScopes(AnalyticsScopes.all())
        .build();

    // Construct the Analytics service object.
    return new Analytics.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName(APPLICATION_NAME).build();
  }


  private static String getFirstProfileId(Analytics analytics) throws IOException {
    // Get the first view (profile) ID for the authorized user.
    String profileId = null;

    // Query for the list of all accounts associated with the service account.
    Accounts accounts = analytics.management().accounts().list().execute();

    if (accounts.getItems().isEmpty()) {
      System.err.println("No accounts found");
    } else {
      String firstAccountId = accounts.getItems().get(0).getId();

      // Query for the list of properties associated with the first account.
      Webproperties properties = analytics.management().webproperties()
          .list(firstAccountId).execute();

      if (properties.getItems().isEmpty()) {
        System.err.println("No Webproperties found");
      } else {
        String firstWebpropertyId = properties.getItems().get(0).getId();

        // Query for the list views (profiles) associated with the property.
        Profiles profiles = analytics.management().profiles()
            .list(firstAccountId, firstWebpropertyId).execute();

        if (profiles.getItems().isEmpty()) {
          System.err.println("No views (profiles) found");
        } else {
          // Return the first (view) profile associated with the property.
          profileId = profiles.getItems().get(0).getId();
        }
      }
    }
    return profileId;
  }

  private static GaData getResults(Analytics analytics, String profileId) throws IOException {
    // Query the Core Reporting API for the number of sessions
    // in the past seven days.
    return analytics.data().ga()
        .get("ga:" + profileId, "7daysAgo", "today", "ga:sessions")
        .execute();
  }

  private static void printResults(GaData results) {
    // Parse the response from the Core Reporting API for
    // the profile name and number of sessions.
    if (results != null && !results.getRows().isEmpty()) {
      System.out.println("View (Profile) Name: "
        + results.getProfileInfo().getProfileName());
      System.out.println("Total Sessions: " + results.getRows().get(0).get(0));
    } else {
      System.out.println("No results found");
    }
  }
}

直接从Hello Analytics API中剥离代码:服务帐户的Java快速入门

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从Google Analytics(分析)中获取数据

来自分类Dev

获取每个用户的Google Analytics(分析)数据

来自分类Dev

如何从Google Analytics(分析)API获取Adwords广告系列数据?

来自分类Dev

如何从Google Analytics(分析)报告API获取维度数据

来自分类Dev

如何在图表中获取Google Analytics(分析)数据

来自分类Dev

从Python API访问Google Analytics(分析)数据

来自分类Dev

为什么我从Google Analytics(分析)API获取的数据与Google Analytics(分析)网络界面不匹配?

来自分类Dev

如何从 Facebook 访问令牌获取活动数据?

来自分类Dev

Rails,从Google Analytics(分析)中获取特定屏幕的数据

来自分类Dev

无法使用RGA R库从Google Analytics(分析)获取数据

来自分类Dev

无法从CloudConnect中的Google Analytics(分析)下载器获取数据

来自分类Dev

从Google Analytics(分析)获取所有数据

来自分类常见问题

将数据推送到Google Analytics(分析)

来自分类Dev

R脚本从Google Analytics(分析)导入数据

来自分类Dev

Google Analytics(分析)API不返回数据

来自分类Dev

将数据推送到Google Analytics(分析)

来自分类Dev

Google Analytics(分析)数据集类型(CRMint)

来自分类Dev

Google Analytics(分析)跟踪动态数据

来自分类Dev

Google Analytics(分析)数据处理时区

来自分类Dev

Google Analytics(分析)API不返回数据

来自分类Dev

Google Analytics(分析)未跟踪数据

来自分类Dev

R脚本从Google Analytics(分析)导入数据

来自分类Dev

Google Analytics(分析)行为流数据导出?

来自分类Dev

如何获取Google Analytics(分析)统计信息?

来自分类Dev

MonsterInsights的Google Analytics(分析)wordpress插件会自动插入跟踪代码还是只是获取分析数据?

来自分类Dev

如何在Google Analytics(分析)Reporting API中获取多个profile_id的数据?

来自分类Dev

如何在Google Analytics(分析)API中获取高级细分的会话数据?

来自分类Dev

如何使用Google Analytics(分析)数据获取Bigquery中可用的日期时间字段

来自分类Dev

如何使用analyticsreporting报表获取Google Analytics(分析)页面的特定数据?

Related 相关文章

  1. 1

    从Google Analytics(分析)中获取数据

  2. 2

    获取每个用户的Google Analytics(分析)数据

  3. 3

    如何从Google Analytics(分析)API获取Adwords广告系列数据?

  4. 4

    如何从Google Analytics(分析)报告API获取维度数据

  5. 5

    如何在图表中获取Google Analytics(分析)数据

  6. 6

    从Python API访问Google Analytics(分析)数据

  7. 7

    为什么我从Google Analytics(分析)API获取的数据与Google Analytics(分析)网络界面不匹配?

  8. 8

    如何从 Facebook 访问令牌获取活动数据?

  9. 9

    Rails,从Google Analytics(分析)中获取特定屏幕的数据

  10. 10

    无法使用RGA R库从Google Analytics(分析)获取数据

  11. 11

    无法从CloudConnect中的Google Analytics(分析)下载器获取数据

  12. 12

    从Google Analytics(分析)获取所有数据

  13. 13

    将数据推送到Google Analytics(分析)

  14. 14

    R脚本从Google Analytics(分析)导入数据

  15. 15

    Google Analytics(分析)API不返回数据

  16. 16

    将数据推送到Google Analytics(分析)

  17. 17

    Google Analytics(分析)数据集类型(CRMint)

  18. 18

    Google Analytics(分析)跟踪动态数据

  19. 19

    Google Analytics(分析)数据处理时区

  20. 20

    Google Analytics(分析)API不返回数据

  21. 21

    Google Analytics(分析)未跟踪数据

  22. 22

    R脚本从Google Analytics(分析)导入数据

  23. 23

    Google Analytics(分析)行为流数据导出?

  24. 24

    如何获取Google Analytics(分析)统计信息?

  25. 25

    MonsterInsights的Google Analytics(分析)wordpress插件会自动插入跟踪代码还是只是获取分析数据?

  26. 26

    如何在Google Analytics(分析)Reporting API中获取多个profile_id的数据?

  27. 27

    如何在Google Analytics(分析)API中获取高级细分的会话数据?

  28. 28

    如何使用Google Analytics(分析)数据获取Bigquery中可用的日期时间字段

  29. 29

    如何使用analyticsreporting报表获取Google Analytics(分析)页面的特定数据?

热门标签

归档