Last active
September 18, 2019 17:59
-
-
Save maxpoletaev/50cd1a2e28a079ffbac0 to your computer and use it in GitHub Desktop.
Goals directive for Yandex.Metrika
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var module = angular.module('yaMetrika', | |
[] | |
); | |
module.provider('yaMetrika', function() { | |
var counter = null; | |
this.setCounter = function(ycounter) { | |
counter = ycounter; | |
}; | |
this.$get = function($log) { | |
return { | |
reachGoal: function(goal, params) { | |
if (counter) { | |
counter.reachGoal(goal, params); | |
$log.debug('yaMetrika goal reached: ' + goal); | |
} else { | |
$log.error('yaMetrika counter is not configured'); | |
} | |
} | |
} | |
}; | |
}); | |
module.directive('yaMetrika', function(yaMetrika) { | |
return { | |
link: function($scope, $element, attrs) { | |
var params = $scope.$eval(attrs.yaMetrika); | |
var event = params.event || 'click'; | |
var goal = params.goal; | |
delete params.event; | |
if (goal) { | |
$element.bind(event, function() { | |
yaMetrika.reachGoal(goal, params); | |
}); | |
} | |
} | |
} | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment