How to get google profile id from tracking code?

I get Google analytics using the Google API. To get analytics, I need to provide a profile identifier that looks like "ga: 12345678".

The problem is that the user can have many profiles. Is it possible to define a profile identifier, for example, a Google tracking code (for example, if I know a tracking identifier that looks like "UA-1234567-1")? Are they related to each other?

thanks

+4
source share
5 answers

I had the same problem and found the easiest way to get the Google Analytics profile ID.

Log in to Google Analytics

1. Log in to your site’s profile (go to the toolbar).

2. Your URL should look like this:

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

/ a1234b23478970p 987654 /

The last part, after "p" is the identifier of your Google Analytics profile , in this case (this is a fake account) - "987654"

+4
source

You can programmatically retrieve the profiles that exist for this WebPropertyId (UA code) using the management API (link below).

The HTTP request you create 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 values ​​of interest to you, or ~all to return everything that the current user has access to.

If by agreement you do not create several profiles under the web property, only the default profile will be returned for this WebPropertyId, which means that you will receive a one-to-one mapping from WebPropertyId to the profile identifier. This will allow you to find the profile identifier from WebPropertyId.

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

+1
source

I just completed this task of finding a profile id using tracking code in Java. The key is that the tracking code is used as the identifier of the web resource, and the profile is connected to the web resource through the internal identifier of the web resource. So the steps are as follows:

  • In the Google Developer Console, configure the client ID of the service account to get the client email address, client ID, and p12 file. Download p12 and put on your server.
  • Authorize your Google Analytics account with customer ID and p12 file to receive an Analytics object
  • With the Analytics object, you can get all the web property objects, select the property with the tracking code as the identifier for the web property and get its internal web property identifier
  • In the Google Analytics object, iterating over all profile objects, select a profile that has an internal web property identifier, the same as that obtained from step 2

The full code is as follows: the getProfileId () method returns the desired profile identifier:

 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; } } 
+1
source

What you are trying to get is called tableId . The identifier used in the tracking code is called webPropertyId . You can create multiple profiles with unique tableId's for each web resource.

You can get the tableId on the “Google Analytics Settings> Profile Settings” screen in GA (click “edit” on one of the profiles). Then take the "Profile ID" field and add it to "ga:". You can also upload account information, including profile information, using the account feed: http://code.google.com/intl/en/apis/analytics/docs/gdata/gdataReferenceAccountFeed.html

0
source

I did this using Perl .

This is the get request url

  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 ga id

Hope this helps.

0
source

Source: https://habr.com/ru/post/1344618/


All Articles