網站開發人員可以使用自訂維度和指標,區隔和評估已登入和未登入的使用者、頁面作者或遊戲關卡間的差異。本頁面說明如何將自訂維度和指標傳送至 Google Analytics (分析)。
如要將自訂參數的值傳送至 Google Analytics (分析),請將這些參數對應至下列 Google Analytics (分析) 自訂維度或指標參數:
自訂參數 | 資料類型 | 說明 |
---|---|---|
dimension<Index> |
string |
自訂維度參數 (例如 dimension3 ) |
metric<Index> |
string |
自訂指標參數 (例如 metric8 ) |
設定及傳送自訂維度
如要將自訂參數的值傳送到 Google Analytics (分析),您必須將網站上的值對應至 Google Analytics (分析) 參數。您可以使用 custom_map
參數完成這項操作。
如要將自訂維度傳送至 Google Analytics (分析),請更新資源的 config
以設定維度的 custom_map
參數,然後使用自訂參數傳送自訂維度的值:
// Configures custom dimension<Index> to use the custom parameter // 'dimension_name' for 'GA_MEASUREMENT_ID', where <Index> is a number // representing the index of the custom dimension. gtag('config', 'GA_MEASUREMENT_ID', { 'custom_map': {'dimension<Index>': 'dimension_name'} }); // Sends the custom dimension to Google Analytics. gtag('event', 'any_event_name', {'dimension_name': dimension_value});
請將 'GA_MEASUREMENT_ID'
替換成自己的 Analytics (分析) ID。
舉例來說,下列程式碼片段會將值為 55
的自訂維度 dimension2
傳送至 Google Analytics (分析):
// Maps 'dimension2' to 'age'. gtag('config', 'GA_MEASUREMENT_ID', { 'custom_map': {'dimension2': 'age'} }); // Sends an event that passes 'age' as a parameter. gtag('event', 'age_dimension', {'age': 55});
設定及傳送自訂指標
如要將自訂指標傳送至 Google Analytics (分析),請更新資源的 config
以設定指標的 custom_map
參數,然後使用自訂參數傳送自訂指標的值:
// Configures custom metric<Index> to use the custom parameter // 'metric_name' for GA_MEASUREMENT_ID, where <Index> is a number // representing the index of the custom metric. gtag('config', 'GA_MEASUREMENT_ID', { 'custom_map': {'metric<Index>': 'metric_name'} }); // Sends the custom dimension to Google Analytics. gtag('event', 'any_event_name', {'metric_name': metric_value});
請將 'GA_MEASUREMENT_ID'
替換成自己的 Analytics (分析) ID。
舉例來說,下列程式碼片段會將值為 1
的自訂指標 metric5
傳送至 Google Analytics (分析):
// Maps 'metric5' to 'avg_page_load_time'. gtag('config', 'GA_MEASUREMENT_ID', { 'custom_map': {'metric5': 'avg_page_load_time'} }); // Sends an event that passes 'avg_page_load_time' as a parameter. gtag('event', 'load_time_metric', {'avg_page_load_time': 1});
設定及傳送自訂維度和自訂指標
您可以更新資源的 config
,以對應自訂維度和自訂指標:
gtag('config', 'GA_MEASUREMENT_ID', { 'custom_map': { 'dimension2': 'age', 'metric5': 'avg_page_load_time' } }); gtag('event', 'foo', {'age': 55, 'avg_page_load_time': 1});