Search code examples
javagoogle-tag-managergoogle-analytics-apigoogle-analytics-4

How to read tag and their related triggers name/ value in google analytics 4 using java


I am using GA4 tutorial to make an API call to Google Analytics 4and the code provided in that link is listing my data based on defined dimension and metrics.Suppose in Google Tag Manager, I have created a tag and relative triggers like in this photo: Tag Trigger For example if the user clicks on the menu bar and the Click URL contains specific URL, then the tag is fired. Of course it is possible to see this information from Google Tag Assistant. My variables in Google Tag Assistant, while the tag is triggered Now I want to know if it is possible to list these tags and triggers using JAVA as well or not? Thank you in advance


Solution

  • Finally I found out how to do it. As an example for the tag and trigger I created:

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
    import com.google.analytics.data.v1beta.BetaAnalyticsDataSettings;
    import com.google.analytics.data.v1beta.DateRange;
    import com.google.analytics.data.v1beta.Dimension;
    import com.google.analytics.data.v1beta.RunReportRequest;
    import com.google.analytics.data.v1beta.RunReportResponse;
    import com.google.api.gax.core.FixedCredentialsProvider;
    import com.google.auth.oauth2.GoogleCredentials;
    
    import io.grpc.LoadBalancerRegistry;
    import io.grpc.grpclb.GrpclbLoadBalancerProvider;
    
    public class Ga4DataAnalytics2 {
    
        static {
            LoadBalancerRegistry.getDefaultRegistry().register( new GrpclbLoadBalancerProvider());
        }
        public static void main(String[] args) throws IOException {
    
        String credentialsJsonPath="path to credential.json";
        String propertyId = "your property ID";
        Files.createDirectories(Paths.get(credentialsJsonPath).getParent());
    
        GoogleCredentials credentials =
                GoogleCredentials.fromStream(new FileInputStream(credentialsJsonPath));
    
        BetaAnalyticsDataSettings betaAnalyticsDataSettings =
                BetaAnalyticsDataSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
                .build();    
    
        try (BetaAnalyticsDataClient analyticsData =
                BetaAnalyticsDataClient.create(betaAnalyticsDataSettings)) {
            RunReportRequest request =
                    RunReportRequest.newBuilder()
                    .setProperty("properties/" + propertyId)
                    .addDimensions(Dimension.newBuilder().setName("linkUrl"))
                    .addDimensions(Dimension.newBuilder().setName("linkText"))          
                    .addDateRanges(DateRange.newBuilder().setStartDate("2022-09-25").setEndDate("2022-09-26"))
                    .build();
            RunReportResponse response = analyticsData.runReport(request);
            System.out.println("Report result:\n"+response);
         
        }
    
    }
    
    }
    

    and why I am using linkUrl and linkText as dimensions, that is because my tag configuration was : My Tag Configuration