function waitForTrackingFields() {
const params = new URLSearchParams(window.location.search);
const fieldMap = {
'utm_source': params.get('utm_source'),
'utm_medium': params.get('utm_medium'),
'utm_campaign': params.get('utm_campaign'),
'utm_term': params.get('utm_term'),
'adgroup_id': params.get('adgroup_id'),
};
Object.entries(fieldMap).forEach(([name, value]) => {
const field = document.querySelector(`[name='${name}'], [id*='${name}']`);
if (field && value !== null) {
field.value = value;
console.log(`✅ Set ${name}:`, value);
} else {
console.log(`⚠️ Could not find or set field for ${name}`);
}
});
// Retry if not found yet (for dynamic loading)
if (!document.querySelector(`[name='utm_source']`)) {
setTimeout(waitForTrackingFields, 1000);
}
}
window.onload = waitForTrackingFields;