Przeglądaj źródła

GA admin service

wfansh 6 miesięcy temu
rodzic
commit
6dec31a303

+ 2 - 1
build.gradle

@@ -30,7 +30,8 @@ dependencies {
 
     // Google
     implementation 'com.google.apis:google-api-services-tagmanager:v2-rev20240701-2.0.0'
-
+    implementation 'com.google.analytics:google-analytics-admin:0.59.0'
+    implementation 'com.google.analytics:google-analytics-data:0.60.0'
 
     testImplementation 'org.springframework.boot:spring-boot-starter-test'
     testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

+ 36 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/GAAdminService.java

@@ -0,0 +1,36 @@
+package com.wechi.adweb.bridge.google.analytics;
+
+import com.google.analytics.admin.v1beta.AnalyticsAdminServiceSettings;
+import com.google.api.gax.core.FixedCredentialsProvider;
+import com.google.auth.oauth2.GoogleCredentials;
+
+import jakarta.annotation.PostConstruct;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+
+/**
+ * @author wfansh
+ */
+@Slf4j
+@Service
+public class GAAdminService {
+    private AnalyticsAdminServiceSettings analyticsAdminServiceSettings;
+
+    @PostConstruct
+    private void init() throws IOException {
+        GoogleCredentials credentials =
+                GoogleCredentials.fromStream(
+                        this.getClass()
+                                .getClassLoader()
+                                .getResourceAsStream("google/service-account-key.json"));
+
+        this.analyticsAdminServiceSettings =
+                AnalyticsAdminServiceSettings.newBuilder()
+                        .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
+                        .build();
+    }
+}

+ 1 - 1
src/main/java/com/wechi/adweb/bridge/google/gtm/GTMService.java

@@ -25,7 +25,7 @@ public class GTMService {
     private TagManager tagManager;
 
     @PostConstruct
-    public void init() throws GeneralSecurityException, IOException {
+    private void init() throws GeneralSecurityException, IOException {
         // Service account authorization;
         HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
         GoogleCredential credential =