多页设备就绪和 google 分析插件

deviceready in multipages and google analytics plugin

在我的 phonegap 应用程序中,我有以下架构:

-index.html

-page1.html

-page2.html ...

我将此添加到 index.html 以便使用 google 分析插件 (https://github.com/danwilson/google-analytics-plugin/):

<script>       
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    window.analytics.startTrackerWithId('UA-1233455-1');
    window.analytics.trackView('Home');
}  
</script>

以及其他页面(在本例中为page1.html):

<script>       
    window.analytics.trackView('page1');
</script>

在 GA 中,我只看到了 'Home' 屏幕,没有看到其他屏幕。

我是不是漏掉了什么,或者这个插件不适用于多页面架构?

谢谢

根据 the documentationwindow.analytics.trackView('page1'); 调用必须位于应用程序的 deviceReady 部分。由于您在单独页面上的 trackView 调用未包含在此处,因此不会调用它们。

在您的 deviceReady 中,您应该能够调用将在每个页面上触发的 if / else 语句。

But I understand that deviceReady only fire once on the first load of the app, is it true?

是的,这是真的。但它应该正确读取,"it only fires once the page is loaded"

So if yes it may be difficult to build an else/if statement to detect the right page?

您可以构建 SPA(单页应用程序)或在每个页面中加载 cordova.js。关于后者,我已经做到了。有效。

需要说明的是,每次加载网页时,都需要重新加载所有库。浏览器不会将库保存在内存中。这似乎是您所做的错误假设。