Ad

How Can I Catch Outbound Link Click On An Angular 1.x Website?

- 1 answer

I'm trying to catch link clicks to external websites, to track them later.

Something like <a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#" ng-click="click()">foo</a> but across all DOM tree.

Most of a content is coming from external REST server, thus I cannot use ng-click there.

Thanks.

Ad

Answer

what you can do is write a directve named a

angular.module('tracking', [])
.directive('a', function() {
  return {
    restrict: 'E'
    link: function(scope, element, attrs) {
      element.on('click', function() {
        if (attrs.target == outside-target)
        //your logic
      })
    }
  }
}`

important logical part would be

     if (attrs.target == outside-target)
        //your logic
      })
Ad
source: stackoverflow.com
Ad