gutenberg
3 years ago
account-static.js
1 year ago
account.js
1 year ago
amplitude.js
1 year ago
consent-mapping.js
1 year ago
cookiebot-admin-script.js
1 year ago
dashboard.js
1 year ago
jquery.tipTip.js
3 years ago
multiple-page.js
3 years ago
network-settings-page.js
1 year ago
prior-consent-settings.js
3 years ago
settings-page.js
1 year ago
support-page.js
1 year ago
account.js
487 lines
| 1 | const API_BASE_URL = 'https://api.ea.prod.usercentrics.cloud/v1'; |
| 2 | let authToken = ''; |
| 3 | |
| 4 | // Helper function to create FormData for AJAX calls |
| 5 | const createFormData = (action, data = {}) => { |
| 6 | const formData = new FormData(); |
| 7 | formData.append('action', action); |
| 8 | formData.append('nonce', cookiebot_account.nonce); |
| 9 | |
| 10 | Object.entries(data).forEach(([key, value]) => { |
| 11 | formData.append(key, value); |
| 12 | }); |
| 13 | |
| 14 | return formData; |
| 15 | }; |
| 16 | |
| 17 | // Function to check scan status |
| 18 | async function checkScanStatus(scanId) { |
| 19 | try { |
| 20 | const response = await fetch(`${API_BASE_URL}/scan/${scanId}`, { |
| 21 | method: 'GET', |
| 22 | headers: { |
| 23 | 'Accept': 'application/json', |
| 24 | 'Content-Type': 'application/json', |
| 25 | 'Authorization': `Bearer ${authToken}` |
| 26 | } |
| 27 | }); |
| 28 | |
| 29 | const data = await response.json(); |
| 30 | |
| 31 | if (data.status === 'DONE') { |
| 32 | await fetch(cookiebot_account.ajax_url, { |
| 33 | method: 'POST', |
| 34 | body: createFormData('cookiebot_store_scan_details', { |
| 35 | scan_id: scanId, |
| 36 | scan_status: 'DONE' |
| 37 | }), |
| 38 | credentials: 'same-origin' |
| 39 | }); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // Store both scan ID and status |
| 44 | await fetch(cookiebot_account.ajax_url, { |
| 45 | method: 'POST', |
| 46 | body: createFormData('cookiebot_store_scan_details', { |
| 47 | scan_id: scanId, |
| 48 | scan_status: 'IN_PROGRESS' |
| 49 | }), |
| 50 | credentials: 'same-origin' |
| 51 | }); |
| 52 | |
| 53 | if (response.status !== 200 && response.status !== 202 && response.status !== 404 || data.status === 'FAILED') { |
| 54 | await fetch(cookiebot_account.ajax_url, { |
| 55 | method: 'POST', |
| 56 | body: createFormData('cookiebot_store_scan_details', { |
| 57 | scan_id: scanId, |
| 58 | scan_status: 'FAILED' |
| 59 | }), |
| 60 | credentials: 'same-origin' |
| 61 | }); |
| 62 | } |
| 63 | } catch (error) { |
| 64 | console.error('Error checking scan status:', error); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const checkUserData = async () => { |
| 69 | try { |
| 70 | let userData = await fetch(`${API_BASE_URL}/accounts`, { |
| 71 | headers: { |
| 72 | 'Accept': 'application/json', |
| 73 | 'Authorization': `Bearer ${authToken}`, |
| 74 | } |
| 75 | }).then(r => r.json()).then(data => data[0]); |
| 76 | |
| 77 | if (!userData) throw new Error('No user data received'); |
| 78 | |
| 79 | const storedUserData = await fetch(cookiebot_account.ajax_url, { |
| 80 | method: 'POST', |
| 81 | body: createFormData('cookiebot_get_user_data'), |
| 82 | credentials: 'same-origin' |
| 83 | }).then(r => r.json()); |
| 84 | |
| 85 | if (storedUserData.data.subscriptions['active'].subscription_status.includes('trial') && !userData.subscriptions['active'].subscription_status.includes('trial')) { |
| 86 | // window.trackAmplitudeEvent('Pricing Plan Chosen', { |
| 87 | // plan: userData.subscriptions['active'].price_plan, |
| 88 | // subscription_status: userData.subscriptions['active'].subscription_status |
| 89 | // }); |
| 90 | } |
| 91 | |
| 92 | // Store user data |
| 93 | userResponseData = await fetch(cookiebot_account.ajax_url, { |
| 94 | method: 'POST', |
| 95 | body: createFormData('cookiebot_post_user_data', { |
| 96 | data: JSON.stringify(userData) |
| 97 | }), |
| 98 | credentials: 'same-origin' |
| 99 | }).then(r => r.json()); |
| 100 | |
| 101 | if (!userResponseData.success) { |
| 102 | throw new Error('Failed to store user data'); |
| 103 | } |
| 104 | |
| 105 | return userResponseData; |
| 106 | } catch (error) { |
| 107 | console.error('Failed to fetch user data:', error); |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | const isAuthenticated = async () => { |
| 113 | try { |
| 114 | const response = await fetch(`${API_BASE_URL}/accounts`, { |
| 115 | headers: { |
| 116 | 'Accept': 'application/json', |
| 117 | 'Authorization': `Bearer ${authToken}` |
| 118 | } |
| 119 | }); |
| 120 | if (response.status === 401 && cookiebot_account.has_user_data) { |
| 121 | await fetch(cookiebot_account.ajax_url, { |
| 122 | method: 'POST', |
| 123 | body: createFormData('cookiebot_delete_auth_token'), |
| 124 | credentials: 'same-origin' |
| 125 | }); |
| 126 | if (!window.prevent_default && canReload()) { |
| 127 | window.location.reload(); |
| 128 | } |
| 129 | } |
| 130 | return response.status !== 401; |
| 131 | } catch (error) { |
| 132 | console.error('Failed to check authentication:', error); |
| 133 | return false; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | // Function to fetch configuration details |
| 138 | async function fetchConfigurationDetails(configId) { |
| 139 | try { |
| 140 | const response = await fetch(`${API_BASE_URL}/configurations/${configId}`, { |
| 141 | method: 'GET', |
| 142 | headers: { |
| 143 | 'Accept': 'application/json', |
| 144 | 'Content-Type': 'application/json', |
| 145 | 'Authorization': `Bearer ${authToken}` |
| 146 | } |
| 147 | }); |
| 148 | |
| 149 | if (!response.ok) { |
| 150 | throw new Error('Failed to fetch configuration details'); |
| 151 | } |
| 152 | |
| 153 | const data = await response.json(); |
| 154 | |
| 155 | // Store full configuration data |
| 156 | await fetch(cookiebot_account.ajax_url, { |
| 157 | method: 'POST', |
| 158 | body: createFormData('cookiebot_store_configuration', { |
| 159 | configuration: JSON.stringify(data) |
| 160 | }), |
| 161 | credentials: 'same-origin' |
| 162 | }); |
| 163 | |
| 164 | return data; |
| 165 | } catch (error) { |
| 166 | console.error('Error fetching configuration details:', error); |
| 167 | throw error; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | function canReload() { |
| 172 | const itemStr = localStorage.getItem('dashboard_reload'); |
| 173 | const now = new Date(); |
| 174 | const numTimes = 3; |
| 175 | const ttl = 30000; |
| 176 | let count = 0; |
| 177 | |
| 178 | if (itemStr) { |
| 179 | const item = JSON.parse(itemStr); |
| 180 | |
| 181 | if (now.getTime() < item.exp) { |
| 182 | if (item.count > numTimes) { |
| 183 | return false; |
| 184 | } |
| 185 | count = item.count + 1; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | const item = { |
| 190 | count: count, |
| 191 | exp: now.getTime() + ttl, |
| 192 | } |
| 193 | localStorage.setItem('dashboard_reload', JSON.stringify(item)); |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | document.addEventListener('DOMContentLoaded', async () => { |
| 198 | const urlParams = new URLSearchParams(window.location.search); |
| 199 | const ucApiCode = urlParams.get('uc_api_code'); |
| 200 | // if (!cookiebot_account.has_cbid) { |
| 201 | // window.trackAmplitudeEvent('Get Started Page Visited'); |
| 202 | // } |
| 203 | // For existing users, no account registration workflow is needed |
| 204 | if (cookiebot_account.has_cbid && !cookiebot_account.has_user_data && !cookiebot_account.was_onboarded) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | // For previously onboarded users that disconnected the account, no new account registration workflow is needed |
| 209 | if (!cookiebot_account.has_cbid && cookiebot_account.was_onboarded) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | try { |
| 214 | authToken = await fetch(cookiebot_account.ajax_url, { |
| 215 | method: 'POST', |
| 216 | body: createFormData('cookiebot_get_auth_token'), |
| 217 | credentials: 'same-origin' |
| 218 | }).then(r => r.json()).then(data => data.data); |
| 219 | |
| 220 | if (ucApiCode) { |
| 221 | |
| 222 | const isNewUser = urlParams.get('is_new_user'); |
| 223 | |
| 224 | if (cookiebot_account.has_cbid) { |
| 225 | try { |
| 226 | const response = await fetch(cookiebot_account.ajax_url, { |
| 227 | method: 'POST', |
| 228 | body: createFormData('cookiebot_process_auth_code', { code: ucApiCode }), |
| 229 | credentials: 'same-origin' |
| 230 | }); |
| 231 | |
| 232 | authToken = await fetch(cookiebot_account.ajax_url, { |
| 233 | method: 'POST', |
| 234 | body: createFormData('cookiebot_get_auth_token'), |
| 235 | credentials: 'same-origin' |
| 236 | }).then(r => r.json()).then(data => data.data); |
| 237 | |
| 238 | if (!response.ok) throw new Error(`Auth failed: ${response.status}`); |
| 239 | if (canReload()) { |
| 240 | const newUrl = new URL(window.location.href); |
| 241 | newUrl.searchParams.delete('uc_api_code'); |
| 242 | newUrl.searchParams.delete('is_new_user'); |
| 243 | window.location.href = newUrl; |
| 244 | } |
| 245 | return; |
| 246 | } catch (error) { |
| 247 | console.error('Failed to process authentication:', error); |
| 248 | return; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | if (isNewUser === 'false' && !cookiebot_account.has_user_data) { |
| 254 | await fetch(cookiebot_account.ajax_url, { |
| 255 | method: 'POST', |
| 256 | body: createFormData('cookiebot_process_auth_code', { code: ucApiCode }), |
| 257 | credentials: 'same-origin' |
| 258 | }); |
| 259 | |
| 260 | if (canReload()) { |
| 261 | const settingsUrl = window.location.protocol + '//' + window.location.hostname + '/wp-admin/admin.php?page=cookiebot_settings'; |
| 262 | window.location.href = settingsUrl; |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Add loading state |
| 268 | const loadingOverlay = document.createElement('div'); |
| 269 | loadingOverlay.className = 'loading-overlay'; |
| 270 | loadingOverlay.innerHTML = ` |
| 271 | <div class="loading-content"> |
| 272 | <div class="loading-spinner"></div> |
| 273 | <h2>Creating your account</h2> |
| 274 | <p>This should only take about a minute. Keep this window open while we set things up.</p> |
| 275 | </div> |
| 276 | `; |
| 277 | document.body.classList.add('has-loading-overlay'); |
| 278 | document.body.appendChild(loadingOverlay); |
| 279 | |
| 280 | try { |
| 281 | const response = await fetch(cookiebot_account.ajax_url, { |
| 282 | method: 'POST', |
| 283 | body: createFormData('cookiebot_process_auth_code', { code: ucApiCode }), |
| 284 | credentials: 'same-origin' |
| 285 | }); |
| 286 | |
| 287 | authToken = await fetch(cookiebot_account.ajax_url, { |
| 288 | method: 'POST', |
| 289 | body: createFormData('cookiebot_get_auth_token'), |
| 290 | credentials: 'same-origin' |
| 291 | }).then(r => r.json()).then(data => data.data); |
| 292 | |
| 293 | if (!response.ok) throw new Error(`Auth failed: ${response.status}`); |
| 294 | } catch (error) { |
| 295 | console.error('Failed to process authentication:', error); |
| 296 | // Remove loading state on error |
| 297 | document.body.classList.remove('has-loading-overlay'); |
| 298 | loadingOverlay.remove(); |
| 299 | return; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Check for existing CBID |
| 304 | const cbid = await fetch(cookiebot_account.ajax_url, { |
| 305 | method: 'POST', |
| 306 | body: createFormData('cookiebot_get_cbid'), |
| 307 | credentials: 'same-origin' |
| 308 | }).then(r => r.json()).then(data => data.success ? data.data : null); |
| 309 | |
| 310 | // Skip if user data exists and is authenticated |
| 311 | const isAuthenticatedCondition = await isAuthenticated(); |
| 312 | if (cookiebot_account.has_user_data && isAuthenticatedCondition) { |
| 313 | // Check scan status before returning |
| 314 | await checkScanStatus(cbid); |
| 315 | await checkUserData(); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | if (!cbid) { |
| 320 | try { |
| 321 | // Create new configuration |
| 322 | const siteDomain = window.location.hostname; |
| 323 | const formattedDomain = siteDomain.startsWith('http') ? siteDomain : `https://${siteDomain}`; |
| 324 | |
| 325 | const configResponse = await fetch(`${API_BASE_URL}/configurations`, { |
| 326 | method: 'POST', |
| 327 | headers: { |
| 328 | 'Accept': 'application/json', |
| 329 | 'Content-Type': 'application/json', |
| 330 | 'Authorization': `Bearer ${authToken}` |
| 331 | }, |
| 332 | body: JSON.stringify({ |
| 333 | configuration_name: "", |
| 334 | data_controller: "", |
| 335 | legal_framework_template: "gdpr", |
| 336 | domains: [formattedDomain] |
| 337 | }) |
| 338 | }); |
| 339 | |
| 340 | if (!configResponse.ok) throw new Error(`Config creation failed: ${configResponse.status}`); |
| 341 | |
| 342 | const configData = await configResponse.json(); |
| 343 | if (!configData?.configuration_id) throw new Error('No configuration ID'); |
| 344 | |
| 345 | // Store CBID |
| 346 | await fetch(cookiebot_account.ajax_url, { |
| 347 | method: 'POST', |
| 348 | body: createFormData('cookiebot_store_cbid', { cbid: configData.configuration_id }), |
| 349 | credentials: 'same-origin' |
| 350 | }); |
| 351 | |
| 352 | // Now that we have a CBID, initiate the scan |
| 353 | const scanResponse = await fetch(`${API_BASE_URL}/scan`, { |
| 354 | method: 'POST', |
| 355 | headers: { |
| 356 | 'Accept': 'application/json', |
| 357 | 'Content-Type': 'application/json', |
| 358 | 'Authorization': `Bearer ${authToken}` |
| 359 | }, |
| 360 | body: JSON.stringify({ |
| 361 | domains: [formattedDomain], |
| 362 | configuration_id: configData.configuration_id |
| 363 | }) |
| 364 | }); |
| 365 | |
| 366 | const scanData = await scanResponse.json(); |
| 367 | |
| 368 | // Check for scan ID in different possible response structures |
| 369 | const scanId = scanData?.scan?.scan_id || scanData?.scan_id || scanData?.id; |
| 370 | if (!scanId) { |
| 371 | console.error('Scan response structure:', scanData); |
| 372 | throw new Error('No scan ID received in response'); |
| 373 | } |
| 374 | |
| 375 | // Store scan ID in WordPress without status initially |
| 376 | await fetch(cookiebot_account.ajax_url, { |
| 377 | method: 'POST', |
| 378 | body: createFormData('cookiebot_store_scan_details', { |
| 379 | scan_id: scanId |
| 380 | }), |
| 381 | credentials: 'same-origin' |
| 382 | }); |
| 383 | |
| 384 | // Start checking scan status |
| 385 | await checkScanStatus(scanId); |
| 386 | |
| 387 | // fetch configuration details |
| 388 | await fetchConfigurationDetails(configData.configuration_id); |
| 389 | |
| 390 | } catch (error) { |
| 391 | console.error('Failed to create configuration:', error); |
| 392 | return; |
| 393 | } |
| 394 | } else { |
| 395 | // If we already have a CBID, check if there's an ongoing scan |
| 396 | const scanDetails = await fetch(cookiebot_account.ajax_url, { |
| 397 | method: 'POST', |
| 398 | body: createFormData('cookiebot_get_scan_details'), |
| 399 | credentials: 'same-origin' |
| 400 | }).then(r => r.json()); |
| 401 | |
| 402 | if (scanDetails.success && scanDetails.data.scan_id) { |
| 403 | await checkScanStatus(scanDetails.data.scan_id); |
| 404 | } |
| 405 | // And fetch configuration details |
| 406 | await fetchConfigurationDetails(cbid); |
| 407 | await checkUserData(); |
| 408 | } |
| 409 | |
| 410 | // Check and fetch user data if needed |
| 411 | let userResponseData = await fetch(cookiebot_account.ajax_url, { |
| 412 | method: 'POST', |
| 413 | body: createFormData('cookiebot_get_user_data'), |
| 414 | credentials: 'same-origin' |
| 415 | }).then(r => r.json()); |
| 416 | |
| 417 | try { |
| 418 | let userData = await fetch(`${API_BASE_URL}/accounts`, { |
| 419 | headers: { |
| 420 | 'Accept': 'application/json', |
| 421 | 'Authorization': `Bearer ${authToken}`, |
| 422 | } |
| 423 | }).then(r => r.json()).then(data => data[0]); |
| 424 | |
| 425 | // Track account creation in Amplitude |
| 426 | // window.trackAmplitudeEvent('Account Created'); |
| 427 | |
| 428 | // Store user data |
| 429 | userResponseData = await fetch(cookiebot_account.ajax_url, { |
| 430 | method: 'POST', |
| 431 | body: createFormData('cookiebot_post_user_data', { |
| 432 | data: JSON.stringify(userData) |
| 433 | }), |
| 434 | credentials: 'same-origin' |
| 435 | }).then(r => r.json()); |
| 436 | |
| 437 | // Store onboarding status separately |
| 438 | await fetch(cookiebot_account.ajax_url, { |
| 439 | method: 'POST', |
| 440 | body: createFormData('cookiebot_store_onboarding_status', { |
| 441 | onboarded: true |
| 442 | }), |
| 443 | credentials: 'same-origin' |
| 444 | }); |
| 445 | |
| 446 | } catch (error) { |
| 447 | console.error('Failed to fetch user data:', error); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | if (userResponseData.data && canReload()) { |
| 452 | const newUrl = new URL(window.location.href); |
| 453 | newUrl.searchParams.delete('uc_api_code'); |
| 454 | window.location.href = newUrl; |
| 455 | } |
| 456 | } catch (error) { |
| 457 | console.error('An unexpected error occurred:', error); |
| 458 | } |
| 459 | }); |
| 460 | |
| 461 | // Event Listeners |
| 462 | document.getElementById('banner-close-btn')?.addEventListener('click', async () => { |
| 463 | try { |
| 464 | const response = await fetch(cookiebot_account.ajax_url, { |
| 465 | method: 'POST', |
| 466 | body: createFormData('cookiebot_dismiss_banner'), |
| 467 | credentials: 'same-origin' |
| 468 | }); |
| 469 | |
| 470 | if (!response.ok) throw new Error(`Failed to dismiss banner: ${response.status}`); |
| 471 | const banner = document.getElementById('banner-live-notice'); |
| 472 | if (banner) banner.remove(); |
| 473 | } catch (error) { |
| 474 | console.error('Failed to dismiss banner:', error); |
| 475 | } |
| 476 | }); |
| 477 | |
| 478 | document.getElementById('get-started-button')?.addEventListener('click', async (e) => { |
| 479 | // window.trackAmplitudeEvent('Sign Up Flow Started'); |
| 480 | e.preventDefault(); |
| 481 | try { |
| 482 | const callbackUrl = window.location.protocol + '//' + window.location.hostname + '/wp-admin/admin.php?page=cookiebot'; |
| 483 | window.location.href = `${API_BASE_URL}/auth/auth0/authorize?origin=wordpress_plugin&callback_domain=${encodeURIComponent(callbackUrl)}`; |
| 484 | } catch (error) { |
| 485 | console.error('Failed to start authentication process:', error); |
| 486 | } |
| 487 | }); |