Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $window.ga在AngularJS事件中未定义?_Javascript_Angularjs_Google Analytics - Fatal编程技术网

Javascript $window.ga在AngularJS事件中未定义?

Javascript $window.ga在AngularJS事件中未定义?,javascript,angularjs,google-analytics,Javascript,Angularjs,Google Analytics,我正在尝试将谷歌分析添加到我的web SPA应用程序中。我已将GA脚本添加到index.html文件中: <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.ge

我正在尝试将谷歌分析添加到我的web SPA应用程序中。我已将GA脚本添加到
index.html
文件中:

<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','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-XXXXXXXX-1', 'auto');
</script>
以下是我得到的:

>>http://localhost:3030/search
>>angular.js:9959 TypeError: undefined is not a function
    at app.run.js:54
    at Scope.$get.Scope.$broadcast (angular.js:12874)
    at $state.transitionTo.$state.transition.resolved.then.$state.transition (angular-ui-router.js:3218)
    at deferred.promise.then.wrappedCallback (angular.js:11485)
    at angular.js:11571
    at Scope.$get.Scope.$eval (angular.js:12595)
    at Scope.$get.Scope.$digest (angular.js:12407)
    at Scope.$get.Scope.$apply (angular.js:12699)
    at done (angular.js:8287)
    at completeRequest (angular.js:8499)
从这个错误中,我可以得到
$window.ga()
未定义的


我做错了什么?我怎样才能让它工作?

你确定这行工作正常吗

您在哪里初始化了
API\u URL

以及初始化
$location
的位置

因此,您可能会从
API\u URL
$location
中得到该错误(但我不确定)

因此,您需要声明
API\u URL
值,并在
$scope.$on
事件中插入位置

你的代码应该是

 $rootScope.$on("$stateChangeSuccess", function($location){
      var API_URL ="Your Url";//Put your url
        var url = API_URL + $location.url();
        console.log(url);
        $window.ga('send', 'pageview', { page: url }); // line 54
    });
编辑 试试下面的代码

您没有初始化$window

 $rootScope.$on("$stateChangeSuccess", function($location, $window){
            var API_URL = "Your Url"; // Put your url
            var url = API_URL + $location.url();
            console.log(url);
            $window.ga('send', 'pageview', { page: url }); // line 54
        });

不确定这是否是问题所在,但请尝试正确注入
$window
对象-
函数($window,event,toState,…)
@TadeášPeták这是事件。注入是在
.run
函数中进行的。对于本地主机调用,可能需要使用
none
而不是auto。在你的
ga('create','UA-XXXXXXXX-1','auto')。本地电话可能会导致问题。我的问题类似于
…'@(Request.IsLocal?“none”:“auto”)”
这个问题很老了,但是如果您仍然需要其他跟踪方法,我知道它的老问题。但是如果你启用了adBlocker,可能是因为adBlocker的原因。那么你的URL是正确的?您是否使用断点检查了该值?是的,它是正确的,错误为
$window.ga('send','pageview',{page:url})
=>
未定义不是函数
是否检查了
$window
的值?好的,您没有初始化
$window。
什么<代码>$window.localStorage
工作正常。如果我没有初始化
$窗口
,我会得到另一个错误!
 $rootScope.$on("$stateChangeSuccess", function($location, $window){
            var API_URL = "Your Url"; // Put your url
            var url = API_URL + $location.url();
            console.log(url);
            $window.ga('send', 'pageview', { page: url }); // line 54
        });