Ver Fonte

GTM account

wfansh há 6 meses atrás
pai
commit
1f11a64511

+ 22 - 0
src/main/java/com/wechi/adweb/bridge/google/gtm/dto/GTMAccountDTO.java

@@ -0,0 +1,22 @@
+package com.wechi.adweb.bridge.google.gtm.dto;
+
+import lombok.Builder;
+import lombok.Data;
+
+/**
+ * @author wfansh
+ */
+@Data
+@Builder
+public class GTMAccountDTO {
+
+    private String id;
+
+    private String name;
+
+    private String displayName;
+
+    private boolean supportUserPermissions;
+
+    private boolean supportMultipleContainers;
+}

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

@@ -2,10 +2,15 @@ package com.wechi.adweb.bridge.google.gtm.service;
 
 import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
 import com.google.api.client.json.gson.GsonFactory;
+import com.google.api.client.util.Lists;
 import com.google.api.services.tagmanager.TagManager;
 import com.google.api.services.tagmanager.TagManagerScopes;
+import com.google.api.services.tagmanager.model.Account;
 import com.google.auth.http.HttpCredentialsAdapter;
 import com.google.auth.oauth2.GoogleCredentials;
+import com.google.gson.Gson;
+import com.wechi.adweb.bridge.exception.DataException;
+import com.wechi.adweb.bridge.google.gtm.dto.GTMAccountDTO;
 
 import lombok.extern.slf4j.Slf4j;
 
@@ -14,6 +19,7 @@ import org.springframework.stereotype.Service;
 
 import java.io.IOException;
 import java.security.GeneralSecurityException;
+import java.util.List;
 
 import javax.annotation.PostConstruct;
 
@@ -29,6 +35,8 @@ public class GTMService {
 
     private TagManager tagManager;
 
+    private final Gson gson = new Gson();
+
     @PostConstruct
     private void init() throws GeneralSecurityException, IOException {
         // Service account authorization;
@@ -47,5 +55,30 @@ public class GTMService {
                         .build();
     }
 
-    //    public String createContainer()
+    public List<GTMAccountDTO> listGTMAccounts() throws DataException {
+        try {
+            List<GTMAccountDTO> gtmAccounts = Lists.newArrayList();
+            for (Account account : tagManager.accounts().list().execute().getAccount()) {
+                GTMAccountDTO gtmAccount =
+                        GTMAccountDTO.builder()
+                                .id(account.getAccountId())
+                                .name(account.getPath())
+                                .displayName(account.getName())
+                                .supportUserPermissions(
+                                        account.getFeatures().getSupportUserPermissions())
+                                .supportMultipleContainers(
+                                        account.getFeatures().getSupportMultipleContainers())
+                                .build();
+
+                // Adds into the list;
+                gtmAccounts.add(gtmAccount);
+            }
+
+            log.info("listGTMAccounts : {}", gson.toJson(gtmAccounts));
+            return gtmAccounts;
+        } catch (IOException e) {
+            log.error(e.getMessage());
+            throw new DataException(e);
+        }
+    }
 }