tôi đã cùng một vấn đề và tôi đã cho tôi khoảng 1h để tìm thấy điều này trong tài liệu v3:
Service Accounts
hữu ích cho tự động/ẩn/truy cập được lập lịch vào dữ liệu Google Analytics cho tài khoản của riêng bạn. Ví dụ: để tạo trang tổng quan trực tiếp về dữ liệu Google Analytics của riêng bạn và chia sẻ nó với những người dùng khác.
Có một vài bước bạn cần phải làm theo để cấu hình tài khoản dịch vụ để làm việc với Google Analytics:
- đăng ký một dự án trong Console API.
- Trong Bảng điều khiển API Google, trong ngăn Truy cập API, tạo ID ứng dụng với Loại ứng dụng được đặt thành Tài khoản dịch vụ.
- Đăng nhập vào Google Analytics và điều hướng đến phần Quản trị.
- Chọn tài khoản mà bạn muốn ứng dụng có quyền truy cập vào.
- Thêm địa chỉ email, từ ID khách hàng được tạo trong Bảng điều khiển API từ bướC# 2, với tư cách là người dùng của tài khoản Google Analytics đã chọn.
- Làm theo hướng dẫn cho Tài khoản dịch vụ để truy cập dữ liệu Google Analytics.
Đọc thêm ở đây: https://developers.google.com/analytics/devguides/reporting/core/v3/gdataAuthorization
Puh, tôi đoán API là quá lớn Google đang gặp rắc rối chứng minh điều này một cách dễ dàng =)
Sau đó, tôi nhìn vào mã kiểm tra vào plus-serviceaccount-cmdline-sample và analytics-cmdline-sample. Đây là một phiên bản rất cơ bản thực hiện trong một ứng dụng java Playframework2 rằng in để System.out như các ví dụ trên:
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
public static Result index() {
GoogleCredential credential = null;
try {
credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("[email protected]")
.setServiceAccountScopes(Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY))
.setServiceAccountPrivateKeyFromP12File(new File("/your/path/to/privatekey/privatekey.p12"))
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Set up and return Google Analytics API client.
Analytics analytics = new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(
"Google-Analytics-Hello-Analytics-API-Sample").build();
String profileId = "";
try {
profileId = getFirstProfileId(analytics);
} catch (IOException e) {
e.printStackTrace();
}
GaData gaData = null;
try {
gaData = executeDataQuery(analytics, profileId);
} catch (IOException e) {
e.printStackTrace();
}
printGaData(gaData);
return ok(index.render("Your new application is ready."));
}
private static String getFirstProfileId(Analytics analytics) throws IOException {
String profileId = null;
// Query accounts collection.
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 webproperties collection.
Webproperties webproperties =
analytics.management().webproperties().list(firstAccountId).execute();
if (webproperties.getItems().isEmpty()) {
System.err.println("No Webproperties found");
} else {
String firstWebpropertyId = webproperties.getItems().get(0).getId();
// Query profiles collection.
Profiles profiles =
analytics.management().profiles().list(firstAccountId, firstWebpropertyId).execute();
if (profiles.getItems().isEmpty()) {
System.err.println("No profiles found");
} else {
profileId = profiles.getItems().get(0).getId();
}
}
}
return profileId;
}
/**
* Returns the top 25 organic search keywords and traffic source by visits. The Core Reporting API
* is used to retrieve this data.
*
* @param analytics the analytics service object used to access the API.
* @param profileId the profile ID from which to retrieve data.
* @return the response from the API.
* @throws IOException tf an API error occured.
*/
private static GaData executeDataQuery(Analytics analytics, String profileId) throws IOException {
return analytics.data().ga().get("ga:" + profileId, // Table Id. ga: + profile id.
"2012-01-01", // Start date.
"2012-01-14", // End date.
"ga:visits") // Metrics.
.setDimensions("ga:source,ga:keyword")
.setSort("-ga:visits,ga:source")
.setFilters("ga:medium==organic")
.setMaxResults(25)
.execute();
}
/**
* Prints the output from the Core Reporting API. The profile name is printed along with each
* column name and all the data in the rows.
*
* @param results data returned from the Core Reporting API.
*/
private static void printGaData(GaData results) {
System.out.println("printing results for profile: " + results.getProfileInfo().getProfileName());
if (results.getRows() == null || results.getRows().isEmpty()) {
System.out.println("No results Found.");
} else {
// Print column headers.
for (GaData.ColumnHeaders header : results.getColumnHeaders()) {
System.out.printf("%30s", header.getName());
}
System.out.println();
// Print actual data.
for (List<String> row : results.getRows()) {
for (String column : row) {
System.out.printf("%30s", column);
}
System.out.println();
}
System.out.println();
}
}
Hope this helps!
Bài đăng của bạn thực sự giúp tôi. Tài liệu phiên bản 3.0 rất khó hiểu và vô dụng. Nó bỏ lỡ rất nhiều bước quan trọng như cách biên dịch client_secrets.json và trỏ đến 'what !!?' Như thế nào? liên kết. Một lần nữa, cảm ơn. –
trang đó bị xóa bởi google tôi đang đối mặt với vấn đề này trong 20 ngày qua, bạn có thể chia sẻ mã chính xác những gì bạn đang sử dụng để truy cập dữ liệu phân tích google –