Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
This module provides a minimal Google Analytics 4 integration for Nuxt 3.
gtag.js
.env
file support# pnpm
pnpm add -D nuxt-gtag
# npm
npm i -D nuxt-gtag
Add nuxt-gtag
to the modules
section of your Nuxt configuration and provide your Google Analytics measurement ID.
// `nuxt.config.ts`
export default defineNuxtConfig({
modules: ['nuxt-gtag'],
gtag: {
id: 'G-XXXXXXXXXX'
}
})
Done! Google Analytics will now run in your application's client.
ℹ️ Make sure the
Page changes based on browser history events
checkbox is enabled. This will allow GA4 to automatically track page views. You can find this option in theAdmin
>Property
>Data Streams
>Web
>General Settings
>User-Defined
>Page changes based on browser history events
section.
All supported module options can be set by either the module options key gtag
, or the public runtime config key with the same name.
export default defineNuxtConfig({
modules: ['nuxt-gtag'],
// Configure using the module options
gtag: {
id: 'G-XXXXXXXXXX',
config: {
page_title: 'My Custom Page Title'
}
},
// **Or** the public runtime config instead
runtimeConfig: {
public: {
gtag: {
id: 'G-XXXXXXXXXX',
config: {
page_title: 'My Custom Page Title'
}
}
}
}
})
Alternatively, leveraging automatically replaced public runtime config values by matching environment variables at runtime, set your desired option in your project's .env
file:
# Sets the `gtag` public runtime config value for the key `id`
NUXT_PUBLIC_GTAG_ID=G-XXXXXXXXXX
Option | Type | Description | Default |
---|---|---|---|
id | string | The Google Analytics measurement ID. | undefined |
config | Record<string, any> | The configuration parameters to be passed to gtag.js on initialization. | {} |
As with other composables in the Nuxt 3 ecosystem, they are auto-imported and can be used in your application's components.
useGtag
The useGtag
composable is SSR-safe and can be used to call any of the gtag.js methods.
// SSR-ready
const gtag = useGtag()
gtag(
// <command>,
// <command-parameters>
)
ℹ️ Since the Gtag instance is available in the client only, any
gtag()
(assuming the variable from above) calls executed on the server will have no effect.
Type Declarations
function useGtag(): {
(command: 'config', targetId: string, config?: Record<string, any>): void
(command: 'event', eventName: string & {}, eventParams?: Record<string, any>): void
(command: 'set', targetId: string, config: string | boolean | Record<string, any>): void
(command: 'set', config: Record<string, any>): void
(command: 'get', targetId: string, fieldName: string, callback?: (field?: string | Record<string, any>) => void): void
(command: 'consent', consentArg: string, consentParams: Record<string, any>): void
(command: 'js', config: Date): void
}
Example
The following event command fires the event screen_view
with two parameters: app_name
and screen_name
.
// SSR-ready
const gtag = useGtag()
gtag('event', 'screen_view', {
app_name: 'My App',
screen_name: 'Home'
})
useTrackEvent
ℹ️ Since the Gtag instance is available in the client only, executing the composable on the server will have no effect.
Track your defined goals by passing the following parameters:
Type Declarations
function useTrackEvent(
eventName: (string & {}),
eventParams?: Record<string, any>
): void
Example
For example, the following is an event called login
with a parameter method
:
// Tracks the `login` event
useTrackEvent('login', {
method: 'Google'
})
corepack enable
pnpm install
pnpm run dev:prepare
pnpm run dev
MIT License © 2023-present Johann Schopplich
FAQs
Natively integrates Google Tag into Nuxt
The npm package nuxt-gtag receives a total of 28,823 weekly downloads. As such, nuxt-gtag popularity was classified as popular.
We found that nuxt-gtag demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.