|
@@ -1,12 +1,13 @@
|
|
|
package com.wechi.adweb.bridge.google.gtm.service;
|
|
|
|
|
|
+import static com.wechi.adweb.bridge.google.common.ResourceDTO.parseId;
|
|
|
+
|
|
|
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
|
|
import com.google.api.client.json.gson.GsonFactory;
|
|
|
import com.google.api.client.util.Lists;
|
|
|
import com.google.api.services.tagmanager.TagManager;
|
|
|
import com.google.api.services.tagmanager.TagManagerScopes;
|
|
|
-import com.google.api.services.tagmanager.model.Account;
|
|
|
-import com.google.api.services.tagmanager.model.Container;
|
|
|
+import com.google.api.services.tagmanager.model.*;
|
|
|
import com.google.auth.http.HttpCredentialsAdapter;
|
|
|
import com.google.auth.oauth2.GoogleCredentials;
|
|
|
import com.wechi.adweb.bridge.exception.DataException;
|
|
@@ -21,6 +22,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.security.GeneralSecurityException;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
@@ -35,6 +37,9 @@ public class GTMService {
|
|
|
@Value("${google.service.account.key}")
|
|
|
private String serviceAccountKey;
|
|
|
|
|
|
+ @Value("${google.gtm.container.admins}")
|
|
|
+ private List<String> containerAdmins;
|
|
|
+
|
|
|
private TagManager tagManager;
|
|
|
|
|
|
@PostConstruct
|
|
@@ -119,26 +124,80 @@ public class GTMService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // public GTMContainerDTO createGTMContainer(String accountResourceName, String displayName)
|
|
|
- // throws DataException {
|
|
|
- // try {
|
|
|
- // Container container = new Container();
|
|
|
- // container.setName(displayName);
|
|
|
- // container.setUsageContext(List.of("web", "android", "ios"));
|
|
|
- //
|
|
|
- // Container createdContainer =
|
|
|
- // tagManager
|
|
|
- // .accounts()
|
|
|
- // .containers()
|
|
|
- // .create(GTMAccountDTO.toResourceName(accountResourceName),
|
|
|
- // container)
|
|
|
- // .execute();
|
|
|
- //
|
|
|
- // return null;
|
|
|
- //
|
|
|
- // } catch (IOException e) {
|
|
|
- // log.error(e.getMessage());
|
|
|
- // throw new DataException(e);
|
|
|
- // }
|
|
|
- // }
|
|
|
+ public GTMContainerDTO createGTMContainer(String accountResourceName, String displayName)
|
|
|
+ throws DataException {
|
|
|
+ try {
|
|
|
+ Container container = new Container();
|
|
|
+ container.setName(displayName);
|
|
|
+ container.setUsageContext(Collections.singletonList("web"));
|
|
|
+
|
|
|
+ ContainerFeatures containerFeatures = new ContainerFeatures();
|
|
|
+ containerFeatures.setSupportUserPermissions(false);
|
|
|
+ container.setFeatures(containerFeatures);
|
|
|
+
|
|
|
+ Container createdContainer =
|
|
|
+ tagManager
|
|
|
+ .accounts()
|
|
|
+ .containers()
|
|
|
+ .create(GTMAccountDTO.toResourceName(accountResourceName), container)
|
|
|
+ .execute();
|
|
|
+ log.info("Created container {}", JsonUtils.toJson(createdContainer));
|
|
|
+
|
|
|
+ // Converts and returns.
|
|
|
+ return GTMContainerDTO.builder()
|
|
|
+ .id(createdContainer.getContainerId())
|
|
|
+ .resourceName(createdContainer.getPath())
|
|
|
+ .displayName(createdContainer.getName())
|
|
|
+ .accountId(parseId(accountResourceName))
|
|
|
+ .domainNames(createdContainer.getDomainName())
|
|
|
+ .publicId(createdContainer.getPublicId())
|
|
|
+ .tagIds(createdContainer.getTagIds())
|
|
|
+ .notes(createdContainer.getNotes())
|
|
|
+ .usageContext(createdContainer.getUsageContext())
|
|
|
+ .fingerprint(container.getFingerprint())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw new DataException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * See
|
|
|
+ * https://developers.google.com/tag-platform/tag-manager/api/v2/reference/accounts/user_permissions
|
|
|
+ */
|
|
|
+ public void setupContainerAccess(String accountResourceName, String containerId)
|
|
|
+ throws DataException {
|
|
|
+ try {
|
|
|
+ // 1. Container access.
|
|
|
+ ContainerAccess containerAccess = new ContainerAccess();
|
|
|
+ containerAccess.setContainerId(containerId);
|
|
|
+ containerAccess.setPermission("publish");
|
|
|
+
|
|
|
+ // 2. Account access - required in user permission request.
|
|
|
+ AccountAccess accountAccess = new AccountAccess();
|
|
|
+ accountAccess.setPermission("admin");
|
|
|
+
|
|
|
+ // 3. Combines as user permission.
|
|
|
+ UserPermission userPermission = new UserPermission();
|
|
|
+ userPermission.setContainerAccess(Collections.singletonList(containerAccess));
|
|
|
+ userPermission.setAccountAccess(accountAccess);
|
|
|
+
|
|
|
+ for (String containerAdmin : containerAdmins) {
|
|
|
+ // One email address per request.
|
|
|
+ userPermission.setEmailAddress(containerAdmin);
|
|
|
+ UserPermission createdUserPermission =
|
|
|
+ tagManager
|
|
|
+ .accounts()
|
|
|
+ .userPermissions()
|
|
|
+ .create(accountResourceName, userPermission)
|
|
|
+ .execute();
|
|
|
+ log.info("Created user permission {}", JsonUtils.toJson(createdUserPermission));
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw new DataException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|