> ## Documentation Index
> Fetch the complete documentation index at: https://docs.asva-ai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate Asva Attribution with Google Analytics 4

> Connect the Asva snippet to GA4 using custom dimensions and channel grouping rules so AI Commerce appears as a named channel in your reports.

Out of the box, GA4 has no concept of AI Commerce. Sessions that arrive from ChatGPT, Gemini, or Perplexity land in Direct and disappear into the noise. The Asva attribution snippet automatically enriches each page view with AI source data — but you need to tell GA4 what to do with that data. This guide walks you through creating the custom dimensions, configuring channel grouping, and building a report so AI Commerce becomes a first-class channel in your analytics.

## Before you begin

You need:

* Admin access to your GA4 property (to create custom dimensions and channel groups)
* The Asva snippet installed on your storefront (see [Surface Hidden AI Traffic with Asva Attribution](/attribution/dark-traffic))
* Your Asva Property ID from **Dashboard → Settings → Property**

## Set up GA4 custom dimensions and reporting

<Steps>
  <Step title="Install the Asva snippet">
    If the snippet is not yet on your site, add it to your storefront `<head>`. The snippet automatically enriches `page_view` events with three parameters GA4 needs:

    ```html theme={null}
    <script>
      (function(w,d,s,l,i){w[l]=w[l]||[];
      w[l].push({'asva.start': new Date().getTime(), event:'asva.js'});
      var f=d.getElementsByTagName(s)[0],
      j=d.createElement(s),dl=l!='asvaLayer'?'&l='+l:'';
      j.async=true;j.src='https://cdn.asva-ai.com/attribution.js?id='+i+dl;
      f.parentNode.insertBefore(j,f);
      })(window,document,'script','asvaLayer','YOUR_PROPERTY_ID');
    </script>
    ```

    The snippet sets:

    | Parameter       | Value example                     | What it means                                     |
    | --------------- | --------------------------------- | ------------------------------------------------- |
    | `ai_source`     | `chatgpt`, `gemini`, `perplexity` | Which AI surface referred the session             |
    | `is_ai_session` | `true` / `false`                  | Whether Asva classified the session as AI-sourced |
    | `ai_confidence` | `0.82`                            | Confidence score of the AI classification         |
  </Step>

  <Step title="Create custom dimensions in GA4">
    GA4 does not recognise `ai_source`, `is_ai_session`, or `ai_confidence` automatically. You must register them as custom dimensions.

    Go to **GA4 Admin → Custom definitions → Custom dimensions** and create one dimension for each row below:

    | Dimension name        | Scope   | Event parameter |
    | --------------------- | ------- | --------------- |
    | AI Source             | Session | `ai_source`     |
    | AI Session            | Session | `is_ai_session` |
    | AI Session Confidence | Session | `ai_confidence` |

    For each dimension:

    1. Click **Create custom dimension**
    2. Enter the **Dimension name** from the table above
    3. Set **Scope** to **Session**
    4. Enter the **Event parameter** from the table above
    5. Click **Save**

    <Note>
      Custom dimensions only populate for data collected after you save them. Historical sessions will not be backfilled, so create your dimensions before expecting to see data.
    </Note>
  </Step>

  <Step title="Add an AI Commerce channel group">
    Create a custom channel group so GA4 labels AI-sourced sessions as "AI Commerce" rather than "Direct".

    Go to **GA4 Admin → Channel groups → Create new channel group**, then add the following rule:

    * **Session source/medium** contains `ai`
    * **OR** custom dimension **AI Source** is not empty

    Name the channel **AI Commerce** and save. Set it as your default channel group.

    If you use Looker Studio or query GA4 via BigQuery, use this equivalent CASE expression:

    ```javascript theme={null}
    // GA4 Channel Grouping Rule (Looker Studio / BigQuery)
    CASE
      WHEN session_default_channel_group = 'Direct'
        AND session_manual_utm_medium IN ('ai', 'chatgpt', 'gemini', 'perplexity')
        THEN 'AI Commerce'
      WHEN session_default_channel_group = 'Direct'
        AND ai_source IS NOT NULL
        THEN 'AI Commerce'
      ELSE session_default_channel_group
    END AS channel
    ```
  </Step>

  <Step title="Build an AI Commerce Exploration report">
    Create a custom Exploration report to track AI commerce performance alongside your other channels.

    Go to **Explore → Blank exploration** and configure:

    **Dimensions to add:**

    * Channel group (custom)
    * AI Source
    * Landing page + query string

    **Metrics to add:**

    * Sessions
    * Conversions
    * Conversion rate
    * Total revenue
    * Average order value

    Drag **Channel group** into Rows and all metrics into Values. Add a filter for **Channel group exactly matches AI Commerce** to isolate the channel.
  </Step>

  <Step title="Verify data is flowing">
    Wait 24–48 hours after installing the snippet for session data to populate, then confirm the integration is working.

    **Browser console check** — open your storefront in a browser and run:

    ```javascript theme={null}
    // Test in browser console on your site
    console.log(window.asvaLayer)
    // Should show { ai_source: 'chatgpt', is_ai_session: true, ... }
    // if you arrived from a ChatGPT link
    ```

    **GA4 DebugView check:**

    1. Append `?asva_debug=1` to any page URL on your store
    2. Open **GA4 → DebugView**
    3. Look for `page_view` events with the `ai_source` parameter populated

    **What to look for in GA4 reports:**

    * AI Commerce appears as a channel in your default channel group breakdown
    * The AI Source dimension shows values like `chatgpt`, `gemini`, or `perplexity`
    * Sessions with `is_ai_session = true` are visible in the Exploration report
  </Step>
</Steps>

## Platform-specific setup

<Tabs>
  <Tab title="Shopify">
    Add the snippet via **Online Store → Themes → Edit code → `theme.liquid`**, inside the `<head>` block:

    ```liquid theme={null}
    {% comment %} Asva AI Attribution {% endcomment %}
    <script>
      (function(w,d,s,l,i){w[l]=w[l]||[];
      w[l].push({'asva.start': new Date().getTime(), event:'asva.js'});
      var f=d.getElementsByTagName(s)[0],
      j=d.createElement(s),dl=l!='asvaLayer'?'&l='+l:'';
      j.async=true;j.src='https://cdn.asva-ai.com/attribution.js?id='+i+dl;
      f.parentNode.insertBefore(j,f);
      })(window,document,'script','asvaLayer','{{ settings.asva_property_id }}');
    </script>
    ```

    Add `asva_property_id` as a theme setting in your `settings_schema.json` so you can update the ID without editing code.
  </Tab>

  <Tab title="Google Tag Manager">
    1. Go to **Tags → New → Custom HTML**
    2. Paste the Asva snippet
    3. Set the trigger to **All Pages**
    4. Publish the container

    To pass `ai_source` to GA4 via GTM, create a **JavaScript variable** that reads `window.asvaLayer[window.asvaLayer.length - 1].ai_source` and reference that variable in your GA4 event tag configuration.
  </Tab>

  <Tab title="Headless / custom">
    For headless stores, add the snippet to your global layout component inside the `<head>` element. For server-side rendering, you can also log attribution events server-side via the [Attribution API](/api-reference/attribution) when your checkout endpoint is called — this gives the highest-confidence classification because it is triggered by a confirmed AI-initiated purchase.
  </Tab>
</Tabs>

## Troubleshooting

<AccordionGroup>
  <Accordion title="The AI Source dimension shows no data">
    Custom dimensions only capture data from the moment they are created. If you created the dimensions after the snippet was already live, the data from sessions before creation is not backfilled. Wait 24–48 hours after creating the dimensions to see data appear.
  </Accordion>

  <Accordion title="AI Commerce does not appear in my channel report">
    Confirm the channel group rule is saved and set as default. Channel group changes can take up to 48 hours to appear in standard reports. Use DebugView to verify the parameters are firing correctly in real time.
  </Accordion>

  <Accordion title="window.asvaLayer is undefined in the console">
    The snippet is either not loading or loading after a navigation event fires. Confirm it is placed inside `<head>`, not before `</body>`. Check your browser network tab for a request to `cdn.asva-ai.com/attribution.js` — a 404 or blocked request indicates an incorrect Property ID or a content security policy blocking the script.
  </Accordion>

  <Accordion title="AI Source is always empty even for known AI referrers">
    Some AI surfaces strip referrer headers inconsistently. Sessions classified through behavioral fingerprinting (rather than a detected referrer) will have `is_ai_session = true` but `ai_source` may be empty. Filter on `is_ai_session = true` rather than `ai_source is not empty` to capture all AI Commerce sessions.
  </Accordion>
</AccordionGroup>

## Related

<CardGroup cols={2}>
  <Card title="Surface Hidden AI Traffic with Asva Attribution" icon="eye-slash" href="/attribution/dark-traffic">
    Understand why AI traffic is misattributed and how the Asva snippet works.
  </Card>

  <Card title="How Asva's Attribution Model Works" icon="diagram-project" href="/attribution/model">
    Learn how Asva classifies sessions using signal tiers and confidence scores.
  </Card>
</CardGroup>
