---
title: Google Tag (gtag.js)
description: Learn how to use Google Tag (gtag.js) with c15t.
lastModified: 2025-09-24
canary: true
---
Google Tag (gtag.js) is used to send data to various Google products like Analytics, Ads & Floodlight.
Based on the product you are using you should set the category to `marketing` (Ads & Floodlight) or `measurement` (Analytics).

By default, c15t will always load this script regardless of consent. This is because Google Tag manages its own consent state via Google Consent Mode v2.

c15t will automatically sync the consent state so you don't need to do anything extra.

## Implementation

1. **Adding the script to c15t** JavaScript

   ```ts
   import { configureConsentManager } from 'c15t';
   import { gtag } from '@c15t/scripts/google-tag';

   configureConsentManager({
     // ... 
     scripts: [
       gtag({
         id: 'G-XXXXXXXXXX',
         category: 'marketing',
       }),
     ],
   });
   ```

   React

   ```tsx
   import { gtag } from '@c15t/scripts/google-tag';
   import { ConsentManagerProvider } from '@c15t/react';

   export function App({ children }: { children: React.ReactNode }) { 
     return (
       <ConsentManagerProvider
         options={{
           scripts: [
             gtag({
               id: 'G-XXXXXXXXXX',
             }),
           ],
         }}
       >
         {children}
       </ConsentManagerProvider>
     );
   }
   ```

   Next.js + App Router

   ```tsx
   'use client';
   import { gtag } from '@c15t/scripts/google-tag';
   import { ClientSideOptionsProvider } from '@c15t/nextjs/client';

   export function ConsentManagerClient({ children }: { children: React.ReactNode }) { 
     return (
       <ClientSideOptionsProvider
         scripts={[
           gtag({
             id: 'G-XXXXXXXXXX',
           }),
         ]}
       >
         {children}
       </ClientSideOptionsProvider>
     );
   }
   ```

   Next.js + Pages Router

   ```tsx
   import { gtag } from '@c15t/scripts/google-tag';
   import { ConsentManagerProvider } from '@c15t/nextjs';

   export function App() { 
     return (
       <ConsentManagerProvider
         options={{
           // ... other options
           scripts: [
             gtag({
               id: 'G-XXXXXXXXXX',
               category: 'measurement',
             }),
           ],
         }}
       >
         {children}
       </ConsentManagerProvider>
     );
   }
   ```

## Types

### GtagOptions

| Property | Type                                | Description                                                                                                                                                                                                          | Default |  Required  |
| :------- | :---------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------ | :--------: |
| id       | string                              | Your gtag id                                                                                                                                                                                                         | -       | ✅ Required |
| category | AllConsentNames                     | The consent category to use for the gtag script. This is typically marketing (Ads & Floodlight) or measurement (Analytics)                                                                                           | -       | ✅ Required |
| script   | Partial\<Omit\<Script, "category">> | Override or extend the default script values.&#xA;&#xA;Default values:&#xA;- \`id\`: 'gtag'&#xA;- \`src\`: \`https\://www\.googletagmanager.com/gtag/js?id=$\{id}\`&#xA;- \`async\`: true&#xA;- \`alwaysLoad\`: true | -       |  Optional  |

### Script

| Property                   | Type                               | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | Default |  Required  |
| :------------------------- | :--------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------ | :--------: |
| id                         | string                             | Unique identifier for the script                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | -       | ✅ Required |
| src                        | string                             | URL of the script to load                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | -       |  Optional  |
| textContent                | string                             | Inline JavaScript code to execute                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | -       |  Optional  |
| category                   | HasCondition\<AllConsentNames>     | Consent category or condition required to load this script                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | -       | ✅ Required |
| callbackOnly               | boolean                            | Whether this is a callback-only script that doesn't need to load an external resource.&#xA;When true, no script tag will be added to the DOM, only callbacks will be executed.&#xA;&#xA;This is useful for:&#xA;- Managing consent for libraries already loaded on the page&#xA;- Enabling/disabling tracking features based on consent changes&#xA;- Running custom code when consent status changes without loading external scripts&#xA;&#xA;Example use cases:&#xA;- Enabling/disabling Posthog tracking&#xA;- Configuring Google Analytics consent mode&#xA;- Managing cookie consent for embedded content | false   |  Optional  |
| persistAfterConsentRevoked | boolean                            | Whether the script should persist after consent is revoked.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | false   |  Optional  |
| alwaysLoad                 | boolean                            | Whether the script should always load regardless of consent state.&#xA;&#xA;This is useful for scripts like Google Tag Manager or PostHog that manage&#xA;their own consent state internally. The script will load immediately and&#xA;never be unloaded based on consent changes.&#xA;&#xA;Note: When using this option, you are responsible for ensuring the script&#xA;itself respects user consent preferences through its own consent management.                                                                                                                                                          | false   |  Optional  |
| fetchPriority              | "high" \| "low" \| "auto"          | Priority hint for browser resource loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | -       |  Optional  |
| attributes                 | Record\<string, string>            | Additional attributes to add to the script element                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | -       |  Optional  |
| async                      | boolean                            | Whether to use async loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | -       |  Optional  |
| defer                      | boolean                            | Whether to defer script loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | -       |  Optional  |
| nonce                      | string                             | Content Security Policy nonce                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| anonymizeId                | boolean                            | Whether to use an anonymized ID for the script element, this helps ensure the script is not blocked by ad blockers                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | true    |  Optional  |
| onBeforeLoad               | (info: ScriptCallbackInfo) => void | Callback executed before the script is loaded                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| onLoad                     | (info: ScriptCallbackInfo) => void | Callback executed when the script loads successfully                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | -       |  Optional  |
| onDelete                   | (info: ScriptCallbackInfo) => void | Callback executed when the script is being unloaded/removed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | -       |  Optional  |
| onError                    | (info: ScriptCallbackInfo) => void | Callback executed if the script fails to load                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| onConsentChange            | (info: ScriptCallbackInfo) => void | Callback executed whenever the consent store is changed.&#xA;This callback only applies to scripts already loaded.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | -       |  Optional  |
