0

i have a situation i have been thinking for a while and cant seem to find logic to solve it. hope you can.

i have a series of buttons that i would like to track, an example of one is this:

when a button is clicked a function is called, sending some strings and an object with a series of attributes. different buttons have different sets of attributes.

$(".btn").on('click', function(ev){
   trackFunction("Purchase","apply_promo", { product_code: "product1", last_page: "home", refer: "facebook", promo: "12345" });
});

The track function will then receive this and send it to my analytics software but i need it to be sent in this format

dataToSend = { event_type : eventType, event_value: eventValue, data };

where data is each and every attribute from the object the button is sending. in that example it woud be something like this

dataToSend = { event_type : eventType, event_value: eventValue, data.product_code, data.last_page, data.refer, data.promo };`

here is my tracking function :

function trackFunction(eventType, eventValue, data ) {
   dataToSend = { event_type : eventType, event_value: eventValue, data };
   analyze(dataToSend);
   return true;
}

the problem here is that not every button is sending the same attributes inside the object so i cant hardcode the output.

hope i made myself clear. and thank you so much.

6
  • Where is your problem exactly ? At the construction, you don't know how to get all the attributes of an object ? Or at the end, you don't know to which property you must access ? Commented Sep 8, 2015 at 22:02
  • i dont know how to get all the attributes of the object and then send them all together to a function. Commented Sep 8, 2015 at 22:03
  • Does the attributes are only text values ? Commented Sep 8, 2015 at 22:05
  • yes all strings, some buttons contain lets say, promo number and price, while others contain just price, and barcode. not all the buttons contain the same attributes. Commented Sep 8, 2015 at 22:07
  • 1
    So why don't you send directly the object ? Commented Sep 8, 2015 at 22:08

1 Answer 1

1

Either you add the common properties to the object each time. The downside is that you're altering a function parameter which is generally a bad idea because it might cause weird bugs:

function track(a, b, data) {
    data.a = a;
    data.b = b;
    sendData(data);
}

Or you turn it around by iterating over the object's properties:

function track(a, b, data) {
    var send = {a: a, b: b}, k;
    for (k in data) {
        if (data.hasOwnProperty(k)) {
            send[k] = data[k];
        }
    } 
    sendData(send);
}

If you use lodash, underscore, or jQuery (or probably every other JS framework out there) you usually have an extend function at your disposal, which does pretty much what you want (and internally the same as the second example).

// with underscore:
function track(a, b, data) {
    sendData(_.extend({a: a, b: b}, data));
}
Sign up to request clarification or add additional context in comments.

Comments

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.