Bu eğitim, bir Google Analytics hesabına erişmek, Analytics API'lerini sorgulamak, API yanıtlarını yönetmek ve sonuçları almak için atılması gereken adımları adım adım açıklar. Bu eğiticide Core Reporting API v3.0, Management API v3.0 ve OAuth2.0 kullanılmıştır.
1. Adım: Analytics API'yi etkinleştirme
Google Analytics API'yi kullanmaya başlamak için önce kurulum aracını kullanmanız gerekir. Bu araç, Google API Konsolu'nda proje, API'yi etkinleştirme ve kimlik bilgileri oluşturma konusunda size yol gösterir.
İstemci kimliği oluşturma
Kimlik bilgileri sayfasından:
- Kimlik Bilgisi Oluştur'u tıklayın ve OAuth istemci kimliği'ni seçin.
- UYGULAMA TÜRÜ için Diğer'i seçin.
- Kimlik bilgilerine ad verin.
- Oluştur'u tıklayın.
Yeni oluşturduğunuz kimlik bilgisini seçin ve JSON'u indir'i tıklayın. İndirilen dosyayı client_secrets.json
olarak kaydedin. Bu eğitime daha sonra ihtiyacınız olacaktır.
2. Adım: Google İstemci Kitaplığı'nı yükleyin
Google Analytics API Java Client'ı yüklemek için çıkarmanız ve Java sınıf yolunuza kopyalamanız gereken tüm jar dosyalarını içeren bir ZIP dosyası indirmeniz gerekir.
- Gerekli tüm bağımlılıkları içeren bir ZIP dosyası olarak gönderilen Google Analytics Java istemci kitaplığını indirin.
- ZIP dosyasını ayıkla
libs
dizinindeki tüm JAR'ları sınıf yolunuza ekleyin.google-api-services-analytics-v3-[version].jar
jar'ı sınıf yolunuza ekleyin.
3. Adım: Örneği ayarlama
Belirtilen örnek kodunu içeren, HelloAnalytics.java
adlı tek bir dosya oluşturmanız gerekir.
- Aşağıdaki kaynak kodunu kopyalayın veya
HelloAnalytics.java
adresine indirin. - Daha önce indirilen
client_secrets.json
öğesini örnek kodla aynı dizine taşıyın.
import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.gson.GsonFactory; import com.google.api.client.util.store.FileDataStoreFactory; import com.google.api.services.analytics.Analytics; import com.google.api.services.analytics.AnalyticsScopes; import com.google.api.services.analytics.model.Accounts; import com.google.api.services.analytics.model.GaData; import com.google.api.services.analytics.model.Profiles; import com.google.api.services.analytics.model.Webproperties; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; /** * A simple example of how to access the Google Analytics API. */ public class HelloAnalytics { // Path to client_secrets.json file downloaded from the Developer's Console. // The path is relative to HelloAnalytics.java. private static final String CLIENT_SECRET_JSON_RESOURCE = "client_secrets.json"; // The directory where the user's credentials will be stored. private static final File DATA_STORE_DIR = new File( System.getProperty("user.home"), ".store/hello_analytics"); private static final String APPLICATION_NAME = "Hello Analytics"; private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); private static NetHttpTransport httpTransport; private static FileDataStoreFactory dataStoreFactory; public static void main(String[] args) { try { Analytics analytics = initializeAnalytics(); String profile = getFirstProfileId(analytics); printResults(getResults(analytics, profile)); } catch (Exception e) { e.printStackTrace(); } } private static Analytics initializeAnalytics() throws Exception { httpTransport = GoogleNetHttpTransport.newTrustedTransport(); dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); // Load client secrets. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(HelloAnalytics.class .getResourceAsStream(CLIENT_SECRET_JSON_RESOURCE))); // Set up authorization code flow for all auth scopes. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow .Builder(httpTransport, JSON_FACTORY, clientSecrets, AnalyticsScopes.all()).setDataStoreFactory(dataStoreFactory) .build(); // Authorize. Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); // Construct the Analytics service object. return new Analytics.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build(); } private static String getFirstProfileId(Analytics analytics) throws IOException { // Get the first view (profile) ID for the authorized user. String profileId = null; // Query for the list of all accounts associated with the service account. Accounts accounts = analytics.management().accounts().list().execute(); if (accounts.getItems().isEmpty()) { System.err.println("No accounts found"); } else { String firstAccountId = accounts.getItems().get(0).getId(); // Query for the list of properties associated with the first account. Webproperties properties = analytics.management().webproperties() .list(firstAccountId).execute(); if (properties.getItems().isEmpty()) { System.err.println("No properties found"); } else { String firstWebpropertyId = properties.getItems().get(0).getId(); // Query for the list views (profiles) associated with the property. Profiles profiles = analytics.management().profiles() .list(firstAccountId, firstWebpropertyId).execute(); if (profiles.getItems().isEmpty()) { System.err.println("No views (profiles) found"); } else { // Return the first (view) profile associated with the property. profileId = profiles.getItems().get(0).getId(); } } } return profileId; } private static GaData getResults(Analytics analytics, String profileId) throws IOException { // Query the Core Reporting API for the number of sessions // in the past seven days. return analytics.data().ga() .get("ga:" + profileId, "7daysAgo", "today", "ga:sessions") .execute(); } private static void printResults(GaData results) { // Parse the response from the Core Reporting API for // the profile name and number of sessions. if (results != null && !results.getRows().isEmpty()) { System.out.println("View (Profile) Name: " + results.getProfileInfo().getProfileName()); System.out.println("Total Sessions: " + results.getRows().get(0).get(0)); } else { System.out.println("No results found"); } } }
4. Adım: Örneği çalıştırın
Analytics API'yi etkinleştirdikten sonra, Java için Google API'leri istemci kitaplığını yükleyin ve örneğin çalıştırılmaya hazır olduğu örnek kaynak kodunu ayarlayın.
IDE kullanıyorsanız HelloAnalytics
sınıfı olarak ayarlanmış varsayılan bir çalıştırma hedefiniz olduğundan emin olun.
- Uygulama, yetkilendirme sayfasını bir tarayıcıda yükler.
- Google hesabınıza giriş yapmadıysanız giriş yapmanız istenir. Birden fazla Google hesabına giriş yaptıysanız yetkilendirme için kullanılacak bir hesap seçmeniz istenir.
Bu adımları tamamladığınızda örnek, yetkili kullanıcının ilk Google Analytics görünümünün adını (profili) ve son yedi gündeki oturum sayısını gösterir.
Yetkili Analytics hizmet nesnesiyle artık Management API'si başvuru dokümanlarında bulunan tüm kod örneklerini çalıştırabilirsiniz. Örneğin, kodu accountSummaries.list yöntemini kullanacak şekilde değiştirmeyi deneyebilirsiniz.