如何向 google 分析视图添加过滤器?

How to add a filter to a google analytics view?

好的,所以我正在尝试以编程方式将过滤器添加到 google 分析视图。指南 google 提出:

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/filters/insert#examples

本指南向您展示了如何插入过滤器。但此过滤器是在帐户级别创建的,然后您需要将其添加到特定视图。如何将它添加到视图中?

您需要创建一个 profile filter link:

# Note: This code assumes you have an authorized Analytics service object.
# See the Filters Developer Guide for details.

# This request creates a new profile filter link.
try:
  analytics.management().profileFilterLinks().insert(
      accountId='123456',
      webPropertyId='UA-123456-1',
      profileId='7654321',
      body={
          'filterRef': {
              'id': '1223334444'
          }
      }
  ).execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print 'There was an error in constructing your query : %s' % error

except HttpError, error:
  # Handle API errors.
  print ('There was an API error : %s : %s' %
         (error.resp.status, error.resp.reason))