Graphcms Analytics Integrations: Setup Guide | OpsBlu Docs

Graphcms Analytics Integrations: Setup Guide

Integrate GA4, GTM, and Meta Pixel with Hygraph (formerly GraphCMS) in React, Next.js, and other frontends.

Hygraph (formerly GraphCMS) is a GraphQL-native headless CMS. Content is delivered exclusively through a GraphQL API -- there is no server-rendered frontend, no template system, and no built-in analytics. All tracking integration happens in your frontend application (React, Next.js, Gatsby, Nuxt, Svelte, etc.).

Integration Architecture

Hygraph is API-only with GraphQL as the primary query language:

  1. Frontend Framework -- Your React/Next.js/Gatsby application handles all script loading, GTM initialization, and event tracking. Hygraph delivers content; your frontend delivers the experience.
  2. GraphQL Content API -- Query { pages { title, stage, locale, createdBy { name } } } to fetch structured content data for your data layer.
  3. Hygraph Webhooks -- Triggered on content lifecycle events (publish, unpublish, delete). Useful for ISR revalidation and cache purging, not for user-facing analytics.

Available Integrations

Analytics Platforms

Google Analytics 4

  • Frontend gtag.js integration
  • GTM-based GA4 with Hygraph content data layer

Tag Management

Google Tag Manager

  • App shell script injection
  • SPA route-change tracking required

Marketing Pixels

Meta Pixel

  • Via GTM container (recommended)
  • Direct fbq integration in frontend

Next.js + Hygraph Example

Fetch content via GraphQL and build the data layer from the response:

// app/blog/[slug]/page.tsx (Next.js App Router + Hygraph)
import { hygraphClient } from '@/lib/hygraph';
import { gql } from 'graphql-request';

const QUERY = gql`
  query Post($slug: String!) {
    post(where: { slug: $slug }) {
      title
      slug
      stage
      locale
      category { name }
      author { name }
      createdAt
    }
  }
`;

export default async function BlogPost({ params }) {
  const { post } = await hygraphClient.request(QUERY, { slug: params.slug });

  return (
    <>
      <script
        dangerouslySetInnerHTML={{
          __html: `
            window.dataLayer = window.dataLayer || [];
            window.dataLayer.push({
              event: 'virtual_pageview',
              content_type: 'blog_post',
              page_title: ${JSON.stringify(post.title)},
              author: ${JSON.stringify(post.author?.name || '')},
              category: ${JSON.stringify(post.category?.name || '')},
              locale: '${post.locale}',
              stage: '${post.stage}',
              publish_date: '${post.createdAt}'
            });
          `,
        }}
      />
      <article>{/* render post */}</article>
    </>
  );
}

Hygraph Schema for Analytics

Hygraph's GraphQL schema provides structured fields that map directly to analytics dimensions:

Hygraph Field Data Layer Variable Notes
__typename content_type Model name (Post, Page, Product)
stage content_stage PUBLISHED or DRAFT
locale language Content locale (e.g., en, de)
createdBy.name author Content author
Custom relations category Related models (Category, Tag, etc.)
createdAt publish_date ISO 8601 timestamp

Platform Limitations

No platform-level tracking. Hygraph has no built-in analytics, no script injection, no tracking settings. All analytics are implemented in your frontend.

SPA route tracking. Frontends consuming Hygraph content via GraphQL are typically SPAs. GTM's standard page view trigger fires only on initial load. You must push virtual_pageview events on every route change.

Draft content in preview. Hygraph supports content stages (DRAFT, PUBLISHED). Preview environments that query stage: DRAFT may generate analytics events for unpublished content. Use the stage field in your data layer and filter or exclude draft traffic in GA4.

GraphQL query complexity. Deeply nested GraphQL queries (multiple related models, rich text fields) increase API response time. Keep analytics-relevant queries lightweight -- you rarely need the full content body for the data layer.

Localized content. Hygraph supports content localization. Each locale variation has the same slug but different content. Ensure your data layer includes the locale field so analytics can segment by language.

Performance Considerations

  • GraphQL efficiency. Unlike REST APIs that return all fields, GraphQL lets you request only the fields needed for your data layer. This reduces payload size and parse time.
  • SSG/ISR. Use static site generation or incremental static regeneration to pre-render pages with data layer values baked into the HTML. This eliminates runtime GraphQL API calls and ensures the data layer is available before GTM fires.
  • Client-side GraphQL risks. If your frontend makes client-side GraphQL queries (instead of SSG/SSR), each page navigation triggers an API call. This adds latency that compounds with tracking script loading.
  • SDK bundle. Libraries like graphql-request add ~5KB gzipped. Use direct fetch() with GraphQL queries if you want minimal overhead.
  1. Add GTM to frontend app shell -- Single container for all tracking
  2. Implement SPA route tracking -- Virtual pageview events on navigation
  3. Build GraphQL-aware data layer -- Map Hygraph model types, locales, and stages to data layer variables
  4. Configure GA4 in GTM -- Content groups from Hygraph categories, custom dimensions from schema
  5. Add Meta Pixel via GTM -- Map content engagement to Meta events

Next Steps

For general integration concepts, see the integrations overview.