I have to retrieve the content in data-birf-extra in the following script:
<a href="" data-birf-log="component" data-birf-par="mdot_v_lstg_1" data-birf-extra="{'c':'569710','v':'282188481','pg':'mdot_mya_qsc'}" data-birf-cmp="mdot_delt_car">
In javascript, I used:
//get all listings in an array
var savedListings=$('[id$=savedListingsPanel]').find(".savelisting-button .savelisting-trigger.favorite");
Since, there are multiple dom elements found by the above selector, so
//fire bi for each
$.each(savedListings,function(index,listing){
var par=$(listing).attr('data-birf-par');
var extra=$(listing).attr('data-birf-extra');
var jsonExtra=JSON.parse(extra.replace(/'/g, '"'));
jsonExtra['cmp']="mdot_my_dtc";
jsonExtra['et']="click";
if ($(listing).attr("data-birf-par")) {
jsonExtra['par']=par;
window.BIRF.log(jsonExtra,'event');
} else {
window.BIRF.log(jsonExtra, 'event');
}
});
The following are the issues I had:
1. JSON.parse(extra) not work, it complains "Uncaught SyntaxError: Unexpected token ' in JSON at position 1(…). "
reason: extra contains single quotation which is not a valid character when use JSON.parse()-----> need to verfiy?
solution: replace all single quotation with double quotation using .replace(regex, value)
var jsonExtra=JSON.parse(extra.replace(/'/g, '"'));
JSON.stringify(...); // convert json object into a string by adding double quotations
JSON.parse(...); // convert string to a json object by removing double quotations
2. Add new filed to json object:
jsonExtra['cmp']="mdot_my_dtc";
jsonExtra['et']="click";
3. fire birf:
window.BIRF.log(jsonExtra,'event'); //fire bi_event
window.BIRF.log(jsonExtra,'subpage'); //fire bi_page