Android cordova中的Google跟踪代码管理器 [英] Google Tag Manager in Android cordova

查看:74
本文介绍了Android cordova中的Google跟踪代码管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Google跟踪代码管理器集成到由ionic2/cordova构建的Android移动应用程序中时遇到问题.通过ionic serve在Web浏览器中运行时,所有内容均可正常运行,但本机版本存在问题.

I have a problem in integrating the Google Tag Manager in my Android mobile app built with ionic2/cordova. Everything works correctly when running in web browser through ionic serve, but it has problems with the native builds.

iOS:

起初它不起作用,但是在添加 https://github.com之后/driftyco/cordova-plugin-wkwebview-engine 插件,则Google Analytics(分析)开始接收流量.因此,这一解决方案可以正常工作.

It didn't work at first, but after adding https://github.com/driftyco/cordova-plugin-wkwebview-engine plugin, the google analytics started to receive traffic. So this one is solved and works as it should.

Android:

运行该应用程序并使用Chrome控制台对其进行调试后,完全没有错误. dataLayer变量可用,我可以将数据放入其中.当没有为给定事件触发任何触发器时,它还会返回有效的响应true;当我使用正确类型的事件时,它也会返回false.因此,似乎GTM可以正确识别所有内容.我在网络"选项卡中观察到,将对象推到dataLayer之后,两个Desktop/iOS都向https://www.google-analytics.com/collect?v=1&_v=j49&a=1838345933&t=pageview&[moredatahere]发送请求,但Android应用程序未发送任何内容(网络"选项卡中什么也没有).我很好奇那是为什么?为什么在Android上没有对GA的请求?

After running the app and debugging it using Chrome console there are no errors at all. The dataLayer variable is available and I can push the data into it. It also returns valid responses true when no trigger has been fired for given event and false when I use proper type of event. So it seems that GTM recognize everything properly. I observed in the Network tab that both Desktop/iOS are sending requests to the https://www.google-analytics.com/collect?v=1&_v=j49&a=1838345933&t=pageview&[moredatahere] after pushing object to the dataLayer, but Android app doesn't send anything (just nothing in the Network tab). I'm curious why is that? Why there are no requests to the GA on the Android?

这是我的index.html文件:

Here's my index.html file:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="UTF-8">
  <title>Ionic App</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="format-detection" content="telephone=no">
  <meta name="msapplication-tap-highlight" content="no">

  <link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico">
  <link rel="manifest" href="manifest.json">
  <meta name="theme-color" content="#4e8ef7">

  <!-- Google Tag Manager -->
  <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  })(window,document,'script','dataLayer','GTM-5CH5MGN');</script>
  <!-- End Google Tag Manager -->

  <!-- cordova.js required for cordova apps -->
  <script src="cordova.js"></script>

  <link href="build/main.css" rel="stylesheet">

</head>
<body>
  <!-- Google Tag Manager (noscript) -->
  <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-KMJLK35"
  height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
  <!-- End Google Tag Manager (noscript) -->

  <!-- Ionic's root component and where the app will load -->
  <ion-app></ion-app>

  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>

  <!-- The bundle js is generated during the build process -->
  <script src="build/main.js"></script>

</body>
</html>

这就是我使用dataLayer变量与GTM通信的方式:

And that's how I communicate with the GTM using dataLayer variable:

let event = {
      event: 'virtualpageview',
      pagepath: url,
      pagetitle: title
    };

    dataLayer.push(event);

此外,我已经在cordova config.xml中添加了

Additionally in the cordova config.xml I've already added:

<access origin="*"/>

推荐答案

我要回答我自己的问题.要解决该问题,您只需调整GTM中的Universal Analytics标签即可.

I'm going to answer my own question. To solve the problem you just simply need to tweak the Universal Analytics tag in the GTM.

设置:

  • checkProtocolTasknull
  • storagenone
  • clientId用于标识没有cookie的用户(因为没有存储).我创建了一个名为Device UUID的新Javascript变量,该变量指向仅是设备标识符的全局变量device.uuid.
  • checkProtocolTask to null
  • storage to none
  • clientId is used to identify user without cookies (as storage is none). I've created new Javascript variable named Device UUID which points to global variable device.uuid which is simply a device identifier.

这些更改之后,所有内容都会正确记录.

After these changes everything is logged correctly.

这篇关于Android cordova中的Google跟踪代码管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆