docs
evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
Build a simple client-side analytics loader utility that automatically loads and initializes Google Analytics (GA3/Universal Analytics) tracking scripts.
Your utility should provide:
Automatic Script Loading: Load the Google Analytics script dynamically on the client side without requiring manual script tag insertion in HTML.
Duplicate Script Detection: Before loading the analytics script, check if a Google Analytics script already exists on the page to prevent conflicts. Skip loading if a duplicate is detected.
Initialization Configuration: Accept a configuration object that includes:
Client-Side Only: The utility should only execute in a browser environment and should handle cases where it might be called in a non-browser context gracefully.
Script Loading Completion: Provide a way to know when the script has finished loading and is ready to use.
/**
* Loads Google Analytics script dynamically
* @param {Object} config - Configuration object
* @param {string} config.id - Google Analytics tracking ID (e.g., "UA-12345-1")
* @param {boolean} [config.checkDuplicatedScript=false] - Whether to check for existing GA scripts
* @returns {Promise<void>} Resolves when script is loaded
*/
function loadAnalytics(config) {
// IMPLEMENTATION HERE
}
module.exports = {
loadAnalytics
};Provides Google Analytics integration patterns and script loading utilities.