如何从跟踪代码获取谷歌个人资料ID?

发布于 2024-10-25 01:45:49 字数 182 浏览 4 评论 0原文

我正在使用 Google API 获取 Google 分析。为了获得分析,我需要提供类似于“ga:12345678”的配置文件 ID。

问题是用户可以拥有多个配置文件。是否可以从 Google 跟踪代码中找出个人资料 ID(例如,如果我知道类似于“UA-1234567-1”的跟踪 ID)? 它们之间有联系吗?

谢谢

I'm getting Google analytics using Google API. In order to get analytics I need to provide profile Id which looks like "ga:12345678".

The problem is that user can have many profiles. Is it possible to figure out profile Id from say Google tracking code (e.g. if I know tracking ID which looks like "UA-1234567-1")?
Are they related to each other at all?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光无声 2024-11-01 01:45:49

我遇到了同样的问题,我找到了获取谷歌分析配置文件 ID 的最简单方法。

登录 Google Analytics(分析)

1.访问您网站的个人资料(进入仪表板)

2.您的 URL 应如下所示:

https://www.google.com/analytics/web/#report/visitors-overview/a1234b23478970p987654/

/a1234b23478970p987654/

最后一部分,“p”之后是您的 Google Analytics 配置文件 ID,在本例中(这是一个虚假帐户)为“987654”

I had the same issue and I find out the simplest way to get google analytics profile id.

Log into Google Analytics

1.Access your site’s profile (get to the dashboard)

2.Your URL should look like:

https://www.google.com/analytics/web/#report/visitors-overview/a1234b23478970p987654/

/a1234b23478970p987654/

That last part, after the “p” is your Google Analytics Profile ID, in this case (this is a fake account) it is “987654”

蓝梦月影 2024-11-01 01:45:49

您可以使用管理 API(下面的链接)以编程方式获取给定 WebPropertyId(UA 代码)存在的配置文件。

您进行的 HTTP 调用将如下所示:

https://www.google.com/analytics/feeds/datasources/ga/accounts/[accountID]/webproperties/[webPropertyID]/profiles

其中 accountIDwebPropertyID 将设置为您感兴趣的特定值或 ~all恢复当前用户有权访问的所有内容。

如果按照惯例,您没有在 Web 属性下创建多个配置文件,则只会为给定的 WebPropertyId 返回默认配置文件,这意味着您将获得从 WebPropertyId 到配置文件 ID 的一对一映射。这将允许您从 WebPropertyId 查找配置文件 ID。

有关详细信息,请参阅此处的管理 API 文档:http://code。 google.com/apis/analytics/docs/mgmt/mgmtFeedReference.html

You can programatically get the profiles that exist for a given WebPropertyId (UA code) using the management API (link below).

The HTTP call you make will look like this:

https://www.google.com/analytics/feeds/datasources/ga/accounts/[accountID]/webproperties/[webPropertyID]/profiles

Where accountID and webPropertyID will either be set to the specific values you are interested in or ~all to bring back everything the current user has access to.

If by convention you don't create multiple profiles under a Web Property then only the default profile will be returned for a given WebPropertyId, which means you will be getting a one-to-one mapping from WebPropertyId to profile id. This will allow you to look up a profile id from a WebPropertyId.

See here on the management API docs for more info: http://code.google.com/apis/analytics/docs/mgmt/mgmtFeedReference.html

一袭白衣梦中忆 2024-11-01 01:45:49

我刚刚完成了通过 Java 中的跟踪代码查找配置文件 ID 的任务。关键是跟踪代码用作网络资源 ID,并且配置文件通过内部网络资源 ID 与网络资源链接。步骤如下:

  1. 在 Google 开发者控制台中,设置服务帐户客户端 ID 以获取客户端电子邮件地址、客户端 ID 和 p12 文件。下载 p12 并放入您的服务器。
  2. 使用客户端 ID 和 p12 文件授权您的 Google Analytics 帐户以获取 Analytics 对象
  3. 使用 Analytics 对象,您可以获得所有 Web 属性对象,选择带有您的跟踪代码的属性作为 Web 属性 id 并获取其内部 Web 属性 id
  4. 使用 Analytics 对象,进行迭代通过所有配置文件对象,选择具有与步骤 2 中获取的内部 Web 属性 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;
    }

}

I had just done this task of finding the profile ID by Tracking Code in Java. The key is that tracking code is used as web property Id and the profile is linked with web property through an internal web property id. So the steps are as below:

  1. In Google developer console, set up a Service Accounts Client ID to get client email address, client Id and p12 file. Download the p12 and put to your server.
  2. Authorize your Google Analytics account with client id and p12 file to obtain Analytics object
  3. With Analytics object, you can obtain all web property objects, select the property with your tracking code as web property id and get its internal web property id
  4. With Analytics object, iterate through all profile objects, select the profile which has internal web property id the same as obtained from step 2

The full code is as follows, the getProfileId() method will return the profile id you want:

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的配置文件。

您可以从 GA 中的“分析设置 > 配置文件设置”屏幕获取 tableId(在配置文件之一上按“编辑”)。然后获取“个人资料 ID”字段并将其附加到“ga:”。您还可以使用帐户 Feed 下载帐户详细信息,包括个人资料数据:http://code.google.com/intl/en/apis/analytics/docs/gdata/gdataReferenceAccountFeed.html

What you're trying to obtain is called the tableId. The ID used in tracking code is called the webPropertyId. It's possible to create multiple profiles, with unique tableId's, for each web property.

You can get the tableId from the "Analytics Settings > Profile Settings" screen within GA (press 'edit' on one of the profiles). Then take the "Profile ID" field and append it to "ga:". You can also download the account details, including profile data, using the Account Feed: http://code.google.com/intl/en/apis/analytics/docs/gdata/gdataReferenceAccountFeed.html

吾性傲以野 2024-11-01 01:45:49

我使用 Perl 完成此操作。

这是获取请求
的网址

 my $ url = qq~https://www.googleapis.com/analytics/v3/management/accounts/ACCOUNTID/webproperties/WEBPROPERTYID/profiles?key=APIKEY~;

使用此 urlToken 来生成数据,您可以在其中找到 ga id

希望这会有所帮助。

I done this using Perl.

This is the url to get request

 my $ url = qq~https://www.googleapis.com/analytics/v3/management/accounts/ACCOUNTID/webproperties/WEBPROPERTYID/profiles?key=APIKEY~;

use this url with Token to generate Data where you will find the ga id

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文