【问题标题】:Google Analytics Ionic 3 (Ionic View)Google Analytics Ionic 3(离子视图)
【发布时间】:2018-01-18 20:32:49
【问题描述】:

使用本教程安装 Google Analytics:

https://www.javascripttuts.com/introduction-to-ionic-google-analytics/

我已经安装了依赖项并使用我的跟踪代码将代码放入我的构造函数中。

我正在导出我的应用以使用 Ionic View 进行测试。 Google 分析是否与 Ionic View 一起使用?我似乎无法让它工作或找到它的参考...

【问题讨论】:

  • 是的,它应该可以工作。检查您的 google-analytics 实时部分,看看那里是否有任何活动。
  • 在实时部分似乎也看不到任何东西。有趣的是,我遵循的教程在构造函数中包含代码,但 ionic 框架的引用将它放在构造函数之外和函数中。会是这样吗? @ari

标签: ionic-framework google-analytics ionic3


【解决方案1】:

我看到 Ionic 的 Google Analytics Plugin 不起作用,并且 Android SDK 正在被弃用,所以我是这样实现的:

在 Index.html 文件中,在 or 之后插入此代码

  <script>
    (function(i, s, o, g, r, a, m) {
              i['GoogleAnalyticsObject'] = r;
              i[r] = i[r] || function() {
                  (i[r].q = i[r].q || []).push(arguments)
              }, i[r].l = 1 * new Date();
              a = s.createElement(o),
                  m = s.getElementsByTagName(o)[0];
              a.async = 1;
              a.src = g;
              m.parentNode.insertBefore(a, m)
          })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
  ga('create', 'UA-XXXXXXX-X', 'auto');
  </script>

显然用你的代码替换“UA-xxxxxx-x”代码......

然后,在您要测量的每个页面中,只需将其添加到您想要的 .TS 文件的 ionViewDidEnter 方法中:

(<any>window).ga('set', 'page', 'MyPageName');
(<any>window).ga('send', 'pageview');

唯一要更改的是“MyPageName”,其余的保持不变。

如果您想跟踪某个事件,只需将此代码添加到该事件中:

(<any>window).ga('send', 'event', {
      eventCategory: 'eventCategory',
      eventLabel: 'eventLabel',
      eventAction: 'eventAction',
      eventValue: 10
    });

将类别、标签和操作替换为您要衡量的内容...

效果很好!

【讨论】:

    【解决方案2】:

    教程Link

    How to Get Google Tracking ID for Mobile Devices?

    安装插件

    $ ionic cordova plugin add cordova-plugin-google-analytics
    $ npm install --save @ionic-native/google-analytics
    

    app.component.ts文件中调用如下方法

    this.platform.ready().then(() => {
    
      this.ga.startTrackerWithId('UA-XXXXXXXXX-X')
      .then(() => {}).catch(e => alert('Error starting GoogleAnalytics == '+ e));
    
    });
    

    确保您已将插件包含在 app.module.ts 文件提供程序数组中

    在List page广告中trackViewtrackEvent方法如下图

    ...
    
      ionViewDidLoad(){
        this.ga.trackView('List Page')
        .then(() => {})
        .catch(e => console.log(e));
      }
    
    
      itemTapped(event, item) {
        this.ga.trackEvent('Category', 'Tapped Action', 'Item Tapped is '+item, 0);
        // That's right, we're pushing to ourselves!
        this.navCtrl.push(ListPage, {
          item: item
        });
      }
    
    ...
    

    【讨论】:

    • 这对原生应用很有用。如果用作 PWA 或 Web,则“Ari Waisberg”提到的答案会有所帮助。最好将两者结合起来,这样它就可以在原生 +PWA 上运行
    【解决方案3】:

    谷歌分析 Ionic 3

    安装这个插件

    $ ionic cordova plugin add cordova-plugin-google-analytics
    $ npm install --save @ionic-native/google-analytics
    

    Create track Id in your google developement console.

    在你的 app.module.ts 和 home.ts 中导入

    import { GoogleAnalytics } from '@ionic-native/google-analytics';
    

    Home.ts

        export class HomePage {
    
      constructor(public navCtrl: NavController,
                  private googleAnalytics: GoogleAnalytics) {
    
      }
      google(){
        this.googleAnalytics.startTrackerWithId('XXXXXXXXX', 30)
       .then(() => {
         console.log('Google analytics is ready now');
            this.googleAnalytics.trackView('Screen Title')
            .then(()=>{
              console.log('Success');
            })
            .catch((e)=>{
              console.log("Faild"+ e)
            })
    
            this.googleAnalytics.trackView('Screen Title', 'my-scheme://content/1111?utm_source=google&utm_campaign=my-campaign')
            .then(()=>{
              console.log("Success 1")
            })
            .catch((e)=>{
              console.log("Faild 1" + e)
            })
    
            this.googleAnalytics.trackEvent('Category', 'Action', 'Label', 30)
            .then(()=>{
              console.log("Success Event");
            })
            .catch(()=>{
              console.log("Faild")
            })
    
            this.googleAnalytics.trackMetric(5)
            .then(()=>{
              console.log("Key Matrics run successfully");
            })
            .catch((e)=>{
              console.log("Faild" + e)
            })
    
            this.googleAnalytics.trackTiming('Category', 2, 'Variable', 'Label')
            .then(()=>{
              console.log("TrackTiming success")
            })
            .catch((e)=>{
              console.log("Faild"+ e)
            })
         // Tracker is ready
         // You can now track pages or set additional information such as AppVersion or UserId
         this.googleAnalytics.debugMode();
         this.googleAnalytics.setAllowIDFACollection(true);
       })
       .catch(e => console.log('Error starting GoogleAnalytics', e));
      }
      // GTM-5SFPKBZ
    }
    

    希望对你有帮助。

    【讨论】:

    • 我必须调用该函数吗?或者只是放在那里就可以启动并录制?
    • 或者我必须把它放在构造函数中吗?这是我在离子文档中的挂断原因,它拥有它,就像你拥有它一样。我只是无法在控制台中调用它@edison-j
    • 你必须调用这个函数。我在按钮点击事件中有那个函数。
    猜你喜欢
    • 1970-01-01
    • 2019-08-16
    • 2019-01-22
    • 2018-06-09
    • 2018-07-26
    • 2018-03-07
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多