我正在使用 Google API 获取 Google 分析。为了获得分析,我需要提供类似于“ga:12345678”的配置文件 ID。
问题是用户可以拥有许多个人资料。是否可以从 Google 跟踪代码中找出配置文件 ID(例如,如果我知道跟踪 ID 看起来像“UA-1234567-1”)?它们是否相互关联?
谢谢
我正在使用 Google API 获取 Google 分析。为了获得分析,我需要提供类似于“ga:12345678”的配置文件 ID。
问题是用户可以拥有许多个人资料。是否可以从 Google 跟踪代码中找出配置文件 ID(例如,如果我知道跟踪 ID 看起来像“UA-1234567-1”)?它们是否相互关联?
谢谢
我遇到了同样的问题,我找到了获取谷歌分析配置文件 ID 的最简单方法。
登录谷歌分析
1.访问您网站的个人资料(进入仪表板)
2.您的网址应如下所示:
https://www.google.com/analytics/web/#report/visitors-overview/a1234b23478970p987654/
/a1234b23478970p 987654/
最后一部分,在“p”之后是您的 Google Analytics(分析)个人资料 ID,在这种情况下(这是一个假帐户)它是“987654”</p>
您可以使用管理 API(下面的链接)以编程方式获取给定 WebPropertyId(UA 代码)存在的配置文件。
您进行的 HTTP 调用将如下所示:
https://www.google.com/analytics/feeds/datasources/ga/accounts/[accountID]/webproperties/[webPropertyID]/profiles
WhereaccountID
和webPropertyID
将被设置为您感兴趣的特定值或~all
带回当前用户可以访问的所有内容。
如果按照惯例,您不在一个网络资产下创建多个配置文件,则只会为给定的 WebPropertyId 返回默认配置文件,这意味着您将获得从 WebPropertyId 到配置文件 ID 的一对一映射。这将允许您从 WebPropertyId 中查找配置文件 ID。
有关更多信息,请参见管理 API 文档:http ://code.google.com/apis/analytics/docs/mgmt/mgmtFeedReference.html
我刚刚完成了通过 Tracking Code in Java 查找配置文件 ID 的任务。关键是跟踪代码用作网络资源 ID,并且配置文件通过内部网络资源 ID 与网络资源链接。所以步骤如下:
完整代码如下,getProfileId()方法会返回你想要的profile id:
import java.io.File;
import java.util.Arrays;
import org.apache.commons.lang.StringUtils;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.AnalyticsScopes;
import com.google.api.services.analytics.model.Profile;
import com.google.api.services.analytics.model.Profiles;
import com.google.api.services.analytics.model.Webproperties;
import com.google.api.services.analytics.model.Webproperty;
public class AnalyticsUtils {
public static final String APP_NAME = "<YOUR APP NAME>";
public static final String CLIENT_ID = "<YOUR CLIENT ID>";
public static final String CLIENT_EMAIL = "<YOUR CLIENT EMAIL>";
public static final String PATH_TO_P12= "<PATH TO YOUR P12 FILE>";
public static final String TRACKING_ID="<YOUR TRACKING CODE>";
public static Analytics initializeAnalytics() throws Exception {
final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
final JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(CLIENT_EMAIL)
.setServiceAccountPrivateKeyFromP12File(new File(PATH_TO_P12))
.setServiceAccountScopes(
Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY))
.build();
Analytics analytics = new Analytics.Builder(HTTP_TRANSPORT,
JSON_FACTORY, credential).setApplicationName(APP_NAME).build();
return analytics;
}
public static String getProfileId(Analytics analytics) throws Exception {
Webproperties webproperties = analytics.management().webproperties().list("~all").execute();
String internalPropertyId = StringUtils.EMPTY;
for (Webproperty webproperty: webproperties.getItems()) {
if (TRACKING_ID.equalsIgnoreCase(webproperty.getId())) {
internalPropertyId = webproperty.getInternalWebPropertyId();
break;
}
}
Profiles profiles = analytics.management().profiles()
.list("~all", "~all").execute();
for (Profile profile: profiles.getItems()) {
if (internalPropertyId.equalsIgnoreCase(profile.getInternalWebPropertyId())) {
return profile.getId();
}
}
return StringUtils.EMPTY;
}
}
您要获取的内容称为tableId
. 跟踪代码中使用的 ID 称为webPropertyId
. tableId's
可以为每个网络资产创建多个具有唯一性的配置文件。
您可以 tableId
从 GA 中的“分析设置 > 配置文件设置”屏幕获取(在其中一个配置文件上按“编辑”)。然后获取“个人资料 ID”字段并将其附加到“ga:”。您还可以使用帐户供稿下载帐户详细信息,包括个人资料数据:http ://code.google.com/intl/en/apis/analytics/docs/gdata/gdataReferenceAccountFeed.html
我使用Perl
.
这是网址get request
my $ url = qq~https://www.googleapis.com/analytics/v3/management/accounts/ACCOUNTID/webproperties/WEBPROPERTYID/profiles?key=APIKEY~;
使用它url
来Token
生成数据,您将在其中找到ga id
希望这可以帮助。