wfansh 3 months ago
parent
commit
92f1f8755a

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

@@ -1,12 +1,16 @@
 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.modules.adweb.dmp.dto.google.analytics.GAAccountDTO;
 import org.jeecg.modules.adweb.dmp.entity.GoogleGTM;
+import org.jeecg.modules.adweb.dmp.service.IGoogleGTMService;
 import org.jeecg.modules.adweb.dmp.service.google.GTMAdminService;
 import org.jeecg.modules.adweb.site.entity.AdwebSite;
 import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
@@ -16,6 +20,9 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.List;
+import java.util.Objects;
+
 /**
  * GTM容器和标签管理
  *
@@ -26,20 +33,40 @@ import org.springframework.web.bind.annotation.RestController;
 @RequestMapping("/gtm")
 @Slf4j
 public class GTMController {
-
     @Autowired private IAdwebSiteService adwebSiteService;
-
+    @Autowired private IGoogleGTMService googleGTMService;
     @Autowired private GTMAdminService gtmAdminService;
 
-    @RequestMapping(value = "/list", method = RequestMethod.GET)
+    @RequestMapping(value = "/get", method = RequestMethod.GET)
+    @ResponseBody
+    public Result<AdwebSite> getGTMSnippets(String siteCode) {
+        GoogleGTM googleGTM =
+                googleGTMService.getOne(
+                        new LambdaQueryWrapper<GoogleGTM>().eq(GoogleGTM::getSiteCode, siteCode));
+        if (Objects.isNull(googleGTM)) {
+            log.info("站点 {} 对应的GoogleGTM不存在", siteCode);
+            return null;
+        }
+
+        AdwebSite adwebSite = new AdwebSite();
+        Pair<String, String> snippets = gtmAdminService.getSnippets(googleGTM.getGtmTagId());
+        adwebSite.setGtmHead(snippets.getKey());
+        adwebSite.setGtmBody(snippets.getValue());
+
+        return Result.ok(adwebSite);
+    }
+
+    @RequestMapping(value = "/create", method = RequestMethod.GET)
     @ResponseBody
-    public Result<AdwebSite> getGTMSnippets(Integer id) {
-        AdwebSite adwebSite = adwebSiteService.getById(id);
+    public Result<AdwebSite> createGTMSnippets(String siteCode, String gaAccountId) {
+        AdwebSite adwebSite = adwebSiteService.getSiteByCode(siteCode);
 
-        // 如果GoogleGTM表中已存在,不再创建,返回现有记录
         GoogleGTM googleGTM =
                 gtmAdminService.createContainer(
-                        adwebSite.getCode(), adwebSite.getDomain(), adwebSite.getName());
+                        adwebSite.getCode(),
+                        adwebSite.getDomain(),
+                        adwebSite.getName(),
+                        gaAccountId);
 
         Pair<String, String> snippets = gtmAdminService.getSnippets(googleGTM.getGtmTagId());
         adwebSite.setGtmHead(snippets.getKey());
@@ -47,4 +74,15 @@ public class GTMController {
 
         return Result.ok(adwebSite);
     }
+
+    @RequestMapping(value = "/listGAAccounts", method = RequestMethod.GET)
+    @ResponseBody
+    public Result<List<Pair<String, String>>> listGAAccounts() {
+        List<GAAccountDTO> gaAccounts = gtmAdminService.listGAAccounts();
+
+        return Result.ok(
+                gaAccounts.stream()
+                        .map(gaAccount -> Pair.of(gaAccount.getDisplayName(), gaAccount.getId()))
+                        .toList());
+    }
 }

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

@@ -8,7 +8,7 @@ import jakarta.annotation.PostConstruct;
 
 import lombok.extern.slf4j.Slf4j;
 
-import org.apache.commons.collections4.CollectionUtils;
+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;
@@ -31,6 +31,7 @@ import org.springframework.web.client.RestTemplate;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * Google Tag Manager + Google Analytics帐号及标签管理
@@ -43,10 +44,11 @@ import java.util.List;
 @Service
 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_DELETE_CONTAINER_API_PATH = "/api/google/gtm/containers/delete";
+    private static final String GA_ACCOUNT_LIST_API_PATH = "/api/google/ga/accounts/list";
+    private static final String GA_PROPERTY_CREATE_API_PATH = "/api/google/ga/properties/create";
+    private static final String GA_PROPERTY_DELETE_API_PATH = "/api/google/ga/properties/delete";
+    private static final String GTM_CONTAINER_CREATE_API_PATH = "/api/google/gtm/containers/create";
+    private static final String GTM_CONTAINER_DELETE_API_PATH = "/api/google/gtm/containers/delete";
 
     @Value("${data-bridge.api.host}")
     private String dataBridgeApiHost;
@@ -57,8 +59,8 @@ public class GTMAdminService {
     @Value("${data-bridge.gtm.account-id}")
     private String gtmAccountId;
 
-    @Value("${data-bridge.ga.account-id}")
-    private String gaAccountId;
+    @Value("${data-bridge.ga.account-ids}")
+    private List<String> gaAccountIds; // v3系统可用的GA帐号IDs
 
     @Autowired private IGoogleGTMService googleGTMService;
 
@@ -91,13 +93,15 @@ public class GTMAdminService {
      * <li>1. 创建GTM container + GA property
      * <li>2. 更新{@link GoogleGTM}表
      */
-    public GoogleGTM createContainer(String siteCode, String siteUrl, String siteName) {
-        List<GoogleGTM> googleGTMs =
-                googleGTMService.list(
+    public GoogleGTM createContainer(
+            String siteCode, String siteUrl, String siteName, String gaAccountId) {
+        GoogleGTM googleGTM =
+                googleGTMService.getOne(
                         new LambdaQueryWrapper<GoogleGTM>().eq(GoogleGTM::getSiteCode, siteCode));
-        if (CollectionUtils.isNotEmpty(googleGTMs)) {
-            log.info("站点 {} 对应的GoogleGTM已存在,ID = {}", siteCode, googleGTMs.get(0).getId());
-            return googleGTMs.get(0);
+        // 如果GoogleGTM表中已存在,不再创建,返回现有记录
+        if (Objects.nonNull(googleGTM)) {
+            log.info("站点 {} 对应的GoogleGTM已存在,ID = {}", siteCode, googleGTM.getId());
+            return googleGTM;
         }
 
         // 1. 创建GA property - 通过data-bridge API
@@ -113,7 +117,7 @@ public class GTMAdminService {
         GAPropertyDTO gaProperty =
                 RestTemplateUtil.postForObject(
                                 restTemplate,
-                                dataBridgeApiHost + GA_CREATE_PROPERTY_API_PATH,
+                                dataBridgeApiHost + GA_PROPERTY_CREATE_API_PATH,
                                 createGAPropertyRequest,
                                 new ParameterizedTypeReference<OpenAPIResponse<GAPropertyDTO>>() {})
                         .getData();
@@ -133,7 +137,7 @@ public class GTMAdminService {
         GTMContainerDTO gtmContainer =
                 RestTemplateUtil.postForObject(
                                 restTemplate,
-                                dataBridgeApiHost + GTM_CREATE_CONTAINER_API_PATH,
+                                dataBridgeApiHost + GTM_CONTAINER_CREATE_API_PATH,
                                 createGTMContainerRequest,
                                 new ParameterizedTypeReference<
                                         OpenAPIResponse<GTMContainerDTO>>() {})
@@ -141,7 +145,7 @@ public class GTMAdminService {
         log.info("为站点 {} 创建GTM container: {}", siteCode, FastJsonUtil.toJSONString(gtmContainer));
 
         // 3. 更新数据库
-        GoogleGTM googleGTM = new GoogleGTM();
+        googleGTM = new GoogleGTM();
         googleGTM.setSiteCode(siteCode);
         googleGTM.setUid(null); // TODO
         googleGTM.setGtmAccountId(gtmAccountId);
@@ -157,6 +161,29 @@ public class GTMAdminService {
         return googleGTM;
     }
 
+    /** 获取GA account列表,并根据v3系统配置为可用的{@link #gaAccountIds}过滤 */
+    public List<GAAccountDTO> listGAAccounts() {
+        // 获取GA account列表 - 通过data-bridge API
+        OpenAPIRequest<?> listGAAccountsRequest = new OpenAPIRequest<>();
+        listGAAccountsRequest.setRequestServer(this.getClass().getSimpleName());
+        listGAAccountsRequest.setRequestTime(System.currentTimeMillis());
+        listGAAccountsRequest.setData(null);
+        OpenAPIResponse<List<GAAccountDTO>> listGAAccountsResponse =
+                RestTemplateUtil.postForObject(
+                        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()));
+        return listGAAccountsResponse.getData().stream()
+                .filter(gaAccount -> gaAccountIds.contains(gaAccount.getId()))
+                .toList();
+    }
+
     /**
      * 返回GTM container的head + body snippet代码
      *
@@ -176,16 +203,14 @@ public class GTMAdminService {
      */
     @VisibleForTesting
     boolean deleteContainer(String siteCode) {
-        List<GoogleGTM> googleGTMs =
-                googleGTMService.list(
+        GoogleGTM googleGTM =
+                googleGTMService.getOne(
                         new LambdaQueryWrapper<GoogleGTM>().eq(GoogleGTM::getSiteCode, siteCode));
-        if (CollectionUtils.isEmpty(googleGTMs)) {
+        if (Objects.isNull(googleGTM)) {
             log.info("站点 {} 对应的GoogleGTM不存在", siteCode);
             return false;
         }
 
-        GoogleGTM googleGTM = googleGTMs.get(0);
-
         // 1. 删除GA property - 通过data-bridge API
         OpenAPIRequest<String> deleteGAPropertyRequest = new OpenAPIRequest<>();
         deleteGAPropertyRequest.setRequestServer(this.getClass().getSimpleName());
@@ -194,7 +219,7 @@ public class GTMAdminService {
         String gaPropertyResourceName =
                 RestTemplateUtil.postForObject(
                                 restTemplate,
-                                dataBridgeApiHost + GA_DELETE_PROPERTY_API_PATH,
+                                dataBridgeApiHost + GA_PROPERTY_DELETE_API_PATH,
                                 deleteGAPropertyRequest,
                                 new ParameterizedTypeReference<OpenAPIResponse<String>>() {})
                         .getData();
@@ -210,7 +235,7 @@ public class GTMAdminService {
         String gtmContainerResourceName =
                 RestTemplateUtil.postForObject(
                                 restTemplate,
-                                dataBridgeApiHost + GTM_DELETE_CONTAINER_API_PATH,
+                                dataBridgeApiHost + GTM_CONTAINER_DELETE_API_PATH,
                                 deleteGTMContainerRequest,
                                 new ParameterizedTypeReference<OpenAPIResponse<String>>() {})
                         .getData();

+ 1 - 1
jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml

@@ -382,7 +382,7 @@ data-bridge:
   gtm:
     account-id: 6000226571
   ga:
-    account-id: 191734056
+    account-ids: 191734056, 339897686 # v3系统中可用的GA帐号
 
 ### GEOIP MMDB 静态数据库文件
 geoip:

+ 1 - 1
jeecg-module-system/jeecg-system-start/src/main/resources/application-prod.yml

@@ -384,7 +384,7 @@ data-bridge:
   gtm:
     account-id: 6000226571
   ga:
-    account-id: 191734056
+    account-ids: 191734056, 339897686 # v3系统中可用的GA帐号
 
 ### GEOIP MMDB 静态数据库文件
 geoip:

+ 1 - 1
jeecg-module-system/jeecg-system-start/src/main/resources/application-test.yml

@@ -381,7 +381,7 @@ data-bridge:
   gtm:
     account-id: 6000226571
   ga:
-    account-id: 191734056
+    account-ids: 191734056, 339897686 # v3系统中可用的GA帐号
 
 ### GEOIP MMDB 静态数据库文件
 geoip: