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; } }
source share