wfansh 2 months ago
parent
commit
a716af1609

+ 12 - 18
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/controller/GTMController.java

@@ -1,8 +1,11 @@
 package org.jeecg.modules.adweb.dmp.controller;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+
 import io.swagger.v3.oas.annotations.tags.Tag;
+
 import lombok.extern.slf4j.Slf4j;
+
 import org.apache.commons.lang3.tuple.Pair;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.constant.CommonConstant;
@@ -31,16 +34,11 @@ import java.util.Objects;
 @RequestMapping("/gtm")
 @Slf4j
 public class GTMController {
-    @Autowired
-    private IAdwebSiteService adwebSiteService;
-    @Autowired
-    private IGoogleGTMService googleGTMService;
-    @Autowired
-    private GTMAdminService gtmAdminService;
-
-    /**
-     * 获取站点绑定的GTM snippets
-     */
+    @Autowired private IAdwebSiteService adwebSiteService;
+    @Autowired private IGoogleGTMService googleGTMService;
+    @Autowired private GTMAdminService gtmAdminService;
+
+    /** 获取站点绑定的GTM snippets */
     @RequestMapping(value = "/get", method = RequestMethod.GET)
     @ResponseBody
     public Result<AdwebSite> getGTMSnippets(String siteCode) {
@@ -52,7 +50,7 @@ public class GTMController {
             return Result.error(CommonConstant.SC_INTERNAL_NOT_FOUND_404, "GoogleGTM不存在");
         }
 
-        Pair<String, String> snippets = gtmAdminService.getSnippets(googleGTM.getGtmTagId());
+        Pair<String, String> snippets = gtmAdminService.getSnippets(googleGTM.getGtmPublicId());
 
         AdwebSite adwebSite = new AdwebSite();
         adwebSite.setGtmHead(snippets.getLeft());
@@ -61,9 +59,7 @@ public class GTMController {
         return Result.ok(adwebSite);
     }
 
-    /**
-     * 为站点添加GTM snippets
-     */
+    /** 为站点添加GTM snippets */
     @RequestMapping(value = "/add", method = RequestMethod.POST)
     @ResponseBody
     public Result<AdwebSite> addGTMSnippets(String siteCode, String gaAccountId) {
@@ -77,16 +73,14 @@ public class GTMController {
                         adwebSite.getName(),
                         gaAccountId);
 
-        Pair<String, String> snippets = gtmAdminService.getSnippets(googleGTM.getGtmTagId());
+        Pair<String, String> snippets = gtmAdminService.getSnippets(googleGTM.getGtmPublicId());
         adwebSite.setGtmHead(snippets.getLeft());
         adwebSite.setGtmBody(snippets.getRight());
 
         return Result.ok(adwebSite);
     }
 
-    /**
-     * 获取所有可用的GA帐号
-     */
+    /** 获取所有可用的GA帐号 */
     @RequestMapping(value = "/listGAAccounts", method = RequestMethod.GET)
     @ResponseBody
     public Result<List<GAAccountDTO>> listGAAccounts() {

+ 8 - 8
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/entity/GoogleGTM.java

@@ -56,10 +56,10 @@ public class GoogleGTM implements Serializable {
     @Schema(description = "gtmContainerId")
     private String gtmContainerId;
 
-    /** gtmTagId */
-    @Excel(name = "gtmTagId", width = 15)
-    @Schema(description = "gtmTagId")
-    private String gtmTagId;
+    /** gtmPublicId */
+    @Excel(name = "gtmPublicId", width = 15)
+    @Schema(description = "gtmPublicId")
+    private String gtmPublicId;
 
     /** gaAccountId */
     @Excel(name = "gaAccountId", width = 15)
@@ -82,11 +82,11 @@ public class GoogleGTM implements Serializable {
     private String gaSiteUrl;
 
     /** gaTagId */
-    @Excel(name = "gaTagId", width = 15)
-    @Schema(description = "gaTagId")
-    private String gaTagId;
+    @Excel(name = "gaMeasurementId", width = 15)
+    @Schema(description = "gaMeasurementId")
+    private String gaMeasurementId;
 
-    /** gaTagId */
+    /** matomoSiteId */
     @Excel(name = "matomoSiteId", width = 15)
     @Schema(description = "matomoSiteId")
     private String matomoSiteId;

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

@@ -8,7 +8,6 @@ import jakarta.annotation.PostConstruct;
 
 import lombok.extern.slf4j.Slf4j;
 
-import org.apache.commons.lang3.reflect.TypeUtils;
 import org.apache.commons.lang3.tuple.Pair;
 import org.jeecg.common.util.FastJsonUtil;
 import org.jeecg.modules.adweb.common.databridge.OpenAPIRequest;
@@ -116,15 +115,13 @@ public class GTMAdminService {
                                 .displayName(siteName)
                                 .url(siteUrl)
                                 .build());
-        OpenAPIResponse<GAPropertyDTO> createGAPropertyResponse =
-
+        GAPropertyDTO gaProperty =
                 RestTemplateUtil.postForObject(
                                 restTemplate,
                                 dataBridgeApiHost + GA_PROPERTY_CREATE_API_PATH,
                                 createGAPropertyRequest,
                                 new ParameterizedTypeReference<OpenAPIResponse<GAPropertyDTO>>() {})
-                        ;
-        GAPropertyDTO gaProperty
+                        .getData();
         log.info("为站点 {} 创建GA property: {}", siteCode, FastJsonUtil.toJSONString(gaProperty));
 
         // 2. 创建Matomo site - 通过data-bridge API
@@ -166,12 +163,12 @@ public class GTMAdminService {
         googleGTM.setUid(null); // TODO
         googleGTM.setGtmAccountId(gtmAccountId);
         googleGTM.setGtmContainerId(gtmContainer.getId());
-        googleGTM.setGtmTagId(gtmContainer.getPublicId());
+        googleGTM.setGtmPublicId(gtmContainer.getPublicId());
         googleGTM.setGaAccountId(gaAccountId);
         googleGTM.setGaPropertyId(gaProperty.getId());
         googleGTM.setGaVersion("V4"); // GA4 https://support.google.com/analytics/answer/10089681
         googleGTM.setGaSiteUrl(siteUrl);
-        googleGTM.setGaTagId(gaProperty.getDataStreams().get(0).getStreamMeasurementId());
+        googleGTM.setGaMeasurementId(gaProperty.getDataStreams().get(0).getStreamMeasurementId());
         googleGTM.setMatomoSiteId(matomoSite.getSiteId());
         googleGTMService.save(googleGTM);
 
@@ -187,11 +184,7 @@ public class GTMAdminService {
                         restTemplate,
                         dataBridgeApiHost + GA_ACCOUNT_LIST_API_PATH,
                         listGAAccountsRequest,
-                        ParameterizedTypeReference.forType(
-                                TypeUtils.parameterize(
-                                        OpenAPIResponse.class,
-                                        TypeUtils.parameterize(List.class, GAAccountDTO.class))));
-        log.info("获取GA accounts: {}", FastJsonUtil.toJSONString(listGAAccountsResponse.getData()));
+                        new ParameterizedTypeReference<OpenAPIResponse<List<GAAccountDTO>>>() {});
 
         return listGAAccountsResponse.getData().stream()
                 .filter(gaAccount -> gaAccountIds.contains(gaAccount.getId()))
@@ -201,13 +194,13 @@ public class GTMAdminService {
     /**
      * 返回GTM container的head + body snippet代码
      *
-     * @param gtmTagId
+     * @param gtmPublic
      * @return
      */
-    public Pair<String, String> getSnippets(String gtmTagId) {
+    public Pair<String, String> getSnippets(String gtmPublic) {
         return Pair.of(
-                String.format(headSnippetTemplate, gtmTagId),
-                String.format(bodySnippetTemplate, gtmTagId));
+                String.format(headSnippetTemplate, gtmPublic),
+                String.format(bodySnippetTemplate, gtmPublic));
     }
 
     /**
@@ -239,7 +232,7 @@ public class GTMAdminService {
 
         // 2. 删除Matomo site - 通过data-bridge API
         OpenAPIRequest<String> deleteMatomoSiteRequest =
-                this.buildOpenAPIRequest(GAPropertyDTO.toResourceName(googleGTM.getMatomoSiteId()));
+                this.buildOpenAPIRequest(googleGTM.getMatomoSiteId());
         String matomoSiteId =
                 RestTemplateUtil.postForObject(
                                 restTemplate,