'How to track Ajax actions via analytics function

I need to track a number of ajax-actions on my site and I have a reachGoal(TARGET_NAME) analytics function that I need insert to every function I want to track:

function handler_func() {
   reachGoal(TARGET_NAME);
   // Other code
   ... 
}

$(document).ready(function() {
    $('#target_id').click(handler_func);
});

What I would like to have is to be able to manage all targets from one place, something like this:

handler_func.before(function() {
    reachGoal(TARGET_NAME);
}

How can I achieve this?



Solution 1:[1]

You could create a decorator function:

var goalReacher = function (targetName, func) { 
    return function () {
        reachGoal(targetName);
        func.apply(this, arguments);
    };
};

var myCoolFunction = goalReacher("myTarget", function () {
    // your code here
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TylerH