Read Google Analytics Social Interactions

Updated: Sep 3, 2023

This example reads and extracts social interaction data from Google Analytics. It simplifies the process of retrieving and analyzing user interactions with social media content, allowing businesses to gain insights into the effectiveness of their social media campaigns, track social engagement, and optimize their online presence.

You can also reference these examples to get other analytical data from Google Analytics: Read Google Analytics ViewsRead Google Analytics goal conversions.

 

Java Code Listing

package com.northconcepts.datapipeline.examples.google.analytics;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
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.jackson2.JacksonFactory;
import com.google.api.services.analyticsreporting.v4.AnalyticsReportingScopes;
import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.google.analytics.GoogleAnalyticsReportReader;
import com.northconcepts.datapipeline.job.Job;


/**
 *
 * CLIENT_SECRET is your Service account credentials created in the Google Developer Console
 * and stored in the resources folder.
 */
public class ReadGoogleAnalyticsSocialInteractions {

    private static final String CLIENT_SECRET = "/My Project-5ed7b6c44f35.json";
    private static final String APPLICATION_NAME = "Hello Analytics Reporting";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String VIEW_ID = "80548296";
    /** Authorizes the installed application to access user's protected data. */
    private static GoogleCredential authorize(NetHttpTransport httpTransport) throws Throwable {
        return GoogleCredential.fromStream(
                ReadGoogleAnalyticsSocialInteractions.class.getResourceAsStream(CLIENT_SECRET), httpTransport, JSON_FACTORY)
                .createScoped(AnalyticsReportingScopes.all());
    }

    public static void main(String[] args) throws Throwable {

        NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

        DataReader reader = new GoogleAnalyticsReportReader(authorize(httpTransport), VIEW_ID)
                .addDateRange("14DaysAgo", "today")
                .addDimension("ga:socialInteractionNetwork")
                .addDimension("ga:socialInteractionAction")
                .addDimension("ga:socialInteractionNetworkAction")
                .addDimension("ga:socialInteractionTarget")
                .addDimension("ga:socialEngagementType")
                .addMetric("ga:socialInteractions")
                .addMetric("ga:uniqueSocialInteractions")
                .addMetric("ga:socialInteractionsPerSession")
                .setApplicationName(APPLICATION_NAME);
        DataWriter writer = StreamWriter.newSystemOutWriter();

        Job.run(reader, writer);

    }
}

 

Code Walkthrough

  1. authorize() method authorizes the installed application to access the user's protected data. The resource class (ReadGoogleAnalyticsSocialInteractions) and OAuth 2.0 scopes are also defined in this method body along with credentials.
  2. NetHttpTransport instance is created to thread-safe HTTP low-level transport.
  3. GoogleAnalyticsReportReader uses an API to get analytics of social interactions from Google. The constructor of the instance accepts GoogleCredentials and VIEW_ID. Additional parameters such as date range, dimensions, metrics, and application name are specified using corresponding methods.
  4. StreamWriter.newSystemOutWriter() instance is created to print out the results.
  5. Job.run() method is used to transfer the data from reader to writer to print out the records on the console. See how to compile and run data pipeline jobs. 
Mobile Analytics