AngularJS振幅服务不作为单例
我最近发布了一个类似的问题,但这不是重复的。 很抱歉这篇代码繁重的文章,但我想提供尽可能多的上下文。我在Angular.js应用程序中将分析工具“振幅”定义为服务时遇到问题。服务应该在整个应用程序()中充当单例,因此我对以下行为感到困惑 在我的app.js文件中,我在.run函数中调用AmplitudeService.logEvent('EVENT_NAME'),该函数成功地将事件记录到Amplitude注意:Console.log(AmplitudeService)返回一个包含所有正确函数的对象 但是,当我在任何其他控制器(如header.js中调用AmplitudeService.logEvent(“事件名称”)时,我从未在振幅仪表板中看到任何数据注意:Console.log(AmplitudeService)返回header.js中与从app.js返回的对象相同的对象AngularJS振幅服务不作为单例,angularjs,singleton,analytics,angularjs-service,amplitude,Angularjs,Singleton,Analytics,Angularjs Service,Amplitude,我最近发布了一个类似的问题,但这不是重复的。 很抱歉这篇代码繁重的文章,但我想提供尽可能多的上下文。我在Angular.js应用程序中将分析工具“振幅”定义为服务时遇到问题。服务应该在整个应用程序()中充当单例,因此我对以下行为感到困惑 在我的app.js文件中,我在.run函数中调用AmplitudeService.logEvent('EVENT_NAME'),该函数成功地将事件记录到Amplitude注意:Console.log(AmplitudeService)返回一个包含所有正确函数的对
angular.module('app', [
'ngRoute',
'angular-amplitude',
'AmplitudeService',
])
.run(['AmplitudeService', function(AmplitudeService){
console.log(AmplitudeService); // Outputs 'Object {}'
AmplitudeService.init();
*AmplitudeService.logEvent('LAUNCHED_SITE'); // This logs the event*
console.log(AmplitudeService); // Outputs 'Object {}'
}])
angular.module('app.common.header', [])
.controller('HeaderCtrl', [ '$rootScope', '$scope', '$location','$route', '$window', 'AmplitudeService', function($rootScope, $scope, $location, $route, $window, AmplitudeService){
$scope.goToSearch = function(term) {
$location.path('/search/' + term);
console.log(AmplitudeService); // Outputs 'Object {}'
*AmplitudeService.logEvent('SEARCHED');* // This does not log the event
};
}]);
如果您有任何见解,我们将不胜感激
附:官员。我正试图通过这个来实现它
AmplitudeService.js()
注意:如果检查作者的语法,他会在服务结束时返回一个对象。在我的研究中,我读到在定义服务函数()时使用“this”关键字,并且不需要像使用工厂一样返回对象,因此我相应地对其进行了更新
angular.module('AmplitudeService', [])
.service('AmplitudeService',
['$amplitude', '$rootScope', 'amplitudeApiKey', '$location',
function ($amplitude, $rootScope, amplitudeApiKey, $location) {
this.init = function() {
$amplitude.init(amplitudeApiKey, null);
}
this.identifyUser = function(userId, userProperties) {
$amplitude.setUserId(userId);
$amplitude.setUserProperties(userProperties);
}
this.logEvent = function(eventName, params) {
$amplitude.logEvent(eventName, params);
}
}]);
角振幅.js()
这允许在整个应用程序中访问“$振幅”
(function(){
var module = angular.module('angular-amplitude', ['ng']);
module.provider('$amplitude', [function $amplitudeProvider() {
this.$get = ['$window', function($window) {
(function(e,t){
var r = e.amplitude || {};
var n = t.createElement("script");
n.type = "text/javascript";
n.async = true;
n.src = "https://d24n15hnbwhuhn.buttfront.net/libs/amplitude-2.2.0-min.gz.js";
var s = t.getElementsByTagName("script")[0];
s.parentNode.insertBefore(n,s);
r._q = [];
function a(e){
r[e] = function(){
r._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
}
}
var i = ["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties"];
for(var o = 0; o < i.length; o++){
a(i[o])
}
e.amplitude = r
}
)(window,document);
return $window.amplitude;
}];
}]);
return module;
}());
Header.js
angular.module('app', [
'ngRoute',
'angular-amplitude',
'AmplitudeService',
])
.run(['AmplitudeService', function(AmplitudeService){
console.log(AmplitudeService); // Outputs 'Object {}'
AmplitudeService.init();
*AmplitudeService.logEvent('LAUNCHED_SITE'); // This logs the event*
console.log(AmplitudeService); // Outputs 'Object {}'
}])
angular.module('app.common.header', [])
.controller('HeaderCtrl', [ '$rootScope', '$scope', '$location','$route', '$window', 'AmplitudeService', function($rootScope, $scope, $location, $route, $window, AmplitudeService){
$scope.goToSearch = function(term) {
$location.path('/search/' + term);
console.log(AmplitudeService); // Outputs 'Object {}'
*AmplitudeService.logEvent('SEARCHED');* // This does not log the event
};
}]);
更新:我已尝试将服务切换到工厂,但没有产生任何新结果。找到了解决方案,希望这对遇到此问题的任何人都有帮助。我的解决方案是通过在app.js中的.run()函数中调用AmplitudeService.init()来初始化SDK,该函数在我的窗口中初始化了振幅对象。从那时起,我将$window作为服务包含在每个控制器中,并称为$window.Amplication.logEvent('event_name_here')
如果您有任何疑问,请随时联系。谢谢。找到了解决方案,希望这对遇到此问题的人有所帮助。我的解决方案是通过在app.js中的.run()函数中调用AmplitudeService.init()来初始化SDK,该函数在我的窗口中初始化了振幅对象。从那时起,我将$window作为服务包含在每个控制器中,并称为$window.Amplication.logEvent('event_name_here')
如果您有任何疑问,请随时联系。谢谢。我们在一个大型项目中遇到了类似的问题,我们创建了to init Amplication和一个提供logEvent和sendUserId的服务。我们在一个大型项目中遇到了类似的问题,我们创建了to init Amplication和一个提供logEvent和sendUserId的服务