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
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”
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
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:
- 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.
- Authorize your Google Analytics account with client id and p12 file to obtain Analytics object
- 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
- 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;
}
}
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
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.