\n```\n\n## If you want to support decline\nIf the user declines, record that choice too:\n\n```js\nlocalStorage.setItem('analytics_consent', 'denied');\n```\n\nThen skip loading analytics entirely.\n\n## If you need a tag manager\nFor Google Tag Manager / similar tools:\n\n- **Do not load GTM container until consent**\n- Or use the platform’s consent mode if you want limited pre-consent behavior\n\n## Important notes\n\n- **Blocking scripts is not the same as blocking all tracking**\n - Analytics may also use cookies, pixels, or network calls.\n- **Check legal/compliance requirements**\n - Depending on region, you may need explicit opt-in before any non-essential tracking.\n- **Remember consent on every page**\n - Use `localStorage`, a first-party cookie, or your consent platform.\n\n## Best practice\nUse a consent management platform or a small consent wrapper that:\n- defaults analytics to off\n- loads scripts only after opt-in\n- allows revocation of consent\n- prevents tracking before consent\n\nIf you want, I can also show you:\n- a **vanilla JS implementation**\n- a **React/Next.js version**\n- a **Google Tag Manager consent mode setup**\n- a **Cookiebot/OneTrust-style pattern**"}}}

Prompt

How do I block analytics scripts until a user opts in?

Technology / Marketing Automation1 observationLast seen Oct 29, 2026

Latest observation

Oct 29, 2026OpenAI APIWeb search: off

To block analytics scripts until a user opts in, the main idea is:

  1. Don’t load the analytics script by default
  2. Store the user’s consent choice
  3. Only inject/execute the script after opt-in
  4. Persist the choice for future visits
  5. Optionally update page state for consent management systems

Common approach

1) Default to blocked

Do not include your analytics <script src="..."> directly in the HTML if it would run immediately.

Instead, keep it in a non-executing form, or load it only after consent.

2) Show a consent banner

Ask the user whether they accept analytics cookies/scripts.

3) On opt-in, dynamically load the script

Example:

<button id="accept-analytics">Accept analytics</button>

<script>
  function loadAnalytics() {
    const s = document.createElement('script');
    s.src = 'https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX';
    s.async = true;
    document.head.appendChild(s);

    window.dataLayer = window.dataLayer || [];
    function gtag(){ dataLayer.push(arguments); }
    window.gtag = gtag;

    gtag('js', new Date());
    gtag('config', 'G-XXXXXXX');
  }

  document.getElementById('accept-analytics').addEventListener('click', () => {
    localStorage.setItem('analytics_consent', 'granted');
    loadAnalytics();
  });

  if (localStorage.getItem('analytics_consent') === 'granted') {
    loadAnalytics();
  }
</script>

If you want to support decline

If the user declines, record that choice too:

localStorage.setItem('analytics_consent', 'denied');

Then skip loading analytics entirely.

If you need a tag manager

For Google Tag Manager / similar tools:

  • Do not load GTM container until consent
  • Or use the platform’s consent mode if you want limited pre-consent behavior

Important notes

  • Blocking scripts is not the same as blocking all tracking
    • Analytics may also use cookies, pixels, or network calls.
  • Check legal/compliance requirements
    • Depending on region, you may need explicit opt-in before any non-essential tracking.
  • Remember consent on every page
    • Use localStorage, a first-party cookie, or your consent platform.

Best practice

Use a consent management platform or a small consent wrapper that:

  • defaults analytics to off
  • loads scripts only after opt-in
  • allows revocation of consent
  • prevents tracking before consent

If you want, I can also show you:

  • a vanilla JS implementation
  • a React/Next.js version
  • a Google Tag Manager consent mode setup
  • a Cookiebot/OneTrust-style pattern

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.