Ver Fonte

Rename name to resource name

wfansh há 6 meses atrás
pai
commit
529bea28c0

+ 37 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/controller/GAController.java

@@ -7,12 +7,14 @@ import com.wechi.adweb.bridge.common.OpenAPIResponse;
 import com.wechi.adweb.bridge.exception.BadRequestException;
 import com.wechi.adweb.bridge.exception.DataException;
 import com.wechi.adweb.bridge.google.analytics.dto.GAAccountDTO;
+import com.wechi.adweb.bridge.google.analytics.dto.GADataStreamDTO;
 import com.wechi.adweb.bridge.google.analytics.dto.GAPropertyDTO;
 import com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO;
 import com.wechi.adweb.bridge.google.analytics.dto.report.ReportType;
 import com.wechi.adweb.bridge.google.analytics.dto.report.data.GAReportDataDTO;
 import com.wechi.adweb.bridge.google.analytics.service.GAAdminService;
 import com.wechi.adweb.bridge.google.analytics.service.GADataService;
+import com.wechi.adweb.bridge.google.common.CreateRequestDTO;
 import com.wechi.adweb.bridge.util.JsonUtils;
 
 import io.swagger.v3.oas.annotations.Operation;
@@ -81,6 +83,41 @@ public class GAController extends BaseController {
                 .build();
     }
 
+    // It's recommended to use hyphens (-) instead of underscores (_) in URLs.
+    // See https://developers.google.com/search/docs/crawling-indexing/url-structure
+    @PostMapping("/data-streams/create")
+    @ResponseBody
+    public OpenAPIResponse<GADataStreamDTO> createGAPropertyWithDataStream(
+            @RequestBody OpenAPIRequest<CreateRequestDTO> apiRequest)
+            throws BadRequestException, DataException {
+        long start = System.currentTimeMillis();
+        log.info(
+                "****** createGAPropertyWithDataStream() ****** apiRequest = {}",
+                JsonUtils.toJson(apiRequest));
+        CreateRequestDTO createRequest = apiRequest.getData();
+
+        // 1. Validates the request parameters.
+        if (StringUtils.isEmpty(createRequest.getAccountResourceName())
+                || StringUtils.isEmpty(createRequest.getDisplayName())
+                || StringUtils.isEmpty(createRequest.getUrl())) {
+            throw new BadRequestException(apiRequest);
+        }
+
+        // 2. Executes the API request.
+        GADataStreamDTO createdDataStream =
+                gaAdminService.createGAPropertyWithDataStream(
+                        createRequest.getAccountResourceName(),
+                        createRequest.getDisplayName(),
+                        createRequest.getUrl());
+        log.info(
+                "****** createGAPropertyWithDataStream() ****** duration = {} seconds",
+                getElapsedSeconds(start));
+        return OpenAPIResponse.<GADataStreamDTO>builder()
+                .status(APIStatus.SUCCESS)
+                .data(createdDataStream)
+                .build();
+    }
+
     @PostMapping("/report")
     @ResponseBody
     public OpenAPIResponse<List<? extends GAReportDataDTO>> getGAReport(

+ 1 - 1
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/GADataStreamDTO.java

@@ -16,7 +16,7 @@ import lombok.experimental.SuperBuilder;
 @EqualsAndHashCode(callSuper = true)
 public class GADataStreamDTO extends ResourceDTO {
 
-    private String property;
+    private String propertyResourceName;
 
     private String streamMeasurementId;
 

+ 2 - 2
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/GAPropertyDTO.java

@@ -20,9 +20,9 @@ public class GAPropertyDTO extends ResourceDTO {
 
     private String propertyType;
 
-    private String account;
+    private String accountResourceName;
 
-    private String parent;
+    private String parentResourceName;
 
     private String industryCategory;
 

+ 4 - 4
src/main/java/com/wechi/adweb/bridge/google/analytics/service/GAAdminService.java

@@ -101,8 +101,8 @@ public class GAAdminService {
                                 .resourceName(property.getName())
                                 .displayName(property.getDisplayName())
                                 .propertyType(property.getPropertyType().toString())
-                                .account(property.getAccount())
-                                .parent(property.getParent())
+                                .accountResourceName(property.getAccount())
+                                .parentResourceName(property.getParent())
                                 .industryCategory(property.getIndustryCategory().toString())
                                 .timeZone(property.getTimeZone())
                                 .currencyCode(property.getCurrencyCode())
@@ -156,7 +156,7 @@ public class GAAdminService {
                                 .id(parseId(dataStream.getName()))
                                 .resourceName(dataStream.getName())
                                 .displayName(dataStream.getDisplayName())
-                                .property(propertyResourceName)
+                                .propertyResourceName(propertyResourceName)
                                 .streamMeasurementId(
                                         dataStream.getWebStreamData().getMeasurementId())
                                 .streamDefaultUrl(dataStream.getWebStreamData().getDefaultUri())
@@ -208,7 +208,7 @@ public class GAAdminService {
                     .id(parseId(createdDataStream.getName()))
                     .resourceName(createdDataStream.getName())
                     .displayName(createdDataStream.getDisplayName())
-                    .property(createdProperty.getName())
+                    .propertyResourceName(createdProperty.getName())
                     .streamMeasurementId(createdDataStream.getWebStreamData().getMeasurementId())
                     .streamDefaultUrl(createdDataStream.getWebStreamData().getDefaultUri())
                     .build();

+ 19 - 0
src/main/java/com/wechi/adweb/bridge/google/common/CreateRequestDTO.java

@@ -0,0 +1,19 @@
+package com.wechi.adweb.bridge.google.common;
+
+import lombok.Data;
+
+/**
+ * Common request DTO to create GA properties / data streams, and GTM containers.
+ *
+ * @author wfansh
+ */
+@Data
+public class CreateRequestDTO {
+
+    /** Resource name of the parent account. */
+    private String accountResourceName;
+
+    private String displayName;
+
+    private String url;
+}