/kontagent_api.js

https://github.com/d1on/javascript-sdk · JavaScript · 858 lines · 437 code · 114 blank · 307 comment · 243 complexity · 0897ac69ff5ef4f5b4987cc25c8dead1 MD5 · raw file

  1. /*
  2. * Kontagent class constructor
  3. *
  4. * @constructor
  5. *
  6. * @param {string} apiKey The app's Kontagent API key
  7. * @param {object} [optionalParams] An object containing paramName => value
  8. * @param {bool} [optionalParams.useTestServer] Whether to send messages to the Kontagent Test Server
  9. * @param {bool} [optionalParams.validateParams] Whether to validate the parameters passed into the tracking method
  10. */
  11. function KontagentApi(apiKey, optionalParams) {
  12. this._baseApiUrl = "http://api.geo.kontagent.net/api/v1/";
  13. this._baseHttpsApiUrl = "https://api.geo.kontagent.net/api/v1/";
  14. this._baseTestServerUrl = "http://test-server.kontagent.com/api/v1/";
  15. this._apiKey = apiKey;
  16. this._useTestServer = (optionalParams.useTestServer) ? optionalParams.useTestServer : false;
  17. this._useHttps = (optionalParams.useHttps) ? optionalParams.useHttps : false;
  18. this._validateParams = (optionalParams.validateParams) ? optionalParams.validateParams : false;
  19. }
  20. /*
  21. * Sends an HTTP request by creating an <img> tag given a URL.
  22. *
  23. * @param {string} url The request URL
  24. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  25. */
  26. KontagentApi.prototype._sendHttpRequestViaImgTag = function(url, successCallback)
  27. {
  28. var img = new Image();
  29. // The onerror callback will always be triggered because no image header is returned by our API.
  30. // Which is fine because the request would have still gone through.
  31. if (successCallback) {
  32. img.onerror = successallback;
  33. img.onload = successCallback;
  34. }
  35. img.src = url;
  36. }
  37. /*
  38. * Sends the API message by creating an <img> tag.
  39. *
  40. * @param {string} messageType The message type to send ('apa', 'ins', etc.)
  41. * @param {object} params An object containing paramName => value (ex: 's'=>123456789)
  42. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  43. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  44. */
  45. KontagentApi.prototype._sendMessageViaImgTag = function(messageType, params, successCallback, validationErrorCallback) {
  46. if (this._validateParams == true) {
  47. var result;
  48. for (var paramKey in params) {
  49. result = KtValidator.validateParameter(messageType, paramKey, params[paramKey]);
  50. if (result != true) {
  51. validationErrorCallback(result);
  52. return;
  53. }
  54. }
  55. }
  56. var url;
  57. if (this._useTestServer == true) {
  58. url = this._baseTestServerUrl + this._apiKey + "/" + messageType + "/?" + this._httpBuildQuery(params);
  59. } else {
  60. if (this._useHttps == true) {
  61. url = this._baseHttpsApiUrl + this._apiKey + "/" + messageType + "/?" + this._httpBuildQuery(params);
  62. } else {
  63. url = this._baseApiUrl + this._apiKey + "/" + messageType + "/?" + this._httpBuildQuery(params);
  64. }
  65. }
  66. this._sendHttpRequestViaImgTag(url);
  67. }
  68. /*
  69. * Generate URL-encoded query string (same as PHP's http_build_query())
  70. *
  71. * @param {object} data The object containing key, value data to encode
  72. *
  73. * @return {string) A URL-encoded string
  74. */
  75. KontagentApi.prototype._httpBuildQuery = function(data) {
  76. var query, key, val;
  77. var tmpArray = [];
  78. for(key in data) {
  79. val = encodeURIComponent(decodeURIComponent(data[key].toString()));
  80. key = encodeURIComponent(decodeURIComponent(key));
  81. tmpArray.push(key + "=" + val);
  82. }
  83. return tmpArray.join("&");
  84. }
  85. /*
  86. * Returns random 4-character hex
  87. *
  88. * @return {string} Random 4-character hex value
  89. */
  90. KontagentApi.prototype._s4 = function() {
  91. return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
  92. }
  93. /*
  94. * Generates a unique tracking tag.
  95. *
  96. * @return {string} The unique tracking tag
  97. */
  98. KontagentApi.prototype.genUniqueTrackingTag = function() {
  99. var uniqueTrackingTag = "";
  100. for(i=0; i<4; i++) {
  101. uniqueTrackingTag += this._s4();
  102. }
  103. return uniqueTrackingTag;
  104. }
  105. /*
  106. * Generates a short unique tracking tag.
  107. *
  108. * @return {string} The short unique tracking tag
  109. */
  110. KontagentApi.prototype.genShortUniqueTrackingTag = function() {
  111. var shortUniqueTrackingTag = "";
  112. for(i=0; i<2; i++) {
  113. shortUniqueTrackingTag += this._s4();
  114. }
  115. return shortUniqueTrackingTag;
  116. }
  117. /*
  118. * Sends an Invite Sent message to Kontagent.
  119. *
  120. * @param {string} userId The UID of the sending user
  121. * @param {string} recipientUserIds A comma-separated list of the recipient UIDs
  122. * @param {string} uniqueTrackingTag 32-digit hex string used to match
  123. * InviteSent->InviteResponse->ApplicationAdded messages.
  124. * See the genUniqueTrackingTag() helper method.
  125. * @param {object} [optionalParams] An object containing paramName => value
  126. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  127. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  128. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  129. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  130. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  131. */
  132. KontagentApi.prototype.trackInviteSent = function(userId, recipientUserIds, uniqueTrackingTag, optionalParams, successCallback, validationErrorCallback) {
  133. var apiParams = {
  134. s : userId,
  135. r : recipientUserIds,
  136. u : uniqueTrackingTag
  137. };
  138. if (optionalParams != null && typeof optionalParams != 'undefined') {
  139. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  140. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  141. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  142. }
  143. this._sendMessageViaImgTag("ins", apiParams, successCallback, validationErrorCallback);
  144. }
  145. /*
  146. * Sends an Invite Response message to Kontagent.
  147. *
  148. * @param {string} uniqueTrackingTag 32-digit hex string used to match
  149. * InviteSent->InviteResponse->ApplicationAdded messages.
  150. * See the genUniqueTrackingTag() helper method.
  151. * @param {object} [optionalParams] An object containing paramName => value
  152. * @param {string} [optionalParams.recipientUserId] The UID of the responding user
  153. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  154. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  155. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  156. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  157. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  158. */
  159. KontagentApi.prototype.trackInviteResponse = function(uniqueTrackingTag, optionalParams, successCallback, validationErrorCallback) {
  160. var apiParams = {
  161. i : 0,
  162. u : uniqueTrackingTag
  163. };
  164. if (optionalParams != null && typeof optionalParams != 'undefined') {
  165. if (optionalParams.recipientUserId) { apiParams.r = optionalParams.recipientUserId; }
  166. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  167. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  168. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  169. }
  170. this._sendMessageViaImgTag("inr", apiParams, successCallback, validationErrorCallback);
  171. }
  172. /*
  173. * Sends an Notification Sent message to Kontagent.
  174. *
  175. * @param {string} userId The UID of the sending user
  176. * @param {string} recipientUserIds A comma-separated list of the recipient UIDs
  177. * @param {string} uniqueTrackingTag 32-digit hex string used to match
  178. * NotificationSent->NotificationResponse->ApplicationAdded messages.
  179. * See the genUniqueTrackingTag() helper method.
  180. * @param {object} [optionalParams] An object containing paramName => value
  181. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  182. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  183. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  184. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  185. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  186. */
  187. KontagentApi.prototype.trackNotificationSent = function(userId, recipientUserIds, uniqueTrackingTag, optionalParams, successCallback, validationErrorCallback) {
  188. var apiParams = {
  189. s : userId,
  190. r : recipientUserIds,
  191. u : uniqueTrackingTag
  192. };
  193. if (optionalParams != null && typeof optionalParams != 'undefined') {
  194. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  195. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  196. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  197. }
  198. this._sendMessageViaImgTag("nts", apiParams, successCalback, validationErrorCallback);
  199. }
  200. /*
  201. * Sends an Notification Response message to Kontagent.
  202. *
  203. * @param {string} uniqueTrackingTag 32-digit hex string used to match
  204. * NotificationSent->NotificationResponse->ApplicationAdded messages.
  205. * See the genUniqueTrackingTag() helper method.
  206. * @param {object} [optionalParams] An object containing paramName => value
  207. * @param {string} [optionalParams.recipientUserId] The UID of the responding user
  208. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  209. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  210. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  211. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  212. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  213. */
  214. KontagentApi.prototype.trackNotificationResponse = function(uniqueTrackingTag, optionalParams, successCallback, validationErrorCallback) {
  215. var apiParams = {
  216. i : 0,
  217. u : uniqueTrackingTag
  218. };
  219. if (optionalParams != null && typeof optionalParams != 'undefined') {
  220. if (optionalParams.recipientUserId) { apiParams.r = optionalParams.recipientUserId; }
  221. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  222. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  223. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  224. }
  225. this._sendMessageViaImgTag("ntr", apiParams, successCallback, validationErrorCallback);
  226. }
  227. /*
  228. * Sends an Notification Email Sent message to Kontagent.
  229. *
  230. * @param {string} userId The UID of the sending user
  231. * @param {string} recipientUserIds A comma-separated list of the recipient UIDs
  232. * @param {string} uniqueTrackingTag 32-digit hex string used to match
  233. * NotificationEmailSent->NotificationEmailResponse->ApplicationAdded messages.
  234. * See the genUniqueTrackingTag() helper method.
  235. * @param {object} [optionalParams] An object containing paramName => value
  236. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  237. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  238. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  239. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  240. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  241. */
  242. KontagentApi.prototype.trackNotificationEmailSent = function(userId, recipientUserIds, uniqueTrackingTag, optionalParams, successCallback, validationErrorCallback) {
  243. var apiParams = {
  244. s : userId,
  245. r : recipientUserIds,
  246. u : uniqueTrackingTag
  247. };
  248. if (optionalParams != null && typeof optionalParams != 'undefined') {
  249. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  250. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  251. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  252. }
  253. this._sendMessageViaImgTag("nes", apiParams, successCallback, validationErrorCallback);
  254. }
  255. /*
  256. * Sends an Notification Email Response message to Kontagent.
  257. *
  258. * @param {string} uniqueTrackingTag 32-digit hex string used to match
  259. * NotificationEmailSent->NotificationEmailResponse->ApplicationAdded messages.
  260. * See the genUniqueTrackingTag() helper method.
  261. * @param {object} [optionalParams] An object containing paramName => value
  262. * @param {string} [optionalParams.recipientUserId] The UID of the responding user
  263. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  264. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  265. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  266. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  267. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  268. */
  269. KontagentApi.prototype.trackNotificationEmailResponse = function(uniqueTrackingTag, optionalParams, successCallback, validationErrorCallback) {
  270. var apiParams = {
  271. i : 0,
  272. u : uniqueTrackingTag
  273. };
  274. if (optionalParams != null && typeof optionalParams != 'undefined') {
  275. if (optionalParams.recipientUserId) { apiParams.r = optionalParams.recipientUserId; }
  276. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  277. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  278. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  279. }
  280. this._sendMessageViaImgTag("nei", apiParams, successCallback, validationErrorCallback);
  281. }
  282. /*
  283. * Sends an Stream Post message to Kontagent.
  284. *
  285. * @param {string} userId The UID of the sending user
  286. * @param {string} uniqueTrackingTag 32-digit hex string used to match
  287. * NotificationEmailSent->NotificationEmailResponse->ApplicationAdded messages.
  288. * See the genUniqueTrackingTag() helper method.
  289. * @param {string} type The Facebook channel type
  290. * (feedpub, stream, feedstory, multifeedstory, dashboard_activity, or dashboard_globalnews).
  291. * @param {object} [optionalParams] An object containing paramName => value
  292. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  293. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  294. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  295. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  296. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  297. */
  298. KontagentApi.prototype.trackStreamPost = function(userId, uniqueTrackingTag, type, optionalParams, successCallback, validationErrorCallback) {
  299. var apiParams = {
  300. s : userId,
  301. u : uniqueTrackingTag,
  302. tu : type
  303. };
  304. if (optionalParams != null && typeof optionalParams != 'undefined') {
  305. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  306. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  307. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  308. }
  309. this._sendMessageViaImgTag("pst", apiParams, successCallback, validationErrorCallback);
  310. }
  311. /*
  312. * Sends an Stream Post Response message to Kontagent.
  313. *
  314. * @param {string} uniqueTrackingTag 32-digit hex string used to match
  315. * NotificationEmailSent->NotificationEmailResponse->ApplicationAdded messages.
  316. * See the genUniqueTrackingTag() helper method.
  317. * @param {string} type The Facebook channel type
  318. * (feedpub, stream, feedstory, multifeedstory, dashboard_activity, or dashboard_globalnews).
  319. * @param {object} [optionalParams] An object containing paramName => value
  320. * @param {string} [optionalParams.recipientUserId] The UID of the responding user
  321. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  322. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  323. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  324. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  325. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  326. */
  327. KontagentApi.prototype.trackStreamPostResponse = function(uniqueTrackingTag, type, optionalParams, successCallback, validationErrorCallback) {
  328. var apiParams = {
  329. i : 0,
  330. u : uniqueTrackingTag,
  331. tu : type
  332. };
  333. if (optionalParams != null && typeof optionalParams != 'undefined') {
  334. if (optionalParams.recipientUserId) { apiParams.r = optionalParams.recipientUserId; }
  335. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  336. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  337. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  338. }
  339. this._sendMessageViaImgTag("psr", apiParams, successCallback, validationErrorCallback);
  340. }
  341. /*
  342. * Sends an Custom Event message to Kontagent.
  343. *
  344. * @param {string} userId The UID of the user
  345. * @param {string} eventName The name of the event
  346. * @param {object} [optionalParams] An object containing paramName => value
  347. * @param {int} [optionalParams.value] A value associated with the event
  348. * @param {int} [optionalParams.level] A level associated with the event (must be positive)
  349. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  350. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  351. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  352. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  353. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  354. */
  355. KontagentApi.prototype.trackEvent = function(userId, eventName, optionalParams, successCallback, validationErrorCallback) {
  356. var apiParams = {
  357. s : userId,
  358. n : eventName
  359. };
  360. if (optionalParams != null && typeof optionalParams != 'undefined') {
  361. if (optionalParams.value) { apiParams.v = optionalParams.value; }
  362. if (optionalParams.level) { apiParams.l = optionalParams.level; }
  363. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  364. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  365. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  366. }
  367. this._sendMessageViaImgTag("evt", apiParams, successCallback, validationErrorCallback);
  368. }
  369. /*
  370. * Sends an Application Added message to Kontagent.
  371. *
  372. * @param {string} userId The UID of the installing user
  373. * @param {object} [optionalParams] An object containing paramName => value
  374. * @param {string} [optionalParams.uniqueTrackingTag] 16-digit hex string used to match
  375. * Invite/StreamPost/NotificationSent/NotificationEmailSent->ApplicationAdded messages.
  376. * See the genUniqueTrackingTag() helper method.
  377. * @param {string} [optionalParams.shortUniqueTrackingTag] 8-digit hex string used to match
  378. * ThirdPartyCommClicks->ApplicationAdded messages.
  379. * See the genShortUniqueTrackingTag() helper method.
  380. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  381. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  382. */
  383. KontagentApi.prototype.trackApplicationAdded = function(userId, optionalParams, successCallback, validationErrorCallback) {
  384. var apiParams = {s : userId};
  385. if (optionalParams != null && typeof optionalParams != 'undefined') {
  386. if (optionalParams.uniqueTrackingTag) { apiParams.u = optionalParams.uniqueTrackingTag; }
  387. if (optionalParams.shortUniqueTrackingTag) { apiParams.su = optionalParams.shortUniqueTrackingTag; }
  388. }
  389. this._sendMessageViaImgTag("apa", apiParams, successCallback, validationErrorCallback);
  390. }
  391. /*
  392. * Sends an Application Removed message to Kontagent.
  393. *
  394. * @param {string} userId The UID of the removing user
  395. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  396. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  397. */
  398. KontagentApi.prototype.trackApplicationRemoved = function(userId, successCallback, validationErrorCallback) {
  399. var apiParams = {s : userId};
  400. this._sendMessageViaImgTag("apr", apiParams, successCallback, validationErrorCallback);
  401. }
  402. /*
  403. * Sends an Third Party Communication Click message to Kontagent.
  404. *
  405. * @param {string} type The third party comm click type (ad, partner).
  406. * @param {string} shortUniqueTrackingTag 8-digit hex string used to match
  407. * ThirdPartyCommClicks->ApplicationAdded messages.
  408. * @param {object} [optionalParams] An object containing paramName => value
  409. * @param {string} [optionalParams.userId] The UID of the user
  410. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  411. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  412. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  413. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  414. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  415. */
  416. KontagentApi.prototype.trackThirdPartyCommClick = function(type, shortUniqueTrackingTag, optionalParams, successCallback, validationErrorCallback) {
  417. var apiParams = {
  418. i : 0,
  419. tu : type,
  420. su : shortUniqueTrackingTag
  421. };
  422. if (optionalParams != null && typeof optionalParams != 'undefined') {
  423. if (optionalParams.userId) { apiParams.s = optionalParams.userId; }
  424. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  425. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  426. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  427. }
  428. this._sendMessageViaImgTag("ucc", apiParams, successCallback, validationErrorCallback);
  429. }
  430. /*
  431. * Sends an Page Request message to Kontagent.
  432. *
  433. * @param {string} userId The UID of the user
  434. * @param {object} [optionalParams] An object containing paramName => value
  435. * @param {string} [optionalParams.ipAddress] The current users IP address
  436. * @param {string} [optionalParams.pageAddress] The current page address (ex: index.html)
  437. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  438. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  439. */
  440. KontagentApi.prototype.trackPageRequest = function(userId, optionalParams, successCallback, validationErrorCallback) {
  441. var apiParams = {
  442. s : userId,
  443. ts : Math.round(new Date().getTime() / 1000)
  444. };
  445. if (optionalParams != null && typeof optionalParams != 'undefined') {
  446. if (optionalParams.ipAddress) { apiParams.ip = optionalParams.ipAddress; }
  447. if (optionalParams.pageAddress) { apiParams.u = optionalParams.pageAddress; }
  448. }
  449. this._sendMessageViaImgTag("pgr", apiParams, successCallback, validationErrorCallback);
  450. }
  451. /*
  452. * Sends an User Information message to Kontagent.
  453. *
  454. * @param {string} userId The UID of the user
  455. * @param {object} [optionalParams] An object containing paramName => value
  456. * @param {int} [optionalParams.birthYear] The birth year of the user
  457. * @param {string} [optionalParams.gender] The gender of the user (m,f,u)
  458. * @param {string} [optionalParams.country] The 2-character country code of the user
  459. * @param {int} [optionalParams.friendCount] The friend count of the user
  460. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  461. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  462. */
  463. KontagentApi.prototype.trackUserInformation = function (userId, optionalParams, successCallback, validationErrorCallback) {
  464. var apiParams = {s : userId};
  465. if (optionalParams != null && typeof optionalParams != 'undefined') {
  466. if (optionalParams.birthYear) { apiParams.b = optionalParams.birthYear; }
  467. if (optionalParams.gender) { apiParams.g = optionalParams.gender; }
  468. if (optionalParams.country) { apiParams.lc = optionalParams.country; }
  469. if (optionalParams.friendCount) { apiParams.f = optionalParams.friendCount; }
  470. }
  471. this._sendMessageViaImgTag("cpu", apiParams, successCallback, validationErrorCallback);
  472. }
  473. /*
  474. * Sends an Goal Count message to Kontagent.
  475. *
  476. * @param {string} userId The UID of the user
  477. * @param {object} [optionalParams] An object containing paramName => value
  478. * @param {int} [optionalParams.goalCount1] The amount to increment goal count 1 by
  479. * @param {int} [optionalParams.goalCount2] The amount to increment goal count 2 by
  480. * @param {int} [optionalParams.goalCount3] The amount to increment goal count 3 by
  481. * @param {int} [optionalParams.goalCount4] The amount to increment goal count 4 by
  482. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  483. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  484. */
  485. KontagentApi.prototype.trackGoalCount = function(userId, optionalParams, successCallback, validationErrorCallback) {
  486. var apiParams = {s : userId};
  487. if (optionalParams != null && typeof optionalParams != 'undefined') {
  488. if (optionalParams.goalCount1) { apiParams.gc1 = optionalParams.goalCount1; }
  489. if (optionalParams.goalCount2) { apiParams.gc2 = optionalParams.goalCount2; }
  490. if (optionalParams.goalCount3) { apiParams.gc3 = optionalParams.goalCount3; }
  491. if (optionalParams.goalCount4) { apiParams.gc4 = optionalParams.goalCount4; }
  492. }
  493. this._sendMessageViaImgTag("gci", apiParams, successCallback, validationErrorCallback);
  494. }
  495. /*
  496. * Sends an Revenue message to Kontagent.
  497. *
  498. * @param {string} userId The UID of the user
  499. * @param {int} value The amount of revenue in cents
  500. * @param {object} [optionalParams] An object containing paramName => value
  501. * @param {string} [optionalParams.type] The transaction type (direct, indirect, advertisement, credits, other)
  502. * @param {string} [optionalParams.subtype1] Subtype1 value (max 32 chars)
  503. * @param {string} [optionalParams.subtype2] Subtype2 value (max 32 chars)
  504. * @param {string} [optionalParams.subtype3] Subtype3 value (max 32 chars)
  505. * @param {function} [successCallback] The callback function to execute once message has been sent successfully
  506. * @param {function(error)} [validationErrorCallback] The callback function to execute on validation failure
  507. */
  508. KontagentApi.prototype.trackRevenue = function(userId, value, optionalParams, successCallback, validationErrorCallback) {
  509. var apiParams = {
  510. s : userId,
  511. v : value
  512. };
  513. if (optionalParams != null && typeof optionalParams != 'undefined') {
  514. if (optionalParams.type) { apiParams.tu = optionalParams.type; }
  515. if (optionalParams.subtype1) { apiParams.st1 = optionalParams.subtype1; }
  516. if (optionalParams.subtype2) { apiParams.st2 = optionalParams.subtype2; }
  517. if (optionalParams.subtype3) { apiParams.st3 = optionalParams.subtype3; }
  518. }
  519. this._sendMessageViaImgTag("mtu", apiParams, successCallback, validationErrorCallback);
  520. }
  521. ////////////////////////////////////////////////////////////////////////////////
  522. /*
  523. * Helper class to validate the paramters for the Kontagent API messages. All
  524. * methods are static so no need to instantiate this class.
  525. *
  526. * @constructor
  527. */
  528. function KtValidator() {
  529. }
  530. /*
  531. * Validates a parameter of a given message type.
  532. * IMPORTANT: When evaluating the return, use a strict-type comparison: if(response === true) {}
  533. *
  534. * @param {string} messageType The message type that the param belongs to (ex: ins, apa, etc.)
  535. * @param {string} paramName The name of the parameter (ex: s, su, u, etc.)
  536. * @param {mixed} paramValue The value of the parameter
  537. *
  538. * @returns {mixed} Returns true on success and an error message string on failure.
  539. */
  540. KtValidator.validateParameter = function(messageType, paramName, paramValue) {
  541. return KtValidator['_validate' + KtValidator._upperCaseFirst(paramName)](messageType, paramName, paramValue);
  542. }
  543. KtValidator._upperCaseFirst = function(stringVal) {
  544. return stringVal.charAt(0).toUpperCase() + stringVal.slice(1);
  545. }
  546. KtValidator._validateB = function(messageType, paramName, paramValue) {
  547. // birthyear param (cpu message)
  548. if (typeof paramValue == "undefined" || paramValue != parseInt(paramValue)
  549. || paramValue < 1900 || paramValue > 2011
  550. ) {
  551. return 'Invalid birth year.';
  552. } else {
  553. return true;
  554. }
  555. }
  556. KtValidator._validateF = function(messageType, paramName, paramValue) {
  557. // friend count param (cpu message)
  558. if (typeof paramValue == "undefined" || paramValue != parseInt(paramValue) || paramValue < 0) {
  559. return 'Invalid friend count.'
  560. } else {
  561. return true;
  562. }
  563. }
  564. KtValidator._validateG = function(messageType, paramName, paramValue) {
  565. // gender param (cpu message)
  566. var regex = /^[mfu]$/;
  567. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  568. return 'Invalid gender.';
  569. } else {
  570. return true;
  571. }
  572. }
  573. KtValidator._validateGc1 = function(messageType, paramName, paramValue) {
  574. // goal count param (gc1, gc2, gc3, gc4 messages)
  575. if (typeof paramValue == "undefined" || paramValue != parseInt(paramValue)
  576. || paramValue < -16384 || paramValue > 16384
  577. ) {
  578. return 'Invalid goal count value.';
  579. } else {
  580. return true;
  581. }
  582. }
  583. KtValidator._validateGc2 = function(messageType, paramName, paramValue) {
  584. return KtValidator._validateGc1(messageType, paramName, paramValue);
  585. }
  586. KtValidator._validateGc3 = function(messageType, paramName, paramValue) {
  587. return KtValidator._validateGc1(messageType, paramName, paramValue);
  588. }
  589. KtValidator._validateGc4 = function(messageType, paramName, paramValue) {
  590. return KtValidator._validateGc1(messageType, paramName, paramValue);
  591. }
  592. KtValidator._validateI = function(messageType, paramName, paramValue) {
  593. // isAppInstalled param (inr, psr, ner, nei messages)
  594. var regex = /^[01]$/;
  595. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  596. return 'Invalid isAppInstalled value.';
  597. } else {
  598. return true;
  599. }
  600. }
  601. KtValidator._validateIp = function(messageType, paramName, paramValue) {
  602. // ip param (pgr messages)
  603. var regex = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\.\d{1,3})?$/;
  604. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  605. return 'Invalid IP address value.';
  606. } else {
  607. return true;
  608. }
  609. }
  610. KtValidator._validateL = function(messageType, paramName, paramValue) {
  611. // level param (evt messages)
  612. if (typeof paramValue == "undefined" || paramValue != parseInt(paramValue) || paramValue < 0) {
  613. return 'Invalid level value.';
  614. } else {
  615. return true;
  616. }
  617. }
  618. KtValidator._validateLc = function(messageType, paramName, paramValue) {
  619. // country param (cpu messages)
  620. var regex = /^[A-Z]{2}$/;
  621. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  622. return 'Invalid country value.';
  623. } else {
  624. return true;
  625. }
  626. }
  627. KtValidator._validateLp = function(messageType, paramName, paramValue) {
  628. // postal/zip code param (cpu messages)
  629. // this parameter isn't being used so we just return true for now
  630. return true;
  631. }
  632. KtValidator._validateLs = function(messageType, paramName, paramValue) {
  633. // state param (cpu messages)
  634. // this parameter isn't being used so we just return true for now
  635. return true;
  636. }
  637. KtValidator._validateN = function(messageType, paramName, paramValue) {
  638. // event name param (evt messages)
  639. var regex = /^[A-Za-z0-9-_]{1,32}$/;
  640. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  641. return 'Invalid event name value.';
  642. } else {
  643. return true;
  644. }
  645. }
  646. KtValidator._validateR = function(messageType, paramName, paramValue) {
  647. // Sending messages include multiple recipients (comma separated) and
  648. // response messages can only contain 1 recipient UID.
  649. if (messageType == 'ins' || messageType == 'nes' || messageType == 'nts') {
  650. // recipients param (ins, nes, nts messages)
  651. var regex = /^[0-9]+(,[0-9]+)*$/;
  652. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  653. return 'Invalid recipient user ids.';
  654. }
  655. } else if (messageType == 'inr' || messageType == 'psr' || messageType == 'nei' || messageType == 'ntr') {
  656. // recipient param (inr, psr, nei, ntr messages)
  657. if (typeof paramValue == "undefined" || paramValue != parseInt(paramValue)) {
  658. return 'Invalid recipient user id.';
  659. }
  660. }
  661. return true;
  662. }
  663. KtValidator._validateS = function(messageType, paramName, paramValue) {
  664. // userId param
  665. if (typeof paramValue == "undefined" || paramValue != parseInt(paramValue)) {
  666. return 'Invalid user id.';
  667. } else {
  668. return true;
  669. }
  670. }
  671. KtValidator._validateSt1 = function(messageType, paramName, paramValue) {
  672. // subtype1 param
  673. var regex = /^[A-Za-z0-9-_]{1,32}$/;
  674. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  675. return 'Invalid subtype value.';
  676. } else {
  677. return true;
  678. }
  679. }
  680. KtValidator._validateSt2 = function(messageType, paramName, paramValue) {
  681. return KtValidator._validateSt1(messageType, paramName, paramValue);
  682. }
  683. KtValidator._validateSt3 = function(messageType, paramName, paramValue) {
  684. return KtValidator._validateSt1(messageType, paramName, paramValue);
  685. }
  686. KtValidator._validateSu = function(messageType, paramName, paramValue) {
  687. // short tracking tag param
  688. var regex = /^[A-Fa-f0-9]{8}$/;
  689. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  690. return 'Invalid short unique tracking tag.';
  691. } else {
  692. return true;
  693. }
  694. }
  695. KtValidator._validateTs = function(messageType, paramName, paramValue) {
  696. // timestamp param (pgr message)
  697. if (typeof paramValue == "undefined" || paramValue != parseInt(paramValue)) {
  698. return 'Invalid timestamp.';
  699. } else {
  700. return true;
  701. }
  702. }
  703. KtValidator._validateTu = function(messageType, paramName, paramValue) {
  704. // type parameter (mtu, pst/psr, ucc messages)
  705. // acceptable values for this parameter depends on the message type
  706. var regex;
  707. if (messageType == 'mtu') {
  708. regex = /^(direct|indirect|advertisement|credits|other)$/;
  709. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  710. return 'Invalid monetization type.';
  711. }
  712. } else if (messageType == 'pst' || messageType == 'psr') {
  713. regex = /^(feedpub|stream|feedstory|multifeedstory|dashboard_activity|dashboard_globalnews)$/;
  714. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  715. return 'Invalid stream post/response type.';
  716. }
  717. } else if (messageType == 'ucc') {
  718. regex = /^(ad|partner)$/;
  719. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  720. return 'Invalid third party communication click type.';
  721. }
  722. }
  723. return true;
  724. }
  725. KtValidator._validateU = function(messageType, paramName, paramValue) {
  726. // unique tracking tag parameter for all messages EXCEPT pgr.
  727. // for pgr messages, this is the "page address" param
  728. if (messageType != 'pgr') {
  729. var regex = /^[A-Fa-f0-9]{16}$/;
  730. if (typeof paramValue == "undefined" || !regex.test(paramValue)) {
  731. return 'Invalid unique tracking tag.';
  732. }
  733. }
  734. return true;
  735. }
  736. KtValidator._validateV = function(messageType, paramName, paramValue) {
  737. // value param (mtu, evt messages)
  738. if (typeof paramValue == "undefined" || paramValue != parseInt(paramValue)) {
  739. return 'Invalid value.';
  740. } else {
  741. return true;
  742. }
  743. }