Skip to main content
@capgo/capacitor-gtm Analytics Open source

GTM Capacitor plugin

Google Tag Manager integration for analytics and tracking

Install

bun add @capgo/capacitor-gtm bunx cap sync
npm, pnpm or yarn
  • npm install @capgo/capacitor-gtm
  • pnpm add @capgo/capacitor-gtm
  • yarn add @capgo/capacitor-gtm

Guide

How to use GTM in Capacitor

Using @capgo/capacitor-gtm

The main interface for the Google Tag Manager plugin.

Install

bun add @capgo/capacitor-gtm
bunx cap sync

bunx cap sync copies the plugin's native code into your native projects. Run it again after every plugin upgrade.

Import

import { GoogleTagManager } from '@capgo/capacitor-gtm';

API at a glance

Method Description
initialize Initializes Google Tag Manager with the specified container ID.
push Pushes an event to the Google Tag Manager dataLayer.
setUserProperty Sets a user property in the Google Tag Manager dataLayer.
getValue Gets a value from the Google Tag Manager dataLayer. Searches through the dataLayer for the most recent value of the specified key.
reset Resets the Google Tag Manager instance and clears all data. This will remove all data from the dataLayer and require re-initialization.

Examples

initialize()

Initializes Google Tag Manager with the specified container ID.

import { GoogleTagManager } from '@capgo/capacitor-gtm';

await GoogleTagManager.initialize({ containerId: 'container-id-123' });

push()

Pushes an event to the Google Tag Manager dataLayer.

import { GoogleTagManager } from '@capgo/capacitor-gtm';

await GoogleTagManager.push({
  event: 'purchase',
  parameters: {
    value: 99.99,
    currency: 'USD'
  }
});

setUserProperty()

Sets a user property in the Google Tag Manager dataLayer.

import { GoogleTagManager } from '@capgo/capacitor-gtm';

await GoogleTagManager.setUserProperty({
  key: 'user_type',
  value: 'premium'
});

getValue()

Gets a value from the Google Tag Manager dataLayer. Searches through the dataLayer for the most recent value of the specified key.

import { GoogleTagManager } from '@capgo/capacitor-gtm';

const result = await GoogleTagManager.getValue({ key: 'key-123' });
console.log(result);

reset()

Resets the Google Tag Manager instance and clears all data. This will remove all data from the dataLayer and require re-initialization.

import { GoogleTagManager } from '@capgo/capacitor-gtm';

await GoogleTagManager.reset();

Full reference

Keep going from Using @capgo/capacitor-gtm

If you are using Using @capgo/capacitor-gtm to plan native plugin work, connect it with @capgo/capacitor-gtm for the implementation detail in @capgo/capacitor-gtm, Getting Started for the implementation detail in Getting Started, Capgo Plugin Directory for the product workflow in Capgo Plugin Directory, Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, and Adding or Updating Plugins for the implementation detail in Adding or Updating Plugins.

FAQ

GTM plugin FAQ

How do I install the GTM plugin in a Capacitor app?

Run "bun add @capgo/capacitor-gtm" (or "npm install @capgo/capacitor-gtm"), then run "bunx cap sync" so the iOS and Android projects pick up the native code. Import it from "@capgo/capacitor-gtm" in your app code.

Does @capgo/capacitor-gtm work with React, Vue and Angular?

Yes. GTM is a Capacitor package, so it works with any web framework that runs inside Capacitor, including Ionic, React, Vue, Angular, Svelte and plain JavaScript.

Which Capacitor version does @capgo/capacitor-gtm support?

Capgo plugins follow Capacitor's major version: use the plugin major version that matches your Capacitor major version (for example plugin v8 with Capacitor 8). The compatibility table in the GitHub README lists the maintained versions.

Is @capgo/capacitor-gtm free and open source?

Yes. The source code is public on GitHub at https://github.com/Cap-go/capacitor-gtm/ and the package is free to install from npm. Bug reports and pull requests are welcome.

Can I update code that uses GTM without an App Store review?

Installing or upgrading the plugin changes native code, so it needs a new store build. After that, JavaScript, HTML and CSS changes that call the plugin can ship instantly with Capgo live updates.

Ship GTM changes without waiting for app review

Once the plugin is in your store build, Capgo live updates push your JavaScript, HTML and CSS changes to users in minutes.

Start with Capgo