$window.ga在AngularJS事件中未定义
$window.ga is undefined in AngularJS event?
我正在尝试将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.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>
这是我的"$stateChangeSuccess"
事件:
$rootScope.$on("$stateChangeSuccess", function(event, toState, toParams, fromState, fromParams){
var url = API_URL+$location.url()
console.log(url)
$window.ga('send', 'pageview', { page: url }); // line 54
});
这是我得到的:
>>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()
是undefined
我做错了什么?我该怎么做?
你确定var url = API_URL+$location.url()
这条线工作正常吗?
您在哪里初始化API_URL
?
以及您在哪里初始化$location
?
所以你可能会从API_URL
和$location
中得到这个错误。(但不确定)
因此,您需要声明API_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.ga在AngularJS事件中未定义
- 事件在加载之前时的未定义位置
- Razor Dropdown onchange事件未触发始终未定义
- ui.columnIndex在igGridHiding-columnShowing事件中未定义
- ReferenceError:事件未在Firefox中定义错误
- 包含的javascript源代码通过PHP,onmousemove事件,函数未定义
- InAppBrowser事件.url始终未定义
- 自定义jQuery插件:事件未按预期运行
- 事件未定义 火狐.
- 事件未定义错误在 javascript 中只有 Firefox
- 事件未定义错误,仅在火狐中
- 事件未定义 JavaScript Firefox (JSON)
- JS Revearing Pattern事件未定义问题
- 事件未定义 FF
- ReferenceError:事件未定义firefox d3.js
- 为什么使用a.c click时事件未定义?
- "事件未定义“;在firefox中
- 类型错误:firefox中关于javascript的事件未定义
- jQuery onkeyup事件:未定义扫描
- IE8中的图像加载事件;事件未定义,这是“窗口”对象