Google跟踪代码管理器 - Python - 创建自定义事件触发器

时间:2018-05-17 01:01:38

标签: google-tag-manager google-apis-explorer google-client google-python-api

我正在尝试自动化一些GTM任务。下面的代码创建了一个"所有页面" Google Analytics的触发功能。

def CreateGATrigger(service, workspace):


 """Create the GA Trigger.

  Args:
    service: the Tag Manager service object.
    workspace: the workspace to create the trigger within.

  Returns:
    The created trigger.
  """

  GA_trigger = {
      'name': 'All Pages',
      'type': 'PAGEVIEW'
  }

  return service.accounts().containers().workspaces().triggers().create(
      parent=workspace['path'],
      body=GA_trigger).execute()

但是,下面的代码给出了以下错误。下面代码的目标是创建一个自定义事件触发器,当事件变量包含" formSubmit"时触发该事件触发器。

def CreateformSubmitTrigger(service, workspace):

  formSubmit_trigger = {
  "name": 'formSubmit',
  "type": 'customEvent',
  'customEventFilter': [
    {
      'parameter': [
        {
          'type': 'template',
          'key': 'arg0',
          'value': '{{event}}'
        },
        {
          'type': 'template',
          'key': 'arg1',
          'value': 'formSubmit'
        }
      ],
      'type': 'contains'
    }
  ]
}

  return service.accounts().containers().workspaces().triggers().create(
      parent=workspace['path'],
      body=formSubmit_trigger).execute()

错误:(X'出帐户和容器)

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/tagmanager/v2/accounts/XXXXXXXXXX/containers/XXXXXXX/workspaces/1/triggers?alt=json returned "Bad Request">

1 个答案:

答案 0 :(得分:0)

我使用下面的代码让它工作。

def CreateformSubmitTrigger(service, workspace):

  formSubmit_trigger =   {
   "name": "formSubmit",
   "type": "customEvent",
   "customEventFilter": [
    {
     "type": "equals",
     "parameter": [
      {
       "type": "template",
       "key": "arg0",
       "value": "{{_event}}"
      },
      {
       "type": "template",
       "key": "arg1",
       "value": "formSubmit"
      }
     ]
    }
   ],
   "filter": [
    {
     "type": "contains",
     "parameter": [
      {
       "type": "template",
       "key": "arg0",
       "value": "{{Event}}"
      },
      {
       "type": "template",
       "key": "arg1",
       "value": "formSubmit"
      }
     ]
    }
   ],
  }

  return service.accounts().containers().workspaces().triggers().create(
      parent=workspace['path'],
      body=formSubmit_trigger).execute()