Inject Google Analytics in GitHub Actions


This post shows how to inject Google Analytics tracking (gtag.js) into an HTML file with GitHub Actions.

Problem

You want to add Google Analytics to a generated HTML file in CI without touching the source code.

Solution

Use the google-analytics-action to inject the gtag.js script in your GitHub Action workflow.

Usage

Add the action after your build step:

# .github/workflows/build.yml
name: Build
on:
  push:
    branches: [master]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v7

      - name: Build static site
        run: npm run build

      - name: Inject Google Analytics
        uses: remarkablemark/google-analytics-action@v1
        with:
          html-path: dist/index.html
          measurement-id: ${{ secrets.GOOGLE_ANALYTICS_ID }}

Inputs

  • html-path (required): The HTML file to modify.
  • measurement-id (required): The Google Analytics Measurement ID (e.g., G-XXXXXXXXXX).

Behavior

The action:

  • Injects the gtag.js script before the closing </head> tag.
  • Falls back to inserting before </body> if </head> is not present.
  • Skips injection if the snippet for the same measurement ID is already present (idempotent).
  • Fails if the file does not exist or if neither </head> nor </body> is found.

Example

Before:

<!doctype html>
<html>
  <head>
    <title>My Site</title>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>

After:

<!doctype html>
<html>
  <head>
    <title>My Site</title>
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'G-XXXXXXXXXX');
    </script>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>

See the action source code.



Please support this site and join our Discord!