Last active
August 25, 2025 22:18
-
-
Save robisatthefunction/4937a7963d48c5549edc7c49eb522ede to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Inject Optimizely script immediately | |
| (function() { | |
| 'use strict'; | |
| console.log('π Optimizely injection script starting...'); | |
| console.log('π Current URL:', window.location.href); | |
| console.log('π Document ready state:', document.readyState); | |
| // Function to inject the Optimizely script | |
| function injectOptimizelyScript() { | |
| console.log('π§ Starting Optimizely script injection process...'); | |
| // Replace this script with your script | |
| const optimizelyUrl = 'https://cdn.optimizely.com/js/29420580055.js'; | |
| // Check if Optimizely script is already loaded | |
| const existingScript = document.querySelector(`script[src="${optimizelyUrl}"]`); | |
| if (existingScript) { | |
| console.warn('β οΈ Optimizely script already exists on this page - skipping injection'); | |
| console.log('π Existing script element:', existingScript); | |
| return; | |
| } | |
| // Check if Optimizely object exists globally | |
| if (window.optimizely) { | |
| console.warn('β οΈ Optimizely object already exists globally - skipping injection'); | |
| console.log('π Existing optimizely object:', window.optimizely); | |
| return; | |
| } | |
| console.log('β No existing Optimizely found - proceeding with injection'); | |
| // Create the script element | |
| console.log('π Creating script element...'); | |
| var script = document.createElement('script'); | |
| script.src = optimizelyUrl; | |
| script.type = 'text/javascript'; | |
| script.async = false; // Optimizely should load synchronously for proper A/B testing | |
| console.log('π Script element configured:'); | |
| console.log(' - Source:', script.src); | |
| console.log(' - Type:', script.type); | |
| console.log(' - Async:', script.async); | |
| // Add load event listener | |
| script.onload = function() { | |
| console.log('π Optimizely script loaded successfully!'); | |
| console.log('π Optimizely object now available:', !!window.optimizely); | |
| if (window.optimizely) { | |
| console.log('π Optimizely object details:', window.optimizely); | |
| } | |
| }; | |
| // Add error event listener | |
| script.onerror = function() { | |
| console.error('β Failed to load Optimizely script'); | |
| console.log('π Failed URL:', optimizelyUrl); | |
| }; | |
| // Find injection target (head is preferred for Optimizely) | |
| var target = document.head || document.getElementsByTagName('head')[0] || document.body; | |
| console.log('π― Injection target found:', target ? target.tagName : 'NONE'); | |
| if (target) { | |
| console.log('π Injecting script into', target.tagName, 'element...'); | |
| // For Optimizely, inject at the beginning of head for earliest execution | |
| if (target === document.head || target.tagName === 'HEAD') { | |
| if (target.firstChild) { | |
| target.insertBefore(script, target.firstChild); | |
| console.log('π Script injected at beginning of HEAD'); | |
| } else { | |
| target.appendChild(script); | |
| console.log('π Script appended to empty HEAD'); | |
| } | |
| } else { | |
| target.appendChild(script); | |
| console.log('π Script appended to', target.tagName); | |
| } | |
| console.log('π Optimizely script element successfully injected!'); | |
| // Monitor for script loading | |
| setTimeout(function() { | |
| console.log('π Post-injection check (100ms):'); | |
| console.log(' - Script in DOM:', !!document.querySelector(`script[src="${optimizelyUrl}"]`)); | |
| console.log(' - Optimizely object exists:', !!window.optimizely); | |
| }, 100); | |
| setTimeout(function() { | |
| console.log('π Extended check (1000ms):'); | |
| console.log(' - Script loaded:', script.readyState !== 'loading'); | |
| console.log(' - Optimizely object exists:', !!window.optimizely); | |
| if (window.optimizely) { | |
| console.log(' - Optimizely ready:', typeof window.optimizely !== 'undefined'); | |
| } | |
| }, 1000); | |
| } else { | |
| console.error('β Could not find head or body element to inject script'); | |
| console.log('ποΈ Document structure:', { | |
| head: !!document.head, | |
| body: !!document.body, | |
| documentElement: !!document.documentElement, | |
| headElements: document.getElementsByTagName('head').length, | |
| bodyElements: document.getElementsByTagName('body').length | |
| }); | |
| } | |
| } | |
| // Run injection immediately | |
| console.log('β‘ Running injection immediately...'); | |
| injectOptimizelyScript(); | |
| console.log('π Injection script completed'); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment