wfansh пре 6 месеци
родитељ
комит
a38a238ea0

+ 30 - 0
src/main/java/com/wechi/adweb/bridge/google/gtm/dto/GTMTriggerDTO.java

@@ -0,0 +1,30 @@
+package com.wechi.adweb.bridge.google.gtm.dto;
+
+import com.wechi.adweb.bridge.google.common.ResourceDTO;
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.ToString;
+import lombok.experimental.SuperBuilder;
+
+/**
+ * @author wfansh
+ */
+@Data
+@SuperBuilder
+@ToString(callSuper = true)
+@EqualsAndHashCode(callSuper = true)
+public class GTMTriggerDTO extends ResourceDTO {
+
+    private String accountId;
+
+    private String containerId;
+
+    private String workspaceId;
+
+    private String type;
+
+    private String notes;
+
+    private String fingerprint;
+}

+ 46 - 2
src/main/java/com/wechi/adweb/bridge/google/gtm/service/GTMService.java

@@ -14,6 +14,7 @@ import com.google.common.annotations.VisibleForTesting;
 import com.wechi.adweb.bridge.exception.DataException;
 import com.wechi.adweb.bridge.google.gtm.dto.GTMAccountDTO;
 import com.wechi.adweb.bridge.google.gtm.dto.GTMContainerDTO;
+import com.wechi.adweb.bridge.google.gtm.dto.GTMTriggerDTO;
 import com.wechi.adweb.bridge.google.gtm.dto.GTMWorkspaceDTO;
 import com.wechi.adweb.bridge.util.JsonUtils;
 
@@ -42,7 +43,7 @@ public class GTMService {
     @Value("${google.gtm.container.admins}")
     private List<String> containerAdmins;
 
-    private TagManager tagManager;
+    public TagManager tagManager;
 
     @PostConstruct
     private void init() throws GeneralSecurityException, IOException {
@@ -172,14 +173,19 @@ public class GTMService {
      * https://developers.google.com/tag-platform/tag-manager/api/v2/reference/accounts/user_permissions
      */
     @VisibleForTesting
-    void setupContainerAccess(String accountResourceName, String containerId) throws DataException {
+    void initContainerAccess(String accountResourceName, String containerId) throws DataException {
         try {
             // 1. Container access.
+            // See
+            // https://developers.google.com/tag-platform/tag-manager/api/v2/reference/accounts/user_permissions#containerAccess
             ContainerAccess containerAccess = new ContainerAccess();
             containerAccess.setContainerId(containerId);
+            // publish > approve > edit > read
             containerAccess.setPermission("publish");
 
             // 2. Account access - required in user permission request.
+            // See
+            // https://developers.google.com/tag-platform/tag-manager/api/v2/reference/accounts/user_permissions#accountAccess
             AccountAccess accountAccess = new AccountAccess();
             accountAccess.setPermission("admin");
 
@@ -241,4 +247,42 @@ public class GTMService {
             throw new DataException(e);
         }
     }
+
+    @VisibleForTesting
+    public GTMTriggerDTO createGTMTrigger(String workspaceResourceName, String displayName)
+            throws DataException {
+        try {
+            Trigger trigger = new Trigger();
+            // See
+            // https://developers.google.com/tag-platform/tag-manager/api/v2/reference/accounts/containers/workspaces/triggers#type
+            trigger.setType("pageview");
+            trigger.setName("Page View");
+
+            Trigger createdTrigger =
+                    tagManager
+                            .accounts()
+                            .containers()
+                            .workspaces()
+                            .triggers()
+                            .create(workspaceResourceName, trigger)
+                            .execute();
+            log.info("Created trigger {}", JsonUtils.toJson(createdTrigger));
+
+            // Converts and returns.
+            return GTMTriggerDTO.builder()
+                    .id(createdTrigger.getTriggerId())
+                    .resourceName(createdTrigger.getPath())
+                    .displayName(createdTrigger.getName())
+                    .accountId(createdTrigger.getAccountId())
+                    .containerId(createdTrigger.getContainerId())
+                    .workspaceId(createdTrigger.getWorkspaceId())
+                    .type(createdTrigger.getType())
+                    .notes(createdTrigger.getNotes())
+                    .fingerprint(createdTrigger.getFingerprint())
+                    .build();
+        } catch (IOException e) {
+            log.error(e.getMessage());
+            throw new DataException(e);
+        }
+    }
 }