Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

providers.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. const qs = require('querystring');
  3. /**
  4. * Tracking providers
  5. *
  6. * Each provider is a function(id, path) that should return
  7. * options object for request() call. It will be called bound
  8. * to Insight instance object.
  9. */
  10. module.exports = {
  11. // Google Analytics — https://www.google.com/analytics/
  12. google(id, payload) {
  13. const now = Date.now();
  14. const _qs = {
  15. // GA Measurement Protocol API version
  16. v: 1,
  17. // Hit type
  18. t: payload.type,
  19. // Anonymize IP
  20. aip: 1,
  21. tid: this.trackingCode,
  22. // Random UUID
  23. cid: this.clientId,
  24. cd1: this.os,
  25. // GA custom dimension 2 = Node Version, scope = Session
  26. cd2: this.nodeVersion,
  27. // GA custom dimension 3 = App Version, scope = Session (temp solution until refactored to work w/ GA app tracking)
  28. cd3: this.appVersion,
  29. // Queue time - delta (ms) between now and track time
  30. qt: now - parseInt(id, 10),
  31. // Cache busting, need to be last param sent
  32. z: now
  33. };
  34. // Set payload data based on the tracking type
  35. if (payload.type === 'event') {
  36. _qs.ec = payload.category;
  37. _qs.ea = payload.action;
  38. if (payload.label) {
  39. _qs.el = payload.label;
  40. }
  41. if (payload.value) {
  42. _qs.ev = payload.value;
  43. }
  44. } else {
  45. _qs.dp = payload.path;
  46. }
  47. return {
  48. url: 'https://ssl.google-analytics.com/collect',
  49. method: 'POST',
  50. // GA docs recommends body payload via POST instead of querystring via GET
  51. body: qs.stringify(_qs)
  52. };
  53. },
  54. // Yandex.Metrica - http://metrica.yandex.com
  55. yandex(id, payload) {
  56. const request = require('request');
  57. const ts = new Date(parseInt(id, 10))
  58. .toISOString()
  59. .replace(/[-:T]/g, '')
  60. .replace(/\..*$/, '');
  61. const {path} = payload;
  62. const qs = {
  63. wmode: 3,
  64. ut: 'noindex',
  65. 'page-url': `http://${this.packageName}.insight${path}?version=${this.packageVersion}`,
  66. 'browser-info': `i:${ts}:z:0:t:${path}`,
  67. // Cache busting
  68. rn: Date.now()
  69. };
  70. const url = `https://mc.yandex.ru/watch/${this.trackingCode}`;
  71. // Set custom cookie using tough-cookie
  72. const _jar = request.jar();
  73. const cookieString = `name=yandexuid; value=${this.clientId}; path=/;`;
  74. const cookie = request.cookie(cookieString);
  75. _jar.setCookie(cookie, url);
  76. return {
  77. url,
  78. method: 'GET',
  79. qs,
  80. jar: _jar
  81. };
  82. }
  83. };