|
11 | 11 | } |
12 | 12 |
|
13 | 13 | const SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY = "SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY"; |
| 14 | + const SAVED_CUSTOM_USER_ID_KEY = "SAVED_CUSTOM_USER_ID"; |
14 | 15 | let gaReady = false, loadedGoogleAnalyticsID; |
| 16 | + let savedCustomUserID = localStorage.getItem(SAVED_CUSTOM_USER_ID_KEY); |
15 | 17 |
|
16 | 18 | const url = window.location.href; |
17 | 19 | const urlParams = new URLSearchParams(window.location.search); |
|
90 | 92 | document.head.appendChild(gaScript); |
91 | 93 |
|
92 | 94 | gtag('js', new Date()); |
93 | | - gtag('config', analyticsID); |
| 95 | + const configParams = {}; |
| 96 | + if (savedCustomUserID) { |
| 97 | + configParams.client_id = savedCustomUserID; |
| 98 | + } |
| 99 | + gtag('config', analyticsID, configParams); |
94 | 100 | gaReady = true; |
95 | 101 | } |
96 | 102 |
|
|
109 | 115 |
|
110 | 116 | async function processRequest(event) { |
111 | 117 | const payload = event.payload; |
| 118 | + if (payload.customUserID && payload.customUserID !== savedCustomUserID) { |
| 119 | + savedCustomUserID = payload.customUserID; |
| 120 | + localStorage.setItem(SAVED_CUSTOM_USER_ID_KEY, payload.customUserID); |
| 121 | + if (gaReady) { |
| 122 | + gtag('config', loadedGoogleAnalyticsID, { |
| 123 | + client_id: savedCustomUserID, |
| 124 | + send_page_view: false |
| 125 | + }); |
| 126 | + } |
| 127 | + } |
112 | 128 | if(payload.analyticsID && loadedGoogleAnalyticsID !== payload.analyticsID) { |
113 | 129 | localStorage.setItem(SAVED_GOOGLE_ANALYTICS_DESKTOP_KEY, payload.analyticsID); |
114 | 130 | installGoogleAnalytics(); |
|
0 commit comments