Skip to content

Instantly share code, notes, and snippets.

@maxpoletaev
Last active September 18, 2019 17:59
Show Gist options
  • Save maxpoletaev/50cd1a2e28a079ffbac0 to your computer and use it in GitHub Desktop.
Save maxpoletaev/50cd1a2e28a079ffbac0 to your computer and use it in GitHub Desktop.
Goals directive for Yandex.Metrika
(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