我正在使用Google API获取Google分析。为了获得分析,我需要提供类似“ga:12345678”的配置文件ID。
问题是用户可以拥有多个配置文件。是否可以从Google跟踪代码中找出配置文件ID(例如,如果我知道跟踪ID看起来像“UA-1234567-1”)? 它们是否相互关联?
由于
答案 0 :(得分:3)
我遇到了同样的问题,我发现了获取谷歌分析个人资料ID的最简单方法。
登录Google Analytics
1.访问您网站的个人资料(转到信息中心)
2.您的网址应如下所示:
https://www.google.com/analytics/web/#report/visitors-overview/a1234b23478970p987654/
/ a1234b23478970p的 987654 / 强>
最后一部分,在“p”之后是您的Google Analytics配置文件ID ,在这种情况下(这是一个虚假帐户),它是“987654”
答案 1 :(得分:1)
您可以使用管理API(下面的链接)以编程方式获取给定WebPropertyId(UA代码)的配置文件。
您拨打的HTTP电话将如下所示:
https://www.google.com/analytics/feeds/datasources/ga/accounts/[accountID]/webproperties/[webPropertyID]/profiles
将accountID
和webPropertyID
设置为您感兴趣的特定值,或~all
以恢复当前用户有权访问的所有内容。
如果按照惯例,您不在Web属性下创建多个配置文件,则只返回给定WebPropertyId的默认配置文件,这意味着您将获得从WebPropertyId到配置文件ID的一对一映射。这将允许您从WebPropertyId中查找配置文件ID。
有关详细信息,请参阅管理API文档:http://code.google.com/apis/analytics/docs/mgmt/mgmtFeedReference.html
答案 2 :(得分:1)
我刚刚完成了通过Java中的跟踪代码查找配置文件ID的任务。关键是跟踪代码用作网络媒体资源ID,配置文件通过内部网络媒体资源ID与网络媒体资源相关联。所以步骤如下:
完整代码如下,getProfileId()方法将返回您想要的配置文件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;
}
}
答案 3 :(得分:0)
您要获取的内容称为tableId
。跟踪代码中使用的ID称为webPropertyId
。可以为每个网络媒体资源创建具有唯一tableId's
的多个配置文件。
您可以在GA中的“Analytics设置&gt;配置文件设置”屏幕中获取tableId
(在其中一个配置文件中按“编辑”)。然后选择“个人资料ID”字段并将其附加到“ga:”。您还可以使用帐户Feed http://code.google.com/intl/en/apis/analytics/docs/gdata/gdataReferenceAccountFeed.html
答案 4 :(得分:0)
我使用 q = (session.query(User, Product)
.outerjoin(User.products)
)
for u, p in q.all():
print(u, p)
完成此操作。
这是 Perl
get request
将此 my $ url = qq~https://www.googleapis.com/analytics/v3/management/accounts/ACCOUNTID/webproperties/WEBPROPERTYID/profiles?key=APIKEY~;
与url
一起使用,以生成数据,您可在其中找到Token
希望这有帮助。