nb-gtm
This is a Google Tag Manager plugin for Vue.js 3+.
Installation
Yarn
yarn add @vlalg-nimbus/nb-gtm
Usage
Vue 3
import { createApp } from 'vue'
import App from './App.vue'
import gtmPlugin from '@vlalg-nimbus/nb-gtm'
// Your css files
import './style/style.css';
const app = createApp(App)
app.use(gtmPlugin, {
googleKey: "GTM-XXXXXXXX", // Google Key
permitedDomains: [], // List of allowed domains (If you have domain limitation)
dev: true, // Console triggers
})
app.mount('#app')
The only required option when creating is googleKey, the options (dev and permitedDomains) are optional.
To use it, simply call the sets in your files using Vue inject.
Below I provide the names of the sets.
component
<template>
<button @click="triggerGtm">Button A</button>
</template>
<script setup lang="ts">
import { inject } from 'vue'
const gtagpush = inject('$gtagpush')
function triggerGtm () => {
gtagpush({ category: 'test', label: 'button A' }, 'click')
}
</script>
Typescript
The lib provides automatic typing
Vue
main.js
import { createApp } from 'vue'
import App from './App.vue'
import gtmPlugin from '@vlalg-nimbus/nb-gtm'
import type { IOptions } from '@vlalg-nimbus/nb-gtm'
const app = createApp(App)
const gtmOptions: IOptions = {
googleKey: 'GTM-XXXXXXXX',
permitedDomains: ['http://localhost:3000'],
dev: true,
}
app.use(gtmPlugin, gtmOptions)
app.mount('#app')
Nuxt
main.js
import { createApp } from 'vue'
import App from './App.vue'
import gtmPlugin from '@vlalg-nimbus/nb-gtm'
import type { IOptions } from '@vlalg-nimbus/nb-gtm'
const app = createApp(App)
const gtmOptions: IOptions = {
googleKey: 'GTM-XXXXXXXX',
permitedDomains: ['http://localhost:3000'],
dev: true,
}
app.use(gtmPlugin, gtmOptions)
app.mount('#app')
Preview & Playground
This plugin cannot provide a Preview & Playground because we would have to leave a GA key exposed.
Options
Items with an (*) mean they are required
| name | Value type | Default | Description |
|---|---|---|---|
| googleKey (*) | String | Sets default Google Key | |
| dev | Boolean | false | Defines whether to print to the console when trigger a event |
| permitedDomains | String[] | [] | For greater security, it is possible to pass a list of domains where events can be triggered. Accepts only hostname, example 'domainx.com', 'domainxpto.com', 'localhost' |
Sets
| name | Description |
|---|---|
$gtagpush | It's a function that you can call by passing an object with event information and an event type (by default, click). It sends custom events to GTM |
$gtag | This simply makes the window.dataLayer array available as an injection called $gtag. If you want to directly access the Google Tag Manager (GTM) dataLayer in some component or service, you can use $gtag to view or even interact with the raw data being sent. |
Custom Event Example:
const $gtagpush = inject('$gtagpush')
$gtagpush?.({ category: 'loja', action: 'comprou', value: 99.9 }, 'purchase')
/*
// print
{
"event": "purchase",
"category": "loja",
"action": "comprou",
"value": 99.9
}
*/
Table of Contents
