ソースを参照

Get GTM and GA

wfansh 6 ヶ月 前
コミット
bb23dff30c

+ 27 - 1
src/main/java/com/wechi/adweb/bridge/google/analytics/controller/GAController.java

@@ -81,6 +81,32 @@ public class GAController extends BaseController {
                 .build();
     }
 
+    @PostMapping("/properties/get")
+    @ResponseBody
+    public OpenAPIResponse<GAPropertyDTO> getProperty(
+            @RequestBody OpenAPIRequest<String> apiRequest)
+            throws BadRequestException, DataException {
+        long start = System.currentTimeMillis();
+        log.info("****** getProperty() ****** apiRequest = {}", JsonUtils.toJson(apiRequest));
+        String propertyResourceName = apiRequest.getData();
+
+        // 1. Validates the request parameters.
+        if (StringUtils.isEmpty(propertyResourceName)) {
+            throw new BadRequestException(apiRequest);
+        }
+
+        // 2. Executes the API request.
+        GAPropertyDTO gaProperty =
+                gaAdminService.getProperty(
+                        // Converts to resource name if the property ID is provided by mistake.
+                        GAPropertyDTO.toResourceName(propertyResourceName));
+        log.info("****** getProperty() ****** duration = {} seconds", getElapsedSeconds(start));
+        return OpenAPIResponse.<GAPropertyDTO>builder()
+                .status(APIStatus.SUCCESS)
+                .data(gaProperty)
+                .build();
+    }
+
     @PostMapping("/properties/create")
     @ResponseBody
     public OpenAPIResponse<GAPropertyDTO> createPropertyWithDataStream(
@@ -129,7 +155,7 @@ public class GAController extends BaseController {
 
         // 2. Executes the API request.
         gaAdminService.deleteProperty(
-                // Converts to resource name if the account ID is provided by mistake.
+                // Converts to resource name if the property ID is provided by mistake.
                 GAPropertyDTO.toResourceName(propertyResourceName));
         log.info("****** deleteProperty() ****** duration = {} seconds", getElapsedSeconds(start));
         return OpenAPIResponse.<String>builder()

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

@@ -6,7 +6,6 @@ import com.google.analytics.admin.v1beta.*;
 import com.google.api.client.util.Lists;
 import com.google.api.gax.core.FixedCredentialsProvider;
 import com.google.auth.oauth2.GoogleCredentials;
-import com.google.protobuf.util.JsonFormat;
 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;
@@ -62,7 +61,6 @@ public class GAAdminService {
                 GAAccountDTO gaAccount =
                         GAAccountDTO.builder()
                                 .id(parseId(account.getName()))
-                                .resourceName(account.getName())
                                 .displayName(account.getDisplayName())
                                 .createTime(account.getCreateTime().getSeconds())
                                 .updateTime(account.getUpdateTime().getSeconds())
@@ -177,6 +175,38 @@ public class GAAdminService {
         }
     }
 
+    public GAPropertyDTO getProperty(String propertyResourceName) throws DataException {
+        try (AnalyticsAdminServiceClient analyticsAdminServiceClient =
+                AnalyticsAdminServiceClient.create(adminServiceSettings)) {
+            Property property = analyticsAdminServiceClient.getProperty(propertyResourceName);
+
+            // Converts and returns.
+            GAPropertyDTO gaProperty =
+                    GAPropertyDTO.builder()
+                            .id(parseId(property.getName()))
+                            .resourceName(property.getName())
+                            .displayName(property.getDisplayName())
+                            .propertyType(property.getPropertyType().toString())
+                            .accountResourceName(property.getAccount())
+                            .parentResourceName(property.getParent())
+                            .industryCategory(property.getIndustryCategory().toString())
+                            .timeZone(property.getTimeZone())
+                            .currencyCode(property.getCurrencyCode())
+                            .serviceLevel(property.getServiceLevel().toString())
+                            .createTime(property.getCreateTime().getSeconds())
+                            .updateTime(property.getUpdateTime().getSeconds())
+                            // Sets the data streams associated with this property.
+                            .dataStreams(listDataStreams(property.getName()))
+                            .build();
+
+            log.info("getProperty : {}", JsonUtils.toJson(gaProperty));
+            return gaProperty;
+        } catch (IOException e) {
+            log.error(e.getMessage());
+            throw new DataException(e);
+        }
+    }
+
     public GAPropertyDTO createPropertyWithDataStream(
             String accountResourceName, String displayName, String url) throws DataException {
         try (AnalyticsAdminServiceClient analyticsAdminServiceClient =
@@ -189,7 +219,6 @@ public class GAAdminService {
                             .setTimeZone(DEFAULT_TIMEZONE)
                             .build();
             Property createdProperty = analyticsAdminServiceClient.createProperty(property);
-            log.info("Created property {}", JsonFormat.printer().print(createdProperty));
 
             // 2. Creates the data stream.
             DataStream dataStream =
@@ -202,7 +231,6 @@ public class GAAdminService {
             DataStream createdDataStream =
                     analyticsAdminServiceClient.createDataStream(
                             createdProperty.getName(), dataStream);
-            log.info("Created data stream {}", JsonFormat.printer().print(createdProperty));
 
             // 3. Converts and returns.
             GADataStreamDTO gaDataStream =
@@ -216,21 +244,28 @@ public class GAAdminService {
                             .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();
+            GAPropertyDTO gaProperty =
+                    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();
+
+            log.info(
+                    "createPropertyWithDataStream for {} : {}",
+                    accountResourceName,
+                    JsonUtils.toJson(gaProperty));
+            return gaProperty;
         } catch (IOException e) {
             log.error(e.getMessage());
             throw new DataException(e);
@@ -241,7 +276,7 @@ public class GAAdminService {
         try (AnalyticsAdminServiceClient analyticsAdminServiceClient =
                 AnalyticsAdminServiceClient.create(adminServiceSettings)) {
             analyticsAdminServiceClient.deleteProperty(propertyResourceName);
-            log.info("Deleted property {}", propertyResourceName);
+            log.info("deleteProperty {}", propertyResourceName);
         } catch (IOException e) {
             log.error(e.getMessage());
             throw new DataException(e);

+ 0 - 1
src/main/java/com/wechi/adweb/bridge/google/gtm/service/GTMService.java

@@ -242,7 +242,6 @@ public class GTMService {
                             .filter(w -> w.getName().equalsIgnoreCase("Default Workspace"))
                             .findFirst()
                             .get();
-
             log.info(
                     "Default workspace for {} : {}",
                     containerResourceName,