-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Anyone made this work for Ionic2 by any chance? #44
Comments
Hi, I can confirm that this plugin works with Ionic 2. Here is the provider that I wrote for an Ionic 2 app: Click to expandimport { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
declare var cordova: any;
declare class GoogleTagManager {
init(success: any, error: any, id: string, period: number);
trackPage(success: any, error: any, pageURL: string);
trackEvent(success: any, error: any, category: string, eventAction: string, eventLabel: string, eventValue: number);
pushEvent(success: any, fail: any, eventData: any);
}
@Injectable()
export class GoogleTagManagerService {
private gtm: GoogleTagManager;
private isInitialized: boolean = false;
public initialized$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
init(id: string) {
return new Promise((resolve, reject) => {
if (window['cordova']) {
this.gtm = cordova.require('com.jareddickson.cordova.tag-manager.TagManager');
let cbSuccess = (e) => {
console.info('GTM init was successful', e);
this.isInitialized = true;
this.initialized$.next(true);
resolve(e);
}
let cbError = (e) => {
console.error('GTM init was not successful', e);
reject(e);
}
this.gtm.init(cbSuccess, cbError, id, 30);
}
});
}
trackPage(path: string) {
if (this.isInitialized === false) {
console.info('Simulated >> GTM.trackPage', path);
return Promise.resolve();
}
console.info('GTM.trackPage', path);
return new Promise((resolve, reject) => {
let cbSuccess = (e) => {
console.info('GTM trackPage was successful', e);
resolve(e);
}
let cbError = (e) => {
console.error('GTM trackPage was not successful', e);
reject(e);
}
this.gtm.trackPage(cbSuccess, cbError, path);
});
}
trackEvent(category: string, eventAction: string, eventLabel: string, eventValue: number) {
if (this.isInitialized === false) {
console.info('Simulated >> GTM.trackEvent', category, eventAction, eventLabel, eventValue);
return Promise.resolve();
}
console.info('GTM.trackEvent', category, eventAction, eventLabel, eventValue);
return new Promise((resolve, reject) => {
let cbSuccess = (e) => {
console.info('GTM trackEvent was successful', e);
resolve(e);
}
let cbError = (e) => {
console.error('GTM trackEvent was not successful', e);
reject(e);
}
this.gtm.trackEvent(
cbSuccess, cbError,
category, eventAction, eventLabel, eventValue
);
});
}
pushEvent(data = {}) {
if (this.isInitialized === false) {
console.info('Simulated >> GTM.pushEvent', data);
Promise.resolve();
}
return new Promise((resolve, reject) => {
let dataObj = data;
dataObj['event'] = event;
let cbSuccess = (e) => {
console.info('GTM pushEvent was successful', e);
resolve(e);
}
let cbError = (e) => {
console.error('GTM pushEvent was not successful', e);
reject(e);
}
this.gtm.pushEvent(cbSuccess, cbError, data);
});
}
} Here is an example of usage: this.gtm.pushEvent({
prop1: var1,
prop2: var2
}).then(() => {
this.gtm.trackPage(`/somePage`);
}) When used in a browser the provider will write log messages to the browser console for verification purposes. |
@abinici , Could you please provide the list of variables created in tag manager container. |
@abinici, any update on this ? |
Hi @jsk548, Sorry mate, didnt have time to comment. In my tag manager the following built-in variables have been defined:
And the following user-defined variables:
|
@abinici , |
@jsk548 , you can try. Otherwise, I would recommend you create containers for native apps if you want the built-in variables. Thats what I have done, one container for Android and the other one for iOS. When I initialise the GTM, I use an id thats is determined by the platform. |
I'm trying Ionic2 and it would be nice if we there was a way to access the plugin directly in js if it exists.
The text was updated successfully, but these errors were encountered: