Skip to content

googleAnalytics

The googleAnalytics registry script. It loads https://www.googletagmanager.com/gtag/js?id=G-ABCDEF1234 for the input of the example. Call useScriptGoogleAnalytics(input, options?) from @hamur/scripts/<library> in a component, or list it under scripts.registry.googleAnalytics to load it on every page.

Config keyscripts.registry.googleAnalytics
FunctionuseScriptGoogleAnalytics of @hamur/scripts/<library>
Input keysid (required), l
Default triggerclient
Scripthttps://www.googletagmanager.com/gtag/js?id=G-ABCDEF1234
Hostswww.googletagmanager.com, www.google-analytics.com, region1.google-analytics.com
Bundleyes: scripts.bundle: ["googleAnalytics"]
Collection proxyyes: /_hamur/scripts/p/ga; flags on by default: ip, userAgent, language, screen, timezone, hardware
Consenttold by useScriptTriggerConsent: Google Consent Mode v2

Example

ts
import { defineConfig } from "hamur";
import scripts from "@hamur/scripts";

export default defineConfig({
  framework: "react",
  modules: [scripts],
  scripts: {
    registry: {
      googleAnalytics: { id: "G-ABCDEF1234" },
    },
  },
});

It loads after the app mounted and the first idle callback. List googleAnalytics in scripts.bundle and the build serves the file from your origin with a SHA-384 integrity (see Bundling). With the script bundled, its collection requests go to /_hamur/scripts/p/ga on your origin, which forwards them to https://www.google-analytics.com with the privacy flags applied; see The privacy proxy.

Every key is checked when the config loads and again in useScriptGoogleAnalytics: a wrong value stops the build with a message that starts with scripts.registry.googleAnalytics. The registry list is on the registry page, and the shared options are in Scripts.