Package: gtm-manager
GTM Manager (WIP)An object-oriented helper library wrapping theTag Manager API Client Library for Pythonfor theGoogle Tag Manager API.pip3installgtm_managerDocumentation:https://gtm-manager.readthedocs.ioInstallation from Sourcepython3setup.pysdist
pip3installgtm_manager--find-links$(pwd)/dist/Getting StartedUse a Google account to create application credentials, download the JSON file and put it in the same directory as your script with the nameclient_secret.json. During the first execution of any API-dependent library code, you will be prompted to perform the auth flow.fromgtm_managerimportGTMManageraccounts=GTMManager().list_accounts()foraccountinaccount:print(account.name)AuthenticationThis library currently only supports user-based oauth crendentials. Service accounts can not be used.When using any of the classes from the resources that require loading data from the Google Tag Manager API, the library will look for an existing in OAuth token in the credentials file or prompt the user to authorize which requires a client secret file.Client SecretsThe Google OAuth flows requires you to provide aclient id and secretin the from of aJSONfile. You can create these in any Google Cloud or Google Developer project:Open the Google API Console Credentials page.From the project drop-down, choose Create a new project, enter a name for the project, and optionally, edit the provided Project ID. Click Create.On the Credentials page, select Create credentials, then select OAuth client ID.You may be prompted to set a product name on the Consent screen; if so, click Configure consent screen, supply the requested information, and click Save to return to the Credentials screen.Select Other for the Application type, and enter any additional information required.Click Create.On the page that appears, you can download client id and secret as a JSON file.By default, thegtm_managerwill look for client id and secret in the fileclient_secret.json. You can overwrite the default to any file name with absolute or relative path:gtm_manager.CLIENT_SECRET_FILE="my_client_secret.json"Credentials FileOAuth user credentials will be stored in plain text. During every invocation of API-dependent library code, thegtm_managerwill try to open this file for credentials. If no credentials are present, a new auth flow with the provided client id and secret will be started.The auth flow will requestofflineaccess from the user providing the script with an oauth refresh token to continue using the file as long as the user not explicitly revokes access.By default, thegtm_managerwill look for existing and store new credentials in the fileauth_credentials.json. You can overwrite the default to any file name with absolute or relative path:gtm_manager.CREDENTIALS_FILE_NAME="my_auth_credentials.json"ScopesThe Google OAuth system uses scopes to request different types of account access from a user. The Google Tag Manager API uses seven different scopes. Different API methods requires different scopes. Details can be found in theGoogle Tag Manager API Reference. After initial authorisation, the stored credentials can not extend or chang their scope.By default, thegtm_managerwill request all seven scopes from a user during the auth flow. For ease of use, all scopes are defined as constants undergtm_manager.GoogleTagManagerScopes.You can overwritte the requested scopes to an array of scope strings:gtm_manager.AUTH_SCOPES=[gtm_manager.GoogleTagManagerScopes.EDIT_CONTAINERS,gtm_manager.GoogleTagManagerScopes.PUBLISH,]Auth Best PracticeEven though the default settings get you up and running quickly, it is recommended to alwaysexplicitlyset your settings. Everygtm_managerscript should therefore contain the following code block:# explicitly set the location of your client secret filegtm_manager.CLIENT_SECRET_FILE="my_client_secret.json"# explicitly set the location of your credentials filegtm_manager.CREDENTIALS_FILE_NAME="my_auth_credentials.json"# explicitly set the required scopesgtm_manager.AUTH_SCOPES=[gtm_manager.GoogleTagManagerScopes.EDIT_CONTAINERS,gtm_manager.GoogleTagManagerScopes.PUBLISH,]Usage ExamplesThegtm_managerspeeds up interaction working with the Google Tag Manager API for various use cases. Some of these use cases are demoed below. If you are missing a use case, please open an issue and we will be happy to help and extend the demo!List all GTM containers in an account# import gtm_manager# from gtm_manager.manager import GTMManager# gtm_manager.CLIENT_SECRET_FILE = "my_client_secret.json"# gtm_manager.CREDENTIALS_FILE_NAME = "my_auth_credentials.json"# gtm_manager.AUTH_SCOPES = [gtm_manager.GoogleTagManagerScopes.READONLY]accounts=GTMManager().list_accounts()foraccountinaccounts:print(account.name)Add a new tag, trigger, variable to all containers in an account# import gtm_manager# from gtm_manager.account import GTMAccount# gtm_manager.CLIENT_SECRET_FILE = "my_client_secret.json"# gtm_manager.CREDENTIALS_FILE_NAME = "my_auth_credentials.json"# gtm_manager.AUTH_SCOPES = [#     gtm_manager.GoogleTagManagerScopes.EDIT_CONTAINERS,#     gtm_manager.GoogleTagManagerScopes.PUBLISH,#     gtm_manager.GoogleTagManagerScopes.EDIT_CONTAINERVERSIONS,# ]account=GTMAccount(path="accounts/1234")containers=account.list_containers()forcontainerincontainers:workspace=container.create_workspace("Global Update Workspace")trigger=workspace.create_trigger({"name":"Custom - Generic Event","type":"CUSTOM_EVENT","customEventFilter":[{"type":"EQUALS","parameter":[{"type":"TEMPLATE","key":"arg0","value":"{{_event}}"},{"type":"TEMPLATE","key":"arg1","value":"Generic Event"},],}],})variable=workspace.create_variable({"name":"cjs.randomNumber","type":"jsm","parameter":[{"type":"TEMPLATE","key":"javascript","value":"function() {\nreturn Math.random();\n}",}],})workspace.create_tag({"name":"HTML - Hello Log","type":"html","parameter":[{"type":"TEMPLATE","key":"html","value":'<script>\nconsole.log("Hello World")\n</script>',}],"firingTriggerId":[trigger.triggerId],})version=workspace.create_version("Global Update Workspace")version.publish()Update a Variable on a Tag# import gtm_manager# from gtm_manager.account import GTMAccount# gtm_manager.CLIENT_SECRET_FILE = "my_client_secret.json"# gtm_manager.CREDENTIALS_FILE_NAME = "my_auth_credentials.json"# gtm_manager.AUTH_SCOPES = [#     gtm_manager.GoogleTagManagerScopes.EDIT_CONTAINERS,#     gtm_manager.GoogleTagManagerScopes.EDIT_CONTAINERVERSIONS,#     gtm_manager.GoogleTagManagerScopes.PUBLISH,# ]account=GTMAccount(path="accounts/1985550951")containers=account.list_containers()forcontainerincontainers:workspace=container.create_workspace("Global Update Workspace")tag=workspace.get_tag_by_name("UA - Pageview")doubleClick_param=tag.parameter_dict["doubleClick"]doubleClick_param.value="false"tag.update(parameter=[doubleClick_param])version=workspace.create_version("Global Update Workspace")version.publish()