Guest User

Untitled

a guest
Feb 25th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. /* eslint-disable prefer-rest-params */
  2. import DiadocMain from './diadoc-main';
  3.  
  4. let gaDiadoc = () => {};
  5.  
  6. if (!window.ga_diadoc && !process.env.TESTING) {
  7.  
  8. ((w, d, s, src, ga_diadoc, a, m) => {
  9. w['GoogleAnalyticsObject'] = ga_diadoc;
  10. w[ga_diadoc] = w[ga_diadoc]
  11. || (() => (w[ga_diadoc].q = w[ga_diadoc].q || []).push(arguments));
  12.  
  13. w[ga_diadoc].l = 1 * new Date();
  14. a = d.createElement(s);
  15. m = d.getElementsByTagName(s)[0];
  16. a.async = 1;
  17. a.src = src;
  18. m.parentNode.insertBefore(a, m);
  19.  
  20. a.onload = () => {
  21. window.ga_diadoc('create', 'UA-17444089-1', 'auto', {
  22. 'allowLinker': true
  23. });
  24.  
  25. const userId = DiadocMain.getCurrentUserId();
  26. const orgId = DiadocMain.getCurrentOrgId();
  27.  
  28. if (userId != null) {
  29. window.ga_diadoc('set', '&uid', userId);
  30. window.ga_diadoc('set', 'dimension1', userId);
  31. }
  32.  
  33. if (orgId != null) {
  34. window.ga_diadoc('set', 'dimension2', orgId);
  35. }
  36.  
  37. window.ga_diadoc('require', 'linker');
  38. window.ga_diadoc('linker:autoLink', ['www.diadoc.ru']);
  39. window.ga_diadoc('send', 'pageview');
  40. };
  41.  
  42. })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga_diadoc');
  43.  
  44. gaDiadoc = (e, label, eventValue) => {
  45. const fieldsObject = eventValue === undefined ? undefined : { eventValue };
  46. if (window.ga_diadoc) {
  47. console.log(`ga->${e}`);
  48. window.ga_diadoc('send', 'event', 'system', e, label === undefined ? '' : label, fieldsObject);
  49. }
  50. };
  51. //TODO: if testing env, then write ga to localStorage
  52. } else if (process.env.TESTING) {
  53. window.ga_diadoc = (send, ev, sys, action, label, count, metrics) => {
  54. let current = localStorage.getItem('diadoc_ga') || '';
  55. let tmp = action;
  56.  
  57. if (label) tmp += `$${label}`;
  58. if (count) tmp += `$${JSON.stringify(count)}`;
  59. if (metrics) tmp += `$${JSON.stringify(metrics)}`;
  60.  
  61. current += `${tmp}|`;
  62. localStorage.setItem('diadoc_ga', current);
  63. };
  64.  
  65. window.ga_diadoc.reset = () => localStorage.setItem('diadoc_ga', '');
  66. window.ga_diadoc.getLog = () => localStorage.getItem('diadoc_ga') || '';
  67.  
  68. gaDiadoc = (e, label, count, metrics) => {
  69. window.ga_diadoc('send', 'event', 'system', e, label === undefined ? '' : label, count, metrics);
  70. };
  71. }
  72.  
  73.  
  74. if (typeof module !== 'undefined' && module.exports) {
  75. module.exports = gaDiadoc;
  76. }
  77.  
  78. if (typeof dd !== 'undefined') {
  79. dd.ui = { ...dd.ui, ga_diadoc: gaDiadoc };
  80. }
Add Comment
Please, Sign In to add comment