cordovaGoogleAnalytics

cordovaGoogleAnalytics

我正在尝试将ngCordova与'https://github.com/danwilson/google-analytics-plugin.git'插件一起使用。我已经添加了插件并将ngCordova添加为控制器中的依赖项。

当我尝试设置:

$cordovaGoogleAnalytics.startTrackerWithId('UA-XXXXXXXX-X');


我收到此错误:'TypeError:无法读取未定义的属性'startTrackerWithId'。

我已经在Google仪表板中将分析设置为移动应用程序。

有人可以帮忙吗?

最佳答案

发生这种情况是因为您尝试在Cordova初始化分析插件之前使用它。

只需用setTimetout递归包装初始化即可:

function _waitForAnalytics(){
    if(typeof analytics !== 'undefined'){
        $cordovaGoogleAnalytics.debugMode();
        $cordovaGoogleAnalytics.startTrackerWithId('UA-XXXXXXXX-X');
        $cordovaGoogleAnalytics.trackView('APP first screen');
    }
    else{
        setTimeout(function(){
            _waitForAnalytics();
        },250);
    }
};
_waitForAnalytics();

关于angularjs - ngCordova Google Analytics($ cordovaGoogleAnalytics),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26910421/

10-10 08:49