Memulai
Instalasi
Section titled âInstalasiânpm install @capgo/capacitor-gtmnpx cap syncyarn add @capgo/capacitor-gtmnpx cap syncpnpm add @capgo/capacitor-gtmnpx cap syncbun add @capgo/capacitor-gtmnpx cap syncKonfigurasi Platform
Section titled âKonfigurasi Platformâ- Unduh file konfigurasi container GTM Anda dari konsol Google Tag Manager
- Tambahkan file JSON container ke proyek iOS Anda di Xcode
- File tersebut harus diberi nama seperti
GTM-XXXXXX.json
Android
Section titled âAndroidâ- Unduh file konfigurasi container GTM Anda dari konsol Google Tag Manager
- Tambahkan file JSON container ke
android/app/src/main/assets/ - File tersebut harus diberi nama seperti
GTM-XXXXXX.json
Contoh Penggunaan
Section titled âContoh Penggunaanâ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);Referensi API
Section titled âReferensi APIâinit(options)
Section titled âinit(options)âinit(options: { containerId: string }) => Promise<void>Inisialisasi Google Tag Manager dengan ID container Anda.
| Param | Type |
|---|---|
options | { containerId: string } |
pushEvent(options)
Section titled âpushEvent(options)âpushEvent(options: GTMEvent) => Promise<void>Kirim event ke dataLayer dengan parameter opsional.
| Param | Type |
|---|---|
options | GTMEvent |
setUserProperty(options)
Section titled âsetUserProperty(options)âsetUserProperty(options: { key: string; value: string }) => Promise<void>Tetapkan properti pengguna untuk segmentasi dan penargetan.
| Param | Type |
|---|---|
options | { key: string; value: string } |
getVariable(options)
Section titled âgetVariable(options)âgetVariable(options: { key: string }) => Promise<{ value: any }>Dapatkan nilai dari konfigurasi container GTM.
| Param | Type |
|---|---|
options | { key: string } |
Returns: Promise<{ value: any }>
Interface
Section titled âInterfaceâGTMEvent
Section titled âGTMEventâ| Prop | Type | Description |
|---|---|---|
event | string | Nama event |
parameters | Record<string, any> | Parameter event opsional (opsional) |
Contoh Event Umum
Section titled âContoh Event UmumâTampilan Layar
Section titled âTampilan Layarâawait CapacitorGTM.pushEvent({ event: 'screen_view', parameters: { screen_name: 'Product Details', screen_class: 'ProductScreen' }});Aksi Pengguna
Section titled âAksi Penggunaâawait CapacitorGTM.pushEvent({ event: 'button_click', parameters: { button_name: 'add_to_cart', product_id: 'SKU_12345' }});Event E-commerce
Section titled âEvent E-commerceâ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 } ] }});Praktik Terbaik
Section titled âPraktik Terbaikâ- Inisialisasi GTM lebih awal dalam lifecycle aplikasi Anda
- Gunakan konvensi penamaan event yang konsisten
- Sertakan parameter yang relevan dengan setiap event
- Test konfigurasi GTM Anda dalam mode preview
- Monitor data di konsol Google Tag Manager
- Gunakan properti pengguna untuk segmentasi yang bermakna
Debugging
Section titled âDebuggingâAktifkan mode debug di konsol Google Tag Manager untuk menguji implementasi Anda:
- Buka konsol GTM
- Masuk ke mode Preview
- Hubungkan ke aplikasi Anda menggunakan ID container
- Verifikasi bahwa event dilacak dengan benar
Kasus Penggunaan
Section titled âKasus Penggunaanâ- Lacak interaksi dan perilaku pengguna
- Monitor metrik performa aplikasi
- Kumpulkan data e-commerce
- Kelola tag pihak ketiga tanpa perubahan kode
- Implementasikan pengujian A/B dan personalisasi
- Integrasikan dengan Google Analytics dan layanan lainnya