Browse Source

Delete GTM

wfansh 5 months ago
parent
commit
70aea28949

+ 5 - 1
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/dto/google/analytics/GAAccountDTO.java

@@ -1,5 +1,7 @@
 package org.jeecg.modules.adweb.dmp.dto.google.analytics;
 
+import com.google.common.base.Joiner;
+
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NoArgsConstructor;
@@ -24,6 +26,8 @@ public class GAAccountDTO extends ResourceDTO {
     private long updateTime;
 
     public static String toResourceName(String id) {
-        return StringUtils.isNumeric(id) ? "accounts" + RESOURCE_NAME_SPLITTER + id : id;
+        return StringUtils.isNumeric(id)
+                ? Joiner.on(RESOURCE_NAME_SPLITTER).join("accounts", id)
+                : id;
     }
 }

+ 5 - 1
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/dto/google/analytics/GAPropertyDTO.java

@@ -1,5 +1,7 @@
 package org.jeecg.modules.adweb.dmp.dto.google.analytics;
 
+import com.google.common.base.Joiner;
+
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NoArgsConstructor;
@@ -42,6 +44,8 @@ public class GAPropertyDTO extends ResourceDTO {
     private List<GADataStreamDTO> dataStreams;
 
     public static String toResourceName(String id) {
-        return StringUtils.isNumeric(id) ? "properties" + RESOURCE_NAME_SPLITTER + id : id;
+        return StringUtils.isNumeric(id)
+                ? Joiner.on(RESOURCE_NAME_SPLITTER).join("properties", id)
+                : id;
     }
 }

+ 5 - 1
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/dto/google/gtm/GTMAccountDTO.java

@@ -1,5 +1,7 @@
 package org.jeecg.modules.adweb.dmp.dto.google.gtm;
 
+import com.google.common.base.Joiner;
+
 import lombok.*;
 import lombok.experimental.SuperBuilder;
 
@@ -21,6 +23,8 @@ public class GTMAccountDTO extends ResourceDTO {
     private boolean supportMultipleContainers;
 
     public static String toResourceName(String id) {
-        return StringUtils.isNumeric(id) ? "accounts" + RESOURCE_NAME_SPLITTER + id : id;
+        return StringUtils.isNumeric(id)
+                ? Joiner.on(RESOURCE_NAME_SPLITTER).join("accounts", id)
+                : id;
     }
 }

+ 7 - 2
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/dto/google/gtm/GTMContainerDTO.java

@@ -1,5 +1,7 @@
 package org.jeecg.modules.adweb.dmp.dto.google.gtm;
 
+import com.google.common.base.Joiner;
+
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NoArgsConstructor;
@@ -41,7 +43,10 @@ public class GTMContainerDTO extends ResourceDTO {
 
     private List<GTMTagDTO> tags;
 
-    public static String toResourceName(String id) {
-        return StringUtils.isNumeric(id) ? "accounts" + RESOURCE_NAME_SPLITTER + id : id;
+    public static String toResourceName(String accountId, String containerId) {
+        return StringUtils.isNumeric(accountId) && StringUtils.isNumeric(containerId)
+                ? Joiner.on(RESOURCE_NAME_SPLITTER)
+                        .join("accounts", accountId, "containers", containerId)
+                : containerId;
     }
 }

+ 50 - 6
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/google/GTMAdminService.java

@@ -18,6 +18,7 @@ import org.jeecg.modules.adweb.dmp.dto.google.analytics.CreatePropertyRequestDTO
 import org.jeecg.modules.adweb.dmp.dto.google.analytics.GAAccountDTO;
 import org.jeecg.modules.adweb.dmp.dto.google.analytics.GAPropertyDTO;
 import org.jeecg.modules.adweb.dmp.dto.google.gtm.CreateContainerRequestDTO;
+import org.jeecg.modules.adweb.dmp.dto.google.gtm.GTMAccountDTO;
 import org.jeecg.modules.adweb.dmp.dto.google.gtm.GTMContainerDTO;
 import org.jeecg.modules.adweb.dmp.entity.GoogleGTM;
 import org.jeecg.modules.adweb.dmp.service.IGoogleGTMService;
@@ -43,8 +44,9 @@ import java.util.List;
 public class GTMAdminService {
 
     private static final String GA_CREATE_PROPERTY_API_PATH = "/api/google/ga/properties/create";
+    private static final String GA_DELETE_PROPERTY_API_PATH = "/api/google/ga/properties/delete";
     private static final String GTM_CREATE_CONTAINER_API_PATH = "/api/google/gtm/containers/create";
-    private static final String GTM_GET_CONTAINER_API_PATH = "/api/google/gtm/containers/get";
+    private static final String GTM_DELETE_CONTAINER_API_PATH = "/api/google/gtm/containers/delete";
 
     @Value("${data-bridge.api.host}")
     private String dataBridgeApiHost;
@@ -93,7 +95,6 @@ public class GTMAdminService {
         List<GoogleGTM> googleGTMs =
                 googleGTMService.list(
                         new LambdaQueryWrapper<GoogleGTM>().eq(GoogleGTM::getSiteCode, siteCode));
-
         if (ListUtil.notEmpty(googleGTMs)) {
             log.info("站点 {} 对应的GoogleGTM已存在,ID = {}", siteCode, googleGTMs.get(0).getId());
             return googleGTMs.get(0);
@@ -109,7 +110,6 @@ public class GTMAdminService {
                         .displayName(siteName)
                         .url(siteUrl)
                         .build());
-
         GAPropertyDTO gaProperty =
                 RestTemplateUtil.postForObject(
                                 restTemplate,
@@ -126,11 +126,10 @@ public class GTMAdminService {
         createGTMContainerRequest.setRequestTime(System.currentTimeMillis());
         createGTMContainerRequest.setData(
                 CreateContainerRequestDTO.builder()
-                        .accountResourceName(GTMContainerDTO.toResourceName(gtmAccountId))
+                        .accountResourceName(GTMAccountDTO.toResourceName(gtmAccountId))
                         .displayName(siteName)
                         .googleTagId(gaProperty.getDataStreams().get(0).getStreamMeasurementId())
                         .build());
-
         GTMContainerDTO gtmContainer =
                 RestTemplateUtil.postForObject(
                                 restTemplate,
@@ -176,5 +175,50 @@ public class GTMAdminService {
      * <li>2. 删除{@link GoogleGTM}表
      */
     @VisibleForTesting
-    void deleteContainer(String siteCode) {}
+    boolean deleteContainer(String siteCode) {
+        List<GoogleGTM> googleGTMs =
+                googleGTMService.list(
+                        new LambdaQueryWrapper<GoogleGTM>().eq(GoogleGTM::getSiteCode, siteCode));
+        if (ListUtil.isEmpty(googleGTMs)) {
+            log.info("站点 {} 对应的GoogleGTM不存在,ID = {}", siteCode);
+            return false;
+        }
+
+        GoogleGTM googleGTM = googleGTMs.get(0);
+
+        // 1. 删除GA property - 通过data-bridge API
+        OpenAPIRequest<String> deleteGAPropertyRequest = new OpenAPIRequest<>();
+        deleteGAPropertyRequest.setRequestServer(this.getClass().getSimpleName());
+        deleteGAPropertyRequest.setRequestTime(System.currentTimeMillis());
+        deleteGAPropertyRequest.setData(GAPropertyDTO.toResourceName(googleGTM.getGaPropertyId()));
+        String gaPropertyResourceName =
+                RestTemplateUtil.postForObject(
+                                restTemplate,
+                                dataBridgeApiHost + GA_DELETE_PROPERTY_API_PATH,
+                                deleteGAPropertyRequest,
+                                new ParameterizedTypeReference<OpenAPIResponse<String>>() {})
+                        .getData();
+        log.info("为站点 {} 删除GA property: {}", siteCode, gaPropertyResourceName);
+
+        // 2. 删除GTM container - 通过data-bridge API
+        OpenAPIRequest<String> deleteGTMContainerRequest = new OpenAPIRequest<>();
+        deleteGTMContainerRequest.setRequestServer(this.getClass().getSimpleName());
+        deleteGTMContainerRequest.setRequestTime(System.currentTimeMillis());
+        deleteGTMContainerRequest.setData(
+                GTMContainerDTO.toResourceName(
+                        googleGTM.getGtmAccountId(), googleGTM.getGtmContainerId()));
+        String gtmContainerResourceName =
+                RestTemplateUtil.postForObject(
+                                restTemplate,
+                                dataBridgeApiHost + GTM_DELETE_CONTAINER_API_PATH,
+                                deleteGTMContainerRequest,
+                                new ParameterizedTypeReference<OpenAPIResponse<String>>() {})
+                        .getData();
+        log.info("为站点 {} 删除GTM container: {}", siteCode, gtmContainerResourceName);
+
+        // 3. 更新数据库
+        googleGTMService.removeById(googleGTM.getId());
+
+        return true;
+    }
 }

+ 13 - 0
jeecg-module-system/jeecg-system-start/src/test/java/org/jeecg/modules/adweb/dmp/service/google/GoogleServiceTest.java

@@ -4,6 +4,7 @@ import org.jeecg.common.util.FastJsonUtil;
 import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.GAReportRequestDTO;
 import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.ReportType;
 import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.data.CountryChartData;
+import org.jeecg.modules.adweb.dmp.entity.GoogleGTM;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
@@ -32,4 +33,16 @@ public class GoogleServiceTest {
 
         System.out.println(FastJsonUtil.toJSONString(countryReport));
     }
+
+    @Test
+    public void testCreateAndDeleteGTMContainer() {
+        GoogleGTM googleGTM =
+                gtmAdminService.createContainer(
+                        "230206anit40",
+                        "https://www.sourcingstone.com",
+                        "泉州索兴石业有限公司#" + this.getClass().getSimpleName());
+        System.out.println(FastJsonUtil.toJSONString(googleGTM));
+
+        gtmAdminService.deleteContainer(googleGTM.getSiteCode());
+    }
 }