node.js - How to add campaign tracking data to any url automatically? -
i bunch of different url sources , redirect same url, campaign data added url (to track referred clicks).
for example have these urls:
www.example.com/category/product/name.html
www.example.com/id_product=5
i want add @ end following: utm_source=source&utm_medium=medium&utm_campaign=campaign
and urls become
www.example.com/category/product/name.html?utm_source=source&utm_medium=medium&utm_campaign=campaign
www.example.com/id_product=5&utm_source=source&utm_medium=medium&utm_campaign=campaign
how correctly check , cover cases if url string has parameters, , add mine? want in node.js
thank you
elaborating on @snkashis, similar arguably more elegant solution, again using node's url module, is:
var addqueryparams = function (cleanurl) { var obj = url.parse(cleanurl, true, false); obj.query['utm_source'] = 'source'; obj.query['utm_medium'] = 'medium'; obj.query['utm_campaign'] = 'campaign'; delete obj.search; // makes format compose search string out of query object var trackedurl = url.format(obj); return trackedurl; };
this works, because url.format first looks search
and, if can't find it, composes query string query
object
(taken node url module documentation http://nodejs.org/api/url.html#url_url_format_urlobj )
search used in place of query
query (object; see querystring) used if search absent.
Comments
Post a Comment