Obtaining variable information (fields to set) from Google Tag Manager through google api client

250 Views Asked by At

Because of the amount of GTM-containers I need to review, I am using python to iterate through all the containers and to get all the tags and variables in a dataframe. Getting the most basic information works, however the most important part is variable information like "fields to set" with information like anonymizeip and forceSSL settings. However, I only get these in a form where I can't figure out how to get these usable in a dataframe.

My best try is in the code example below. It at least gives me the fields to set and their values in rows below each other.

pathlist = 'the path of a certain workspace I use to test'
testdict = []
def testvariable(service):
    for i in pathlist:
        variables = service.accounts().containers().workspaces().variables().list(parent=i).execute()
        for variable in variables.get('variable', []):
            name = variable.get('name')
            accountid = variable.get('accountId')
            containerid = variable.get('containerId')
            for parameter in variable.get('parameter', []):
                key = parameter.get('key')
                value = parameter.get('value')
                lists = parameter.get('list')
                for map in parameter.get('list', []):
                    for l in map.get('map', []):
                        fn = l.get('value', [])
                        testdict.append({'accountId': accountid, 'containerId': containerid, 'variableName': name, 'Key': key, 'Value': value, 'List': fn})
    df = pd.DataFrame(testdict)
    print('Obtained all the variables')
    with pd.ExcelWriter('testvariable.xlsx') as writer:
        df.to_excel(writer, sheet_name='test')
    print('excel created')
    print(df)

I expect the output to get the fields to set from a settings variable to have a column with fields to set, and a column with that value. Right now i've only managed to get them as separate rows.

1

There are 1 best solutions below

0
Joost On

I answered my own question for this case. Because the lists provided deeper in the parameters, I had to loop through them on certain positions. I've added this part to loop through even/uneven rows and got the results I needed:

                for map in parameter.get('list', []):
                for l in map.get('map', [])[::2]:
                    field = l.get('value', [])
                for k in map.get('map', [])[1::2]:
                    set = k.get('value', [])
                testdict.append({'accountId': accountid, 'containerId': containerid, \
                                 'variableName': name, 'Key': key, 'Value': value, 'Field': field, 'Set': set})
        testdict.append({'accountId': accountid, 'containerId': containerid, \
                         'variableName': name, 'Key': key, 'Value': value})