Last active
July 12, 2021 10:10
-
-
Save chizhovdee/22a03dadca59cebbf77b0404f93b9a05 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var _excluded = ["type", "state"]; | |
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | |
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | |
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | |
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | |
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | |
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | |
var PAGE_VIEW_PARAMS_KEY = 'dimension3'; | |
var ROUTE_PREFIX = '/backend/admin'; | |
var gaParams = {// examples | |
// visitTest: generatePageviewParams(`${ROUTE_PREFIX}/tests`), | |
// visitTestQuestion: generatePageviewParams(`${ROUTE_PREFIX}/tests/:state`) | |
}; | |
window.GA_CATEGORIES = { | |
UI_Suggestion: 'UI Suggestion', | |
UI_Interaction: 'UI Interaction', | |
Navigation: 'Navigation', | |
User_Content_Interaction: 'User Content Interaction' | |
}; | |
window.sendGA = function sendGA(eventName) { | |
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | |
var type = params.type, | |
state = params.state, | |
restParams = _objectWithoutProperties(params, _excluded); | |
try { | |
// на некоторых страницах отключена аналитика | |
if (window.__GTM_DISABLED__ || typeof window.ga === 'undefined') return; | |
if (eventName) { | |
var gaFilledParams = {}; | |
var config = gaParams[eventName]; | |
config && Object.keys(config).forEach(function (field) { | |
// если поле является функцией, то вызываем ее с параметрами для ga события | |
if (typeof config[field] === 'function') { | |
gaFilledParams[field] = config[field](restParams); | |
} else if (field === 'page') { | |
gaFilledParams[field] = config[field].replace('/:type', type ? "/".concat(type) : '').replace('/:state', state ? "/".concat(state) : ''); | |
} else { | |
gaFilledParams[field] = config[field]; | |
} | |
}); | |
if (config.hitType === 'pageview' && !isEmpty(restParams)) { | |
gaFilledParams[PAGE_VIEW_PARAMS_KEY] = gaParamsToString(restParams); | |
} | |
window.ga("set", { | |
page: gaFilledParams.page | |
}); | |
window.ga("send", gaFilledParams); | |
} else { | |
console.log("\u041D\u0435\u0442 GA \u0441\u043E\u0431\u044B\u0442\u0438\u044F ".concat(eventName, " \u0432 GA")); | |
} | |
} catch (e) { | |
console.error(e); | |
} | |
}; | |
window.sendGAEvent = function sendGAEvent(params) { | |
var _params$label = params.label, | |
label = _params$label === void 0 ? '' : _params$label, | |
action = params.action, | |
category = params.category, | |
value = params.value, | |
nonInteraction = params.nonInteraction; | |
if (!action || !category) return; | |
var eventValue = value || value === 0 ? { | |
eventValue: value | |
} : {}; | |
var options = _objectSpread(_objectSpread({ | |
eventCategory: category, | |
eventAction: action, | |
eventLabel: label, | |
hitType: 'event' | |
}, nonInteraction && { | |
nonInteraction: nonInteraction | |
}), eventValue); | |
window.ga("send", _objectSpread({}, options)); | |
}; | |
function isEmpty(obj) { | |
return !obj || Object.getOwnPropertyNames(obj).length === 0; | |
} | |
function gaParamsToString(params) { | |
var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '|'; | |
return Object.entries(params).map(function (_ref) { | |
var _ref2 = _slicedToArray(_ref, 2), | |
key = _ref2[0], | |
value = _ref2[1]; | |
return "".concat(key, "=").concat(value); | |
}).join(separator); | |
} | |
function generatePageviewParams(page) { | |
return { | |
hitType: 'pageview', | |
page: page | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PAGE_VIEW_PARAMS_KEY = 'dimension3' | |
const ROUTE_PREFIX = '/backend/admin' | |
const gaParams = { | |
// examples | |
// visitTest: generatePageviewParams(`${ROUTE_PREFIX}/tests`), | |
// visitTestQuestion: generatePageviewParams(`${ROUTE_PREFIX}/tests/:state`) | |
} | |
window.GA_CATEGORIES = { | |
UI_Suggestion: 'UI Suggestion', | |
UI_Interaction: 'UI Interaction', | |
Navigation: 'Navigation', | |
User_Content_Interaction: 'User Content Interaction' | |
} | |
window.sendGA = function sendGA (eventName, params = {}) { | |
const { type, state, ...restParams } = params | |
try { | |
// на некоторых страницах отключена аналитика | |
if (window.__GTM_DISABLED__ || typeof window.ga === 'undefined') return | |
if (eventName) { | |
const gaFilledParams = {} | |
const config = gaParams[eventName] | |
config && Object.keys(config).forEach(field => { | |
// если поле является функцией, то вызываем ее с параметрами для ga события | |
if (typeof config[field] === 'function') { | |
gaFilledParams[field] = config[field](restParams) | |
} else if (field === 'page') { | |
gaFilledParams[field] = config[field] | |
.replace('/:type', type ? `/${type}` : '') | |
.replace('/:state', state ? `/${state}` : '') | |
} else { | |
gaFilledParams[field] = config[field] | |
} | |
}) | |
if (config.hitType === 'pageview' && !isEmpty(restParams)) { | |
gaFilledParams[PAGE_VIEW_PARAMS_KEY] = gaParamsToString(restParams) | |
} | |
window.ga(`set`, { page: gaFilledParams.page }) | |
window.ga(`send`, gaFilledParams) | |
} else { | |
console.log(`Нет GA события ${eventName} в GA`) | |
} | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
window.sendGAEvent = function sendGAEvent (params) { | |
const { label = '', action, category, value, nonInteraction } = params | |
if (!action || !category) return | |
const eventValue = (value || value === 0) ? { eventValue: value } : {} | |
const options = { | |
eventCategory: category, | |
eventAction: action, | |
eventLabel: label, | |
hitType: 'event', | |
...(nonInteraction && { nonInteraction }), | |
...eventValue | |
} | |
window.ga(`send`, { ...options }) | |
} | |
function isEmpty (obj) { | |
return !obj || Object.getOwnPropertyNames(obj).length === 0 | |
} | |
function gaParamsToString (params, separator = '|') { | |
return Object.entries(params).map(([key, value]) => `${key}=${value}`).join(separator) | |
} | |
function generatePageviewParams (page) { | |
return { | |
hitType: 'pageview', | |
page | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment