Google Analytics 4 with DatoCMS | OpsBlu Docs

Google Analytics 4 with DatoCMS

Overview of integrating Google Analytics 4 with DatoCMS headless CMS for comprehensive content and user tracking.

Integrate Google Analytics 4 with your DatoCMS-powered website to track content performance, user behavior, and conversions. Since DatoCMS is a headless CMS, GA4 is implemented in your frontend framework.

Why Track DatoCMS Sites with GA4

Content Performance:

  • Track which DatoCMS content performs best
  • Measure engagement with modular content blocks
  • Analyze GraphQL query patterns via page views
  • Monitor content updates impact on traffic

User Behavior:

  • Understand content discovery paths
  • Track multi-locale content preferences
  • Measure time on structured text content
  • Analyze navigation through DatoCMS taxonomies

Technical Insights:

  • Monitor Imgix image optimization impact on performance
  • Track ISR revalidation effects
  • Measure GraphQL API response times
  • Analyze CDN cache hit rates

DatoCMS-Specific Tracking Opportunities

1. Content Model Tracking

Track different DatoCMS content models:

// Track by model type
gtag('event', 'view_content', {
  content_type: record._modelApiKey, // 'blog_post', 'product', etc.
  content_id: record.id,
  content_title: record.title
});

2. Modular Content Blocks

Track engagement with DatoCMS modular content:

// Track structured text blocks
post.content.blocks.forEach((block, index) => {
  gtag('event', 'content_block_view', {
    block_type: block._modelApiKey,
    block_id: block.id,
    block_position: index,
    parent_content_id: post.id
  });
});

3. Multi-locale Content

Track language preferences and locale switching:

gtag('event', 'locale_view', {
  content_id: record.id,
  content_locale: currentLocale,
  available_locales: record._allLocales?.join(','),
  locale_count: record._allLocales?.length
});

4. Image Performance

Track Imgix-optimized image loading:

// Track responsive image usage
const { responsiveImage } = post.coverImage;

gtag('event', 'optimized_image_load', {
  content_id: post.id,
  image_format: responsiveImage.webpSrcSet ? 'webp' : 'original',
  image_width: responsiveImage.width,
  uses_srcset: !!responsiveImage.srcSet,
  uses_blur_up: !!responsiveImage.base64
});

5. Preview Mode Activity

Exclude or separately track preview sessions:

const isPreview = searchParams.preview === 'true';

if (isPreview) {
  // Track preview sessions separately
  gtag('event', 'preview_mode_view', {
    content_id: post.id,
    preview_session: true
  });
} else {
  // Regular analytics
  gtag('event', 'page_view');
}

Implementation Approaches

DatoCMS works excellently with Next.js:

  • App Router with Server Components
  • Pages Router with SSG/ISR
  • Image optimization with next/image
  • TypeScript support

View Next.js Setup Guide →

Gatsby

Static site generation with DatoCMS:

  • gatsby-source-datocms plugin
  • Build-time GraphQL queries
  • gatsby-plugin-google-gtag
  • Optimized image loading

View Gatsby Setup Guide →

Nuxt.js

Vue framework with DatoCMS:

  • Nuxt 3 support
  • SSR and SSG modes
  • Vue Composition API
  • @nuxtjs/google-analytics

View Nuxt Setup Guide →

Other Frameworks

DatoCMS works with any framework that can consume GraphQL:

  • Remix
  • SvelteKit
  • Astro
  • Vanilla React/Vue

Key Features

Automatic Page Tracking

Track all DatoCMS content pages:

// Track page views with DatoCMS metadata
gtag('event', 'page_view', {
  page_title: post.title,
  page_location: window.location.href,
  content_id: post.id,
  content_type: post._modelApiKey,
  published_at: post._publishedAt
});

Custom Events

Track DatoCMS-specific interactions:

// Content sharing
gtag('event', 'share', {
  method: 'twitter',
  content_type: post._modelApiKey,
  content_id: post.id
});

// Form submissions via DatoCMS forms
gtag('event', 'generate_lead', {
  form_name: formBlock.formName,
  content_id: post.id
});

Enhanced Ecommerce

For DatoCMS-powered ecommerce:

// Product view from DatoCMS
gtag('event', 'view_item', {
  currency: 'USD',
  value: product.price,
  items: [{
    item_id: product.id,
    item_name: product.title,
    item_category: product.category?.title,
    price: product.price
  }]
});

Data Layer Strategy

Structure your data layer for DatoCMS content:

window.dataLayer = window.dataLayer || [];

// Push DatoCMS content metadata
dataLayer.push({
  event: 'datocms_content_loaded',
  contentId: record.id,
  contentType: record._modelApiKey,
  contentLocale: currentLocale,
  publishedAt: record._publishedAt,
  updatedAt: record._updatedAt,
  hasStructuredText: !!record.content,
  blockCount: record.content?.blocks?.length || 0
});

Privacy and Compliance

GDPR Considerations

Implement consent mode with DatoCMS:

// Default consent state
gtag('consent', 'default', {
  'analytics_storage': 'denied',
  'ad_storage': 'denied'
});

// Update after user consent
function handleCookieConsent(analyticsConsent) {
  gtag('consent', 'update', {
    'analytics_storage': analyticsConsent ? 'granted' : 'denied'
  });
}

Use Google Analytics 4's enhanced measurement without cookies where needed.

Common Use Cases

Blog/Magazine Sites

  • Track article performance by author, category, tags
  • Measure reading time on structured text content
  • Monitor content recommendation clicks
  • Analyze social share patterns

E-commerce Sites

Documentation Sites

  • Track helpful/not helpful feedback
  • Monitor search queries
  • Measure page depth
  • Analyze navigation patterns

Next Steps

  1. Set up GA4 with DatoCMS - Installation guide
  2. Configure Event Tracking - Custom events
  3. Troubleshoot Issues - Debug tracking problems