---
title: GA4 + Google Ads (gtag.js)
description: Send data to Google Analytics 4 and Google Ads with automatic Consent Mode v2 support.
lastModified: 2025-09-24

icon: google-analytics
---
Google Tag (`gtag.js`) is Google's unified tracking script for sending data to Google Analytics 4 (GA4), Google Ads, and Floodlight. It measures user behavior, tracks conversions, and powers Google's advertising ecosystem.

c15t initializes Google Tag with Consent Mode v2 defaults set to denied and automatically updates the consent state when users make choices. You don't need to configure Google Consent Mode yourself.

**Choosing the right category:**

* Use `category: 'measurement'` for analytics-only tracking (GA4 events)
* Use `category: 'marketing'` for advertising and conversion tracking (Google Ads)

### Adding GA4 + Google Ads to c15t

**JavaScript**

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

configureConsentManager({
  scripts: [
    gtag({
      id: 'G-XXXXXXXXXX',
      category: 'measurement', // or '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={{
        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">> \| undefined | 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 \| undefined                               | URL of the script to load                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | -       |  Optional  |
| textContent                | string \| undefined                               | Inline JavaScript code to execute                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | -       |  Optional  |
| category                   | HasCondition\<AllConsentNames>                    | Consent category or condition required to load this script                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | -       | ✅ Required |
| callbackOnly               | boolean \| undefined                              | 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 \| undefined                              | Whether the script should persist after consent is revoked.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | false   |  Optional  |
| alwaysLoad                 | boolean \| undefined                              | 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" \| undefined            | Priority hint for browser resource loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | -       |  Optional  |
| attributes                 | Record\<string, string> \| undefined              | Additional attributes to add to the script element                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | -       |  Optional  |
| async                      | boolean \| undefined                              | Whether to use async loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | -       |  Optional  |
| defer                      | boolean \| undefined                              | Whether to defer script loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | -       |  Optional  |
| nonce                      | string \| undefined                               | Content Security Policy nonce                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| anonymizeId                | boolean \| undefined                              | Whether to use an anonymized ID for the script element, this helps ensure the script is not blocked by ad blockers                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | true    |  Optional  |
| target                     | "head" \| "body" \| undefined                     | Where to inject the script element in the DOM.&#xA;- \`'head'\`: Scripts are appended to \`\<head>\` (default)&#xA;- \`'body'\`: Scripts are appended to \`\<body>\`&#xA;&#xA;Use \`'body'\` for scripts that:&#xA;- Need to manipulate DOM elements that don't exist until body loads&#xA;- Should load after page content for performance reasons&#xA;- Are required by third-party services to be in the body&#xA;&#xA;Use \`'head'\` (default) for scripts that:&#xA;- Need to track early page events (analytics)&#xA;- Should be available before page render&#xA;- Most tracking/analytics scripts       | 'head'  |  Optional  |
| onBeforeLoad               | ((info: ScriptCallbackInfo) => void) \| undefined | Callback executed before the script is loaded                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| onLoad                     | ((info: ScriptCallbackInfo) => void) \| undefined | Callback executed when the script loads successfully                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | -       |  Optional  |
| onDelete                   | ((info: ScriptCallbackInfo) => void) \| undefined | Callback executed when the script is being unloaded/removed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | -       |  Optional  |
| onError                    | ((info: ScriptCallbackInfo) => void) \| undefined | Callback executed if the script fails to load                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| onConsentChange            | ((info: ScriptCallbackInfo) => void) \| undefined | Callback executed whenever the consent store is changed.&#xA;This callback only applies to scripts already loaded.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | -       |  Optional  |
