Technologies and Software Engineering

Integrating Silktide and Google Analytics with Astro Starlight

Overview

Integrate Silktide Consent Manager and Google Analytics into an Astro Starlight project by configuring the astro.config.mjs file to inject necessary scripts and stylesheets into the document head.

Silktide Consent Manager is a lightweight cookie consent solution that helps websites comply with privacy regulations like GDPR by managing user consent for analytics and advertising scripts.

Key Insights

Technical Details

To embed Silktide Consent Manager and Google Analytics, modify your astro.config.mjs file. This configuration injects the required <link> and <script> tags directly into the <head> of your Starlight application.

astro.config.mjs Configuration

Update your astro.config.mjs file with the following code snippet. Ensure you replace placeholders for asset paths, Silktide manager code, and Google Analytics IDs.

import { defineConfig } from 'astro/config';
import starlight from '@astrojs/starlight';

export default defineConfig({
  site: 'https://YOUR-USER.github.io', // Update with your site URL
  base: '/YOUR-FOLDER', // Update with your base path if applicable
  build: {
    assets: 'app_assets', // Define your asset build directory
  },
  integrations: [
    starlight({
      head: [
        // Silktide Consent Manager Stylesheet
        {
          tag: 'link',
          attrs: {
            rel: 'stylesheet',
            id: 'silktide-consent-manager-css',
            href: 'cookie-banner/silktide-consent-manager.css', // **Update path as needed**
          },
        },
        // Silktide Consent Manager Script
        {
          tag: 'script',
          attrs: {
            src: 'cookie-banner/silktide-consent-manager.js', // **Update path as needed**
          },
        },
        // Silktide Cookie Banner Manager Initialization
        {
          tag: 'script',
          content: `// Place your SilktideCookieBannerManager initialization code here` // **Add your Silktide code**
        },
        // Google Analytics Global Site Tag (gtag.js)
        {
          tag: 'script',
          attrs: {
            src: 'https://www.googletagmanager.com/gtag/js?id=YOUR-ID', // **Replace YOUR-ID**
          },
        },
        // Google Analytics Initialization
        {
          tag: 'script',
          content: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'YOUR-ID'); // **Replace YOUR-ID**
`
        }
      ],
    }),
  ],
});

Customization Steps

Tags:

Search