0

我正在尝试自动化一些 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()

错误:(删除帐户和容器)

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">
4

1 回答 1

1

I got this to work using the below code.

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()
于 2018-05-17T15:30:17.330 回答