篩選器

如果報表包含篩選器,且社群連接器針對所有要求的欄位傳回未經篩選的資料,Looker Studio 就會將篩選器套用至連接器回應。不過,您可以在社群連接器層級套用篩選器,在某些情況下可以大幅改善效能。篩選器資訊會透過 getData() 要求物件傳遞,且連接器可在將資料傳回 Looker Studio 前篩選資料。

舉例來說,如果您連線至 SQL 資料庫,可以直接在 WHERE 子句 (下圖中的 B3) 套用篩選器,傳回 Looker Studio 的資料列數量可能會大幅減少。如此一來,就能限制需處理並傳送至 Looker Studio (B5) 的資料量。

篩選流程圖

篩選器套用規則

  1. 套用「所有」篩選條件,或「無」篩選條件。請參閱「不支援的篩選器
  2. 請勿在回應中加入 forFilterOnly 欄位。
  3. request.dimensionsFilters 陣列中的每個項目一起 AND

    舉例來說,在下列篩選器中,連接器只能包含 countryUSA 的值,「且」Socialsource

    {
      "dimensionsFilters": [
        [{
          "fieldName": "country",
          "values": ["USA"],
          "type": "INCLUDE",
          "operator": "EQUALS"
        }],
        [{
          "fieldName": "source",
          "values": ["Social"],
          "type": "INCLUDE",
          "operator": "EQUALS"
        }]
      ]
    }
    
  4. request.dimensionsFilters 陣列中每個子陣列的 OR 一起。

    舉例來說,在下列篩選器中,連接器只能包含 countryUSA 的值, countryCanada 的值。

    {
      "dimensionsFilters": [
        [{
          "fieldName": "country",
          "values": ["Canada"],
          "type": "INCLUDE",
          "operator": "EQUALS"
        }, {
          "fieldName": "country",
          "values": ["USA"],
          "type": "INCLUDE",
          "operator": "EQUALS"
        }]
      ]
    }
    

範例

以下範例說明報表使用者定義篩選器的端對端流程,到社群連接器會傳回經過篩選的資料。

範例篩選器

  1. 報表使用者設定了兩個篩選器:

    1. country為第 IN_LIST 張,共 Canada, USA
    2. source為第 IN_LIST 張,共 Social, Organic
  2. 報表使用者已設定包含 source 維度和 sessions 指標的圖表元件

  3. Looker Studio 會透過下列要求物件執行 getData()

    {
      "fields": [
        {"name": "source"},
        {"name": "sessions"},
        {"name": "country", "forFilterOnly": true}
      ],
      "dimensionsFilters": [
        [{
          "fieldName": "country",
          "values": ["Canada", "USA"],
          "type": "INCLUDE",
          "operator": "IN_LIST"
        }],
        [{
          "fieldName": "source",
          "values": ["Social", "Organic"],
          "type": "INCLUDE",
          "operator": "IN_LIST"
        }]
      ]
    }
    
  4. 連接器以經過篩選的資料回應。

    對於範例要求,傳回 sourcesessions,其中 country"Canada""USA" source"Social""Organic"。由於所有篩選器都能夠成功套用,因此請將 filtersApplied 設為 true

原始資料

來源 工作階段 country
社群媒體 60 美國
社群媒體 50 加拿大
社群媒體 40 英國
自然 90 美國
自然 80 加拿大
自然 70 英國
報紙 30 美國
報紙 20 加拿大
報紙 10 英國

經過篩選的資料

來源 工作階段
社群媒體 60
社群媒體 50
自然 90
自然 80

getData() 則回應

{
  "schema": [
    {"name": "source",   "dataType": "STRING"},
    {"name": "sessions", "dataType": "NUMBER"},
  ],
  "rows": [
    {"values": ["Social", 60]},
    {"values": ["Social", 50]},
    {"values": ["Organic", 90]},
    {"values": ["Organic", 80]}
  ],
  "filtersApplied": true
}

不支援的篩選器

如果連接器無法套用要求中的所有篩選器,便不應執行任何篩選。傳回所有要求的欄位 (包括 forFilterOnly 欄位),並在回應中將 filtersApplied 鍵設為 false

示例:

{
  "schema": [
    {"name": "source",   "dataType": "STRING"},
    {"name": "sessions", "dataType": "NUMBER"},
    {"name": "country",  "dataType": "STRING"}
  ],
  "rows": [
    {"values": ["Social", 60, "USA"]},
    {"values": ["Social", 50, "Canada"]},
    {"values": ["Social", 40, "UK"]},
    {"values": ["Organic", 90, "USA"]},
    {"values": ["Organic", 80, "Canada"]},
    {"values": ["Organic", 70, "UK"]},
    {"values": ["Newspaper", 30, "USA"]},
    {"values": ["Newspaper", 20, "Canada"]},
    {"values": ["Newspaper", 10, "UK"]},
  ],
  "filtersApplied": false
}