|
@@ -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;
|
|
@@ -20,6 +19,8 @@ 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.dto.matomo.CreateSiteRequestDTO;
|
|
|
+import org.jeecg.modules.adweb.dmp.dto.matomo.MatomoSiteDTO;
|
|
|
import org.jeecg.modules.adweb.dmp.entity.GoogleGTM;
|
|
|
import org.jeecg.modules.adweb.dmp.service.IGoogleGTMService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -34,7 +35,7 @@ import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
- * Google Tag Manager + Google Analytics帐号及标签管理
|
|
|
+ * Google Tag Manager + Google Analytics + Matomo帐号及标签管理
|
|
|
*
|
|
|
* <p>http://data-bridge.v3.adwebcloud.com:9002/swagger-ui/index.html
|
|
|
*
|
|
@@ -47,6 +48,8 @@ public class GTMAdminService {
|
|
|
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 MATOMO_SITE_CREATE_API_PATH = "/api/matomo/sites/create";
|
|
|
+ private static final String MATOMO_SITE_DELETE_API_PATH = "/api/matomo/sites/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";
|
|
|
|
|
@@ -105,15 +108,13 @@ public class GTMAdminService {
|
|
|
}
|
|
|
|
|
|
// 1. 创建GA property - 通过data-bridge API
|
|
|
- OpenAPIRequest<CreatePropertyRequestDTO> createGAPropertyRequest = new OpenAPIRequest<>();
|
|
|
- createGAPropertyRequest.setRequestServer(this.getClass().getSimpleName());
|
|
|
- createGAPropertyRequest.setRequestTime(System.currentTimeMillis());
|
|
|
- createGAPropertyRequest.setData(
|
|
|
- CreatePropertyRequestDTO.builder()
|
|
|
- .accountResourceName(GAAccountDTO.toResourceName(gaAccountId))
|
|
|
- .displayName(siteName)
|
|
|
- .url(siteUrl)
|
|
|
- .build());
|
|
|
+ OpenAPIRequest<CreatePropertyRequestDTO> createGAPropertyRequest =
|
|
|
+ this.buildOpenAPIRequest(
|
|
|
+ CreatePropertyRequestDTO.builder()
|
|
|
+ .accountResourceName(GAAccountDTO.toResourceName(gaAccountId))
|
|
|
+ .displayName(siteName)
|
|
|
+ .url(siteUrl)
|
|
|
+ .build());
|
|
|
GAPropertyDTO gaProperty =
|
|
|
RestTemplateUtil.postForObject(
|
|
|
restTemplate,
|
|
@@ -123,17 +124,29 @@ public class GTMAdminService {
|
|
|
.getData();
|
|
|
log.info("为站点 {} 创建GA property: {}", siteCode, FastJsonUtil.toJSONString(gaProperty));
|
|
|
|
|
|
- // 2. 创建GTM container - 通过data-bridge API
|
|
|
+ // 2. 创建Matomo site - 通过data-bridge API
|
|
|
+ OpenAPIRequest<CreateSiteRequestDTO> createMatomoSiteRequest =
|
|
|
+ this.buildOpenAPIRequest(
|
|
|
+ CreateSiteRequestDTO.builder().siteName(siteName).url(siteUrl).build());
|
|
|
+ MatomoSiteDTO matomoSite =
|
|
|
+ RestTemplateUtil.postForObject(
|
|
|
+ restTemplate,
|
|
|
+ dataBridgeApiHost + MATOMO_SITE_CREATE_API_PATH,
|
|
|
+ createMatomoSiteRequest,
|
|
|
+ new ParameterizedTypeReference<OpenAPIResponse<MatomoSiteDTO>>() {})
|
|
|
+ .getData();
|
|
|
+ log.info("为站点 {} 创建Matomo site: {}", siteCode, FastJsonUtil.toJSONString(matomoSite));
|
|
|
+
|
|
|
+ // 3. 创建GTM container - 通过data-bridge API
|
|
|
OpenAPIRequest<CreateContainerRequestDTO> createGTMContainerRequest =
|
|
|
- new OpenAPIRequest<>();
|
|
|
- createGTMContainerRequest.setRequestServer(this.getClass().getSimpleName());
|
|
|
- createGTMContainerRequest.setRequestTime(System.currentTimeMillis());
|
|
|
- createGTMContainerRequest.setData(
|
|
|
- CreateContainerRequestDTO.builder()
|
|
|
- .accountResourceName(GTMAccountDTO.toResourceName(gtmAccountId))
|
|
|
- .displayName(siteName)
|
|
|
- .googleTagId(gaProperty.getDataStreams().get(0).getStreamMeasurementId())
|
|
|
- .build());
|
|
|
+ this.buildOpenAPIRequest(
|
|
|
+ CreateContainerRequestDTO.builder()
|
|
|
+ .accountResourceName(GTMAccountDTO.toResourceName(gtmAccountId))
|
|
|
+ .displayName(siteName)
|
|
|
+ .gaMeasurementId(
|
|
|
+ gaProperty.getDataStreams().get(0).getStreamMeasurementId())
|
|
|
+ .matomoSiteId(matomoSite.getSiteId())
|
|
|
+ .build());
|
|
|
GTMContainerDTO gtmContainer =
|
|
|
RestTemplateUtil.postForObject(
|
|
|
restTemplate,
|
|
@@ -144,18 +157,19 @@ public class GTMAdminService {
|
|
|
.getData();
|
|
|
log.info("为站点 {} 创建GTM container: {}", siteCode, FastJsonUtil.toJSONString(gtmContainer));
|
|
|
|
|
|
- // 3. 更新数据库
|
|
|
+ // 4. 更新数据库
|
|
|
googleGTM = new GoogleGTM();
|
|
|
googleGTM.setSiteCode(siteCode);
|
|
|
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);
|
|
|
|
|
|
return googleGTM;
|
|
@@ -164,20 +178,13 @@ public class GTMAdminService {
|
|
|
/** 获取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);
|
|
|
+ OpenAPIRequest<?> listGAAccountsRequest = this.buildOpenAPIRequest(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()));
|
|
|
+ new ParameterizedTypeReference<>() {});
|
|
|
|
|
|
return listGAAccountsResponse.getData().stream()
|
|
|
.filter(gaAccount -> gaAccountIds.contains(gaAccount.getId()))
|
|
@@ -187,13 +194,13 @@ public class GTMAdminService {
|
|
|
/**
|
|
|
* 返回GTM container的head + body snippet代码
|
|
|
*
|
|
|
- * @param gtmTagId
|
|
|
+ * @param gtmPublicId
|
|
|
* @return
|
|
|
*/
|
|
|
- public Pair<String, String> getSnippets(String gtmTagId) {
|
|
|
+ public Pair<String, String> getSnippets(String gtmPublicId) {
|
|
|
return Pair.of(
|
|
|
- String.format(headSnippetTemplate, gtmTagId),
|
|
|
- String.format(bodySnippetTemplate, gtmTagId));
|
|
|
+ String.format(headSnippetTemplate, gtmPublicId),
|
|
|
+ String.format(bodySnippetTemplate, gtmPublicId));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -212,10 +219,8 @@ public class GTMAdminService {
|
|
|
}
|
|
|
|
|
|
// 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()));
|
|
|
+ OpenAPIRequest<String> deleteGAPropertyRequest =
|
|
|
+ this.buildOpenAPIRequest(GAPropertyDTO.toResourceName(googleGTM.getGaPropertyId()));
|
|
|
String gaPropertyResourceName =
|
|
|
RestTemplateUtil.postForObject(
|
|
|
restTemplate,
|
|
@@ -225,13 +230,23 @@ public class GTMAdminService {
|
|
|
.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()));
|
|
|
+ // 2. 删除Matomo site - 通过data-bridge API
|
|
|
+ OpenAPIRequest<String> deleteMatomoSiteRequest =
|
|
|
+ this.buildOpenAPIRequest(googleGTM.getMatomoSiteId());
|
|
|
+ String matomoSiteId =
|
|
|
+ RestTemplateUtil.postForObject(
|
|
|
+ restTemplate,
|
|
|
+ dataBridgeApiHost + MATOMO_SITE_DELETE_API_PATH,
|
|
|
+ deleteMatomoSiteRequest,
|
|
|
+ new ParameterizedTypeReference<OpenAPIResponse<String>>() {})
|
|
|
+ .getData();
|
|
|
+ log.info("为站点 {} 删除Matomo site: {}", siteCode, matomoSiteId);
|
|
|
+
|
|
|
+ // 3. 删除GTM container - 通过data-bridge API
|
|
|
+ OpenAPIRequest<String> deleteGTMContainerRequest =
|
|
|
+ this.buildOpenAPIRequest(
|
|
|
+ GTMContainerDTO.toResourceName(
|
|
|
+ googleGTM.getGtmAccountId(), googleGTM.getGtmContainerId()));
|
|
|
String gtmContainerResourceName =
|
|
|
RestTemplateUtil.postForObject(
|
|
|
restTemplate,
|
|
@@ -246,4 +261,12 @@ public class GTMAdminService {
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ private <T> OpenAPIRequest<T> buildOpenAPIRequest(T data) {
|
|
|
+ OpenAPIRequest<T> openAPIRequest = new OpenAPIRequest<>();
|
|
|
+ openAPIRequest.setRequestServer(this.getClass().getSimpleName());
|
|
|
+ openAPIRequest.setRequestTime(System.currentTimeMillis());
|
|
|
+ openAPIRequest.setData(data);
|
|
|
+ return openAPIRequest;
|
|
|
+ }
|
|
|
}
|