Getting Started
Installation
Section titled “Installation”npm install @capgo/capacitor-gtmnpx cap syncyarn add @capgo/capacitor-gtmnpx cap syncpnpm add @capgo/capacitor-gtmnpx cap syncbun add @capgo/capacitor-gtmnpx cap syncPlatform Configuration
Section titled “Platform Configuration”- Download your GTM container configuration file from Google Tag Manager console
- Add the container JSON file to your iOS project in Xcode
- The file should be named something like
GTM-XXXXXX.json
Android
Section titled “Android”- Download your GTM container configuration file from Google Tag Manager console
- Add the container JSON file to
android/app/src/main/assets/ - The file should be named something like
GTM-XXXXXX.json
Usage Example
Section titled “Usage Example”import { CapacitorGTM } from '@capgo/capacitor-gtm';
// Initialize GTM with your container IDawait CapacitorGTM.init({ containerId: 'GTM-XXXXXX'});
// Push events to dataLayerawait CapacitorGTM.pushEvent({ event: 'screen_view', parameters: { screen_name: 'Home', screen_class: 'HomeScreen' }});
// Push custom events with parametersawait CapacitorGTM.pushEvent({ event: 'purchase', parameters: { transaction_id: 'T12345', value: 25.00, currency: 'USD', items: [ { item_id: 'SKU_12345', item_name: 'Product Name', quantity: 1, price: 25.00 } ] }});
// Set user propertiesawait CapacitorGTM.setUserProperty({ key: 'user_type', value: 'premium'});
// Get values from dataLayerconst result = await CapacitorGTM.getVariable({ key: 'config_value'});console.log('Config value:', result.value);API Reference
Section titled “API Reference”init(options)
Section titled “init(options)”init(options: { containerId: string }) => Promise<void>Initialize Google Tag Manager with your container ID.
| Param | Type |
|---|---|
options | { containerId: string } |
pushEvent(options)
Section titled “pushEvent(options)”pushEvent(options: GTMEvent) => Promise<void>Push an event to the dataLayer with optional parameters.
| Param | Type |
|---|---|
options | GTMEvent |
setUserProperty(options)
Section titled “setUserProperty(options)”setUserProperty(options: { key: string; value: string }) => Promise<void>Set a user property for segmentation and targeting.
| Param | Type |
|---|---|
options | { key: string; value: string } |
getVariable(options)
Section titled “getVariable(options)”getVariable(options: { key: string }) => Promise<{ value: any }>Get a value from the GTM container configuration.
| Param | Type |
|---|---|
options | { key: string } |
Returns: Promise<{ value: any }>
Interfaces
Section titled “Interfaces”GTMEvent
Section titled “GTMEvent”| Prop | Type | Description |
|---|---|---|
event | string | The event name |
parameters | Record<string, any> | Optional event parameters (optional) |
Common Event Examples
Section titled “Common Event Examples”Screen Views
Section titled “Screen Views”await CapacitorGTM.pushEvent({ event: 'screen_view', parameters: { screen_name: 'Product Details', screen_class: 'ProductScreen' }});User Actions
Section titled “User Actions”await CapacitorGTM.pushEvent({ event: 'button_click', parameters: { button_name: 'add_to_cart', product_id: 'SKU_12345' }});E-commerce Events
Section titled “E-commerce Events”await CapacitorGTM.pushEvent({ event: 'begin_checkout', parameters: { currency: 'USD', value: 75.00, items: [ { item_id: 'SKU_12345', item_name: 'Product 1', quantity: 2, price: 25.00 }, { item_id: 'SKU_67890', item_name: 'Product 2', quantity: 1, price: 25.00 } ] }});Best Practices
Section titled “Best Practices”- Initialize GTM early in your app lifecycle
- Use consistent event naming conventions
- Include relevant parameters with each event
- Test your GTM configuration in preview mode
- Monitor data in Google Tag Manager console
- Use user properties for meaningful segmentation
Debugging
Section titled “Debugging”Enable debug mode in Google Tag Manager console to test your implementation:
- Open GTM console
- Go to Preview mode
- Connect to your app using the container ID
- Verify events are being tracked correctly
Use Cases
Section titled “Use Cases”- Track user interactions and behavior
- Monitor app performance metrics
- Collect e-commerce data
- Manage third-party tags without code changes
- Implement A/B testing and personalization
- Integrate with Google Analytics and other services