Przeglądaj źródła

Create GA property

wfansh 6 miesięcy temu
rodzic
commit
f79c35f19b

+ 14 - 17
src/main/java/com/wechi/adweb/bridge/google/analytics/controller/GAController.java

@@ -6,15 +6,14 @@ import com.wechi.adweb.bridge.common.OpenAPIRequest;
 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.CreatePropertyRequestDTO;
 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;
@@ -83,38 +82,36 @@ 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")
+    @PostMapping("/properties/create")
     @ResponseBody
-    public OpenAPIResponse<GADataStreamDTO> createGAPropertyWithDataStream(
-            @RequestBody OpenAPIRequest<CreateRequestDTO> apiRequest)
+    public OpenAPIResponse<GAPropertyDTO> createGAPropertyWithDataStream(
+            @RequestBody OpenAPIRequest<CreatePropertyRequestDTO> apiRequest)
             throws BadRequestException, DataException {
         long start = System.currentTimeMillis();
         log.info(
                 "****** createGAPropertyWithDataStream() ****** apiRequest = {}",
                 JsonUtils.toJson(apiRequest));
-        CreateRequestDTO createRequest = apiRequest.getData();
+        CreatePropertyRequestDTO createPropertyRequest = apiRequest.getData();
 
         // 1. Validates the request parameters.
-        if (StringUtils.isEmpty(createRequest.getAccountResourceName())
-                || StringUtils.isEmpty(createRequest.getDisplayName())
-                || StringUtils.isEmpty(createRequest.getUrl())) {
+        if (StringUtils.isEmpty(createPropertyRequest.getAccountResourceName())
+                || StringUtils.isEmpty(createPropertyRequest.getDisplayName())
+                || StringUtils.isEmpty(createPropertyRequest.getUrl())) {
             throw new BadRequestException(apiRequest);
         }
 
         // 2. Executes the API request.
-        GADataStreamDTO createdDataStream =
+        GAPropertyDTO gaProperty =
                 gaAdminService.createGAPropertyWithDataStream(
-                        createRequest.getAccountResourceName(),
-                        createRequest.getDisplayName(),
-                        createRequest.getUrl());
+                        createPropertyRequest.getAccountResourceName(),
+                        createPropertyRequest.getDisplayName(),
+                        createPropertyRequest.getUrl());
         log.info(
                 "****** createGAPropertyWithDataStream() ****** duration = {} seconds",
                 getElapsedSeconds(start));
-        return OpenAPIResponse.<GADataStreamDTO>builder()
+        return OpenAPIResponse.<GAPropertyDTO>builder()
                 .status(APIStatus.SUCCESS)
-                .data(createdDataStream)
+                .data(gaProperty)
                 .build();
     }
 

+ 16 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/CreatePropertyRequestDTO.java

@@ -0,0 +1,16 @@
+package com.wechi.adweb.bridge.google.analytics.dto;
+
+import lombok.Data;
+
+/**
+ * @author wfansh
+ */
+@Data
+public class CreatePropertyRequestDTO {
+
+    private String accountResourceName;
+
+    private String displayName;
+
+    private String url;
+}

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

@@ -140,7 +140,7 @@ public class GAAdminService {
         }
     }
 
-    public List<GADataStreamDTO> listGADataStreams(String propertyResourceName)
+    private List<GADataStreamDTO> listGADataStreams(String propertyResourceName)
             throws DataException {
         try (AnalyticsAdminServiceClient analyticsAdminServiceClient =
                 AnalyticsAdminServiceClient.create(adminServiceSettings)) {
@@ -177,7 +177,7 @@ public class GAAdminService {
         }
     }
 
-    public GADataStreamDTO createGAPropertyWithDataStream(
+    public GAPropertyDTO createGAPropertyWithDataStream(
             String accountResourceName, String displayName, String url) throws DataException {
         try (AnalyticsAdminServiceClient analyticsAdminServiceClient =
                 AnalyticsAdminServiceClient.create(adminServiceSettings)) {
@@ -204,13 +204,32 @@ public class GAAdminService {
                             createdProperty.getName(), dataStream);
             log.info("Created data stream {}", JsonFormat.printer().print(createdProperty));
 
-            return GADataStreamDTO.builder()
-                    .id(parseId(createdDataStream.getName()))
-                    .resourceName(createdDataStream.getName())
-                    .displayName(createdDataStream.getDisplayName())
-                    .propertyResourceName(createdProperty.getName())
-                    .streamMeasurementId(createdDataStream.getWebStreamData().getMeasurementId())
-                    .streamDefaultUrl(createdDataStream.getWebStreamData().getDefaultUri())
+            // 3. Converts and returns.
+            GADataStreamDTO gaDataStream =
+                    GADataStreamDTO.builder()
+                            .id(parseId(createdDataStream.getName()))
+                            .resourceName(createdDataStream.getName())
+                            .displayName(createdDataStream.getDisplayName())
+                            .propertyResourceName(createdProperty.getName())
+                            .streamMeasurementId(
+                                    createdDataStream.getWebStreamData().getMeasurementId())
+                            .streamDefaultUrl(createdDataStream.getWebStreamData().getDefaultUri())
+                            .build();
+
+            return GAPropertyDTO.builder()
+                    .id(parseId(createdProperty.getName()))
+                    .resourceName(createdProperty.getName())
+                    .displayName(createdProperty.getDisplayName())
+                    .propertyType(createdProperty.getPropertyType().toString())
+                    .accountResourceName(createdProperty.getAccount())
+                    .parentResourceName(createdProperty.getParent())
+                    .industryCategory(createdProperty.getIndustryCategory().toString())
+                    .timeZone(createdProperty.getTimeZone())
+                    .currencyCode(createdProperty.getCurrencyCode())
+                    .serviceLevel(createdProperty.getServiceLevel().toString())
+                    .createTime(createdProperty.getCreateTime().getSeconds())
+                    .updateTime(createdProperty.getUpdateTime().getSeconds())
+                    .dataStreams(Collections.singletonList(gaDataStream))
                     .build();
         } catch (IOException e) {
             log.error(e.getMessage());

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

@@ -1,19 +0,0 @@
-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;
-}