|
8 | 8 | <script async src="../prebid.js"></script> |
9 | 9 | <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> |
10 | 10 | <script> |
11 | | - console.log('Initializing deferred ${IDENTITY_NAME} + Prebid example.'); |
12 | | - |
13 | | - const identityName = '${IDENTITY_NAME}'; // 'UID2' or 'EUID' |
| 11 | + const identityName = '${IDENTITY_NAME}'; |
14 | 12 | const apiBaseKey = identityName === 'EUID' ? 'euidApiBase' : 'uid2ApiBase'; |
15 | | - const userIdName = identityName.toLowerCase(); // 'uid2' or 'euid' |
| 13 | + const userIdName = identityName.toLowerCase(); |
16 | 14 | const storageKey = '${UID_STORAGE_KEY}'; |
17 | 15 |
|
18 | | - // Track the current state |
19 | 16 | let uidConfigured = false; |
20 | 17 |
|
21 | 18 | function updateGuiElements() { |
22 | | - console.log('Updating displayed values.'); |
23 | | - |
24 | | - // Try to get identity from Prebid |
25 | 19 | let identity = pbjs.getUserIds ? pbjs.getUserIds()[userIdName] : null; |
26 | 20 | let advertisingToken = identity ? identity.id : null; |
27 | 21 |
|
28 | | - // Check localStorage for token data |
29 | 22 | let isOptedOut = false; |
30 | 23 | const storedToken = localStorage.getItem(storageKey); |
31 | 24 | if (storedToken) { |
|
35 | 28 | if (!advertisingToken && tokenData.latestToken && tokenData.latestToken.advertising_token) { |
36 | 29 | advertisingToken = tokenData.latestToken.advertising_token; |
37 | 30 | } |
38 | | - } catch (e) { |
39 | | - console.log('Could not parse stored token'); |
40 | | - } |
| 31 | + } catch (e) {} |
41 | 32 | } |
42 | 33 |
|
43 | 34 | const hasValidToken = !!advertisingToken; |
44 | 35 |
|
45 | | - // Update token status |
46 | 36 | $('#targeted_advertising_ready').text(hasValidToken ? 'yes' : 'no'); |
47 | 37 | $('#login_required').text(hasValidToken ? 'no' : 'yes'); |
48 | 38 | $('#has_opted_out').text(isOptedOut ? 'yes' : 'no'); |
|
70 | 60 | } |
71 | 61 | } |
72 | 62 |
|
73 | | - // Update UI visibility |
74 | 63 | updateFormVisibility(hasValidToken, isOptedOut); |
75 | 64 | } |
76 | 65 |
|
|
79 | 68 | $('#login_form').hide(); |
80 | 69 | $('#clear_storage_form').show(); |
81 | 70 | } else if (uidConfigured) { |
82 | | - // UID is configured but no token yet - might be loading |
83 | 71 | $('#login_form').hide(); |
84 | 72 | $('#clear_storage_form').show(); |
85 | 73 | } else { |
|
88 | 76 | } |
89 | 77 | } |
90 | 78 |
|
91 | | - // This is called when user clicks "Configure UID with mergeConfig()" |
92 | 79 | async function handleDeferredLogin() { |
93 | 80 | const email = $('#email').val(); |
94 | 81 | if (!email) { |
95 | 82 | alert('Please enter an email address'); |
96 | 83 | return; |
97 | 84 | } |
98 | 85 |
|
99 | | - console.log('=== DEFERRED LOGIN: Using mergeConfig() to add UID configuration ==='); |
100 | | - |
101 | | - // Build the UID config to merge |
| 86 | + // Build UID config to merge - this adds UID AFTER page load |
102 | 87 | const uidConfig = { |
103 | 88 | userSync: { |
104 | 89 | userIds: [ |
|
115 | 100 | }, |
116 | 101 | }; |
117 | 102 |
|
118 | | - // Add consent management for EUID |
119 | 103 | if (identityName === 'EUID') { |
120 | 104 | uidConfig.consentManagement = consentManagementObj; |
121 | 105 | } |
122 | 106 |
|
123 | | - // Show what we're merging |
124 | | - console.log('Calling pbjs.mergeConfig() with:', JSON.stringify(uidConfig, null, 2)); |
125 | 107 | $('#merged_config').text(JSON.stringify(uidConfig, null, 2)); |
126 | 108 | $('#merge_config_display').show(); |
127 | 109 |
|
128 | | - // *** KEY STEP 1: Use mergeConfig() to add UID configuration *** |
| 110 | + // Use mergeConfig() to add UID configuration after page load |
129 | 111 | pbjs.mergeConfig(uidConfig); |
130 | 112 |
|
131 | | - // *** KEY STEP 2: Call refreshUserIds() to trigger token generation *** |
132 | | - console.log('Calling pbjs.refreshUserIds() to generate token...'); |
| 113 | + // Call refreshUserIds() to trigger token generation |
133 | 114 | await pbjs.refreshUserIds(); |
134 | 115 |
|
135 | 116 | uidConfigured = true; |
136 | 117 |
|
137 | | - // Wait a moment for token to be generated |
138 | 118 | setTimeout(() => { |
139 | 119 | updateGuiElements(); |
140 | 120 | }, 2000); |
141 | 121 | } |
142 | 122 |
|
143 | 123 | function handleClearStorage() { |
144 | | - console.log('Clearing storage and resetting state.'); |
145 | 124 | localStorage.removeItem(storageKey); |
146 | 125 | uidConfigured = false; |
147 | 126 | location.reload(); |
148 | 127 | } |
149 | 128 |
|
150 | 129 | function onDocumentReady() { |
151 | | - console.log('Setting up interface handlers.'); |
152 | 130 | $('#configure_uid').click(handleDeferredLogin); |
153 | 131 | $('#clear_storage').click(handleClearStorage); |
154 | 132 |
|
155 | | - // Display initial config immediately |
156 | 133 | displayInitialConfig(); |
157 | 134 |
|
158 | 135 | pbjs.que.push(function() { |
|
161 | 138 | }); |
162 | 139 | } |
163 | 140 |
|
164 | | - // Initial Prebid config - WITHOUT UID |
| 141 | + // Initial Prebid config - WITHOUT UID (deferred pattern) |
165 | 142 | const initialPrebidConfig = { |
166 | 143 | debug: true, |
167 | 144 | userSync: { |
|
170 | 147 | }, |
171 | 148 | }; |
172 | 149 |
|
173 | | - // Display the initial config (called on document ready) |
174 | 150 | function displayInitialConfig() { |
175 | 151 | $('#initial_config').text(JSON.stringify(initialPrebidConfig, null, 2)); |
176 | 152 | } |
177 | 153 |
|
178 | | - // Apply the initial Prebid config |
179 | 154 | function setInitialConfig() { |
180 | 155 | pbjs.setConfig(initialPrebidConfig); |
181 | 156 | } |
|
226 | 201 | var pbjs = pbjs || {}; |
227 | 202 | pbjs.que = pbjs.que || []; |
228 | 203 | pbjs.que.push(function () { |
229 | | - // Set initial config WITHOUT UID - this is the deferred pattern! |
| 204 | + // Initial config WITHOUT UID - UID is added later via mergeConfig() |
230 | 205 | setInitialConfig(); |
231 | 206 | pbjs.addAdUnits(adUnits); |
232 | 207 | pbjs.requestBids(); |
|
0 commit comments