1

I'd like to track user activity in my angular app by creating a log of the functions tied to buttons the user clicks.

I figured the best way would be to create an event listener which logs the function name and any parameters associated with the clicked element.

I've been able to retrieve the element, but am unsure of how to get the function name and parameters.

Any help would be much appreciated.

Here's my code so far, so if I click a button which includes myFunction, i'd like the clickListener to log that 'myFunction' was called and 'foo' was the parameter passed in.

$scope.clickListener = function () {
    document.addEventListener("click", function (event) {
        console.log(event.target);
    }, false);
};

$scope.myFunction = function (foo) {
    //do stuff
}

<button ng-click="myFunction('foo')"></button>

2
  • Are you saying you want the event listener shown to log any function names of other event listeners at the time those events occur? Commented Jul 12, 2016 at 14:01
  • sorry, i'm trying to make this more clear. just updated it again Commented Jul 12, 2016 at 14:13

2 Answers 2

1

You can get the name of the function by arguments.callee.name. Needless to say that arguments of the above construct give you the list of arguments passed.

$scope.clickListener = function () {
    document.addEventListener("click", function myListener(event) {
        console.log(event.target);
        console.log(arguments.callee.name)
    }, false);
};
Sign up to request clarification or add additional context in comments.

5 Comments

calee should be callee. There is a spelling error here.
Also, even if the spelling is fixed, isn't this just identical to saying console.log('myListener')? It will just display the name of the currently-running function, which you know anyway. There might be more value in looking at arguments.callee.caller.name. Problem is, anonymous functions will return ''.
@ChrisLear, I don't disagree with that. I was simply editing a mistake. If you feel that you have a better answer, you should contribute it.
@JacobHeater I entirely agree with you. If I felt I had a better answer, I would contribute it. I don't have a better answer, but that fact in itself doesn't make this answer especially good. It will just log "myListener" every time, and I don't believe that's what the OP wants.
@ChrisLear Right, hopefully the revisions to my question have cleared things up
0

Why you don't just do:

var fun = event.target.getAtttibute("ng-click").split("(");
var name = fun[0];
var params = fun[1].split(",").pop();

2 Comments

You know old school stuff ;)
Also to have more accurate result you can use this regex stackoverflow.com/a/25469350

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.