UNPKG

4.98 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15var __values = (this && this.__values) || function (o) {
16 var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
17 if (m) return m.call(o);
18 return {
19 next: function () {
20 if (o && i >= o.length) o = void 0;
21 return { value: o && o[i++], done: !o };
22 }
23 };
24};
25var __read = (this && this.__read) || function (o, n) {
26 var m = typeof Symbol === "function" && o[Symbol.iterator];
27 if (!m) return o;
28 var i = m.call(o), r, ar = [], e;
29 try {
30 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
31 }
32 catch (error) { e = { error: error }; }
33 finally {
34 try {
35 if (r && !r.done && (m = i["return"])) m.call(i);
36 }
37 finally { if (e) throw e.error; }
38 }
39 return ar;
40};
41var __importStar = (this && this.__importStar) || function (mod) {
42 if (mod && mod.__esModule) return mod;
43 var result = {};
44 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
45 result["default"] = mod;
46 return result;
47};
48var __importDefault = (this && this.__importDefault) || function (mod) {
49 return (mod && mod.__esModule) ? mod : { "default": mod };
50};
51Object.defineProperty(exports, "__esModule", { value: true });
52var React = __importStar(require("react"));
53var react_import_remote_1 = __importDefault(require("@shopify/react-import-remote"));
54var utilities_1 = require("./utilities");
55exports.SETUP_SCRIPT = "\n window['GoogleAnalyticsObject'] = 'ga';\n window['ga'] = window['ga'] || function() {\n (window['ga'].q = window['ga'].q || []).push(arguments);\n };\n window['ga'].l = 1 * new Date();\n";
56exports.UNIVERSAL_GA_SCRIPT = 'https://www.google-analytics.com/analytics.js';
57exports.UNIVERSAL_GA_DEBUG_SCRIPT = 'https://www.google-analytics.com/analytics_debug.js';
58var UniversalGoogleAnalytics = /** @class */ (function (_super) {
59 __extends(UniversalGoogleAnalytics, _super);
60 function UniversalGoogleAnalytics() {
61 var _this = _super !== null && _super.apply(this, arguments) || this;
62 _this.setAnalytics = function (googleAnalytics) {
63 var e_1, _a;
64 var _b = _this.props, account = _b.account, domain = _b.domain, _c = _b.set, setVariables = _c === void 0 ? {} : _c, onLoad = _b.onLoad, _d = _b.debug, debug = _d === void 0 ? false : _d;
65 var normalizedDomain = utilities_1.getRootDomain(domain);
66 var options = {
67 cookieDomain: normalizedDomain,
68 legacyCookieDomain: normalizedDomain,
69 allowLinker: true,
70 };
71 if (debug) {
72 // The debug version of the analytics.js library
73 // https://developers.google.com/analytics/devguides/collection/analyticsjs/debugging
74 googleAnalytics('set', 'sendHitTask', null);
75 }
76 googleAnalytics('create', account, 'auto', options);
77 try {
78 for (var _e = __values(Object.entries(setVariables)), _f = _e.next(); !_f.done; _f = _e.next()) {
79 var _g = __read(_f.value, 2), key = _g[0], value = _g[1];
80 googleAnalytics('set', key, value);
81 }
82 }
83 catch (e_1_1) { e_1 = { error: e_1_1 }; }
84 finally {
85 try {
86 if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
87 }
88 finally { if (e_1) throw e_1.error; }
89 }
90 if (onLoad) {
91 onLoad(googleAnalytics);
92 }
93 };
94 return _this;
95 }
96 UniversalGoogleAnalytics.prototype.render = function () {
97 var debug = this.props.debug;
98 return (React.createElement(React.Fragment, null,
99 React.createElement("script", { id: "google-analytics-universal-script", dangerouslySetInnerHTML: { __html: exports.SETUP_SCRIPT } }),
100 React.createElement(react_import_remote_1.default, { preconnect: true, source: debug ? exports.UNIVERSAL_GA_DEBUG_SCRIPT : exports.UNIVERSAL_GA_SCRIPT, getImport: getUniversalAnalytics, onError: utilities_1.noop, onImported: this.setAnalytics })));
101 };
102 return UniversalGoogleAnalytics;
103}(React.PureComponent));
104exports.default = UniversalGoogleAnalytics;
105function getUniversalAnalytics(window) {
106 return window.ga;
107}