Created
August 17, 2017 20:02
-
-
Save kyle-mccarthy/eeb4565646450fb5702c342820ea7227 to your computer and use it in GitHub Desktop.
Py class for configuring GTM for container based on JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import sys | |
import httplib2 | |
import re | |
import json | |
from googleapiclient.discovery import build | |
from oauth2client import client | |
from oauth2client import file | |
from oauth2client import tools | |
class Gtm: | |
def __init__(self): | |
self.api_name = 'tagmanager' | |
self.api_version = 'v1' | |
self.scope = ['https://www.googleapis.com/auth/tagmanager.edit.containers'] | |
self.client_secrets_path = 'client_secrets.json' | |
self.service = None | |
self.accountId = '' | |
self.init_service() | |
self.validate_accounts = None | |
def init_service(self): | |
flow = client.flow_from_clientsecrets( | |
self.client_secrets_path, scope=self.scope, message=tools.message_if_missing(self.client_secrets_path)) | |
parser = argparse.ArgumentParser( | |
formatter_class=argparse.RawDescriptionHelpFormatter, | |
parents=[tools.argparser]) | |
flags = parser.parse_args([]) | |
storage = file.Storage(self.api_name + '.json') | |
credentials = storage.get() | |
if credentials is None or credentials.invalid: | |
credentials = tools.run_flow(flow, storage, flags) | |
http = credentials.authorize(http=httplib2.Http()) | |
self.service = build(self.api_name, self.api_version, http=http) | |
def set_accounts_to_validate(self, accounts): | |
self.validate_accounts = accounts | |
def get_accounts(self): | |
return self.service.accounts().list().execute() | |
def get_containers(self, account_id): | |
return self.service.accounts().containers().list(accountId=account_id).execute() | |
def get_tags(self, account_id, container_id): | |
return self.service.accounts()\ | |
.containers().tags().list(accountId=account_id, containerId=container_id).execute() | |
def get_variables(self, account_id, container_id): | |
return self.service.accounts()\ | |
.containers().variables().list(accountId=account_id, containerId=container_id).execute() | |
def get_triggers(self, account_id, container_id): | |
return self.service.accounts()\ | |
.containers().triggers().list(accountId=account_id, containerId=container_id).execute() | |
def validate_containers(self): | |
with open('containers.json') as f: | |
containers = json.load(f) | |
for container in containers['containers']: | |
self.validate_container(container) | |
def validate_container(self, container): | |
variables = self.get_variables(container['account_id'], container['container_id']) | |
tags = self.get_tags(container['account_id'], container['container_id']) | |
events = self.get_triggers(container['account_id'], container['container_id']) | |
hash_v = dict() | |
hash_t = dict() | |
hash_e = dict() | |
for v in variables['variables']: | |
hash_v[v['name']] = v | |
for e in events['triggers']: | |
hash_e[e['name']] = e | |
for t in tags['tags']: | |
hash_t[t['name']] = t | |
template = self.get_template(container['template']) | |
# check for the existence of the variables | |
for v in template['variables']: | |
if v['name'] not in hash_v: | |
self.service.accounts().containers().variables().create( | |
accountId=container['account_id'], | |
containerId=container['container_id'], | |
body=self.get_variable(container, v) | |
).execute() | |
print('Variable added: act[' + container['account_id'] + '] containerId[' + | |
container['container_id'] + '] ' + v['name']) | |
# check for the existence of the triggers | |
for e in template['triggers']: | |
if e['name'] not in hash_e: | |
hash_e[e['name']] = self.service.accounts().containers().triggers().create( | |
accountId=container['account_id'], | |
containerId=container['container_id'], body=e | |
).execute() | |
print('Trigger added: act[' + container['account_id'] + '] containerId[' + | |
container['container_id'] + '] ' + e['name']) | |
# map the triggers name to the ids for the tags | |
for t in template['tags']: | |
if 'firingTrackerName' in t: | |
t['firingTriggerId'] = list(map(lambda e: int(hash_e[e]['triggerId']), t['firingTrackerName'])) | |
# check for the existence of the tags | |
for t in template['tags']: | |
if t['name'] not in hash_t: | |
self.service.accounts().containers().tags().create( | |
accountId=container['account_id'], | |
containerId=container['container_id'], | |
body=t | |
).execute() | |
print('Tag added: act[' + container['account_id'] + '] containerId[' + | |
container['container_id'] + '] ' + t['name']) | |
@staticmethod | |
def get_variable(container, v): | |
if 'variables' in container and v['name'] in container['variables']: | |
v['parameter'][0]['value'] = container['variables'][v['name']] | |
return v | |
@staticmethod | |
def get_template(vendor): | |
with open('templates.json') as f: | |
templates = json.load(f) | |
return templates[vendor] | |
def main(argv): | |
gtm = Gtm() | |
gtm.validate_containers() | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment