UNPKG

3.42 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var React = tslib_1.__importStar(require("react"));
5var react_import_remote_1 = tslib_1.__importDefault(require("@shopify/react-import-remote"));
6var utilities_1 = require("./utilities");
7exports.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";
8exports.UNIVERSAL_GA_SCRIPT = 'https://www.google-analytics.com/analytics.js';
9exports.UNIVERSAL_GA_DEBUG_SCRIPT = 'https://www.google-analytics.com/analytics_debug.js';
10var UniversalGoogleAnalytics = /** @class */ (function (_super) {
11 tslib_1.__extends(UniversalGoogleAnalytics, _super);
12 function UniversalGoogleAnalytics() {
13 var _this = _super !== null && _super.apply(this, arguments) || this;
14 _this.setAnalytics = function (googleAnalytics) {
15 var e_1, _a;
16 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, _e = _b.disableTracking, disableTracking = _e === void 0 ? false : _e;
17 var normalizedDomain = utilities_1.getRootDomain(domain);
18 var options = {
19 cookieDomain: normalizedDomain,
20 legacyCookieDomain: normalizedDomain,
21 allowLinker: true,
22 };
23 googleAnalytics('create', account, 'auto', options);
24 if (debug || disableTracking) {
25 // Prevent data being sent to Google
26 // https://developers.google.com/analytics/devguides/collection/analyticsjs/debugging#testing_your_implementation_without_sending_hits
27 googleAnalytics('set', 'sendHitTask', null);
28 }
29 try {
30 for (var _f = tslib_1.__values(Object.entries(setVariables)), _g = _f.next(); !_g.done; _g = _f.next()) {
31 var _h = tslib_1.__read(_g.value, 2), key = _h[0], value = _h[1];
32 googleAnalytics('set', key, value);
33 }
34 }
35 catch (e_1_1) { e_1 = { error: e_1_1 }; }
36 finally {
37 try {
38 if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
39 }
40 finally { if (e_1) throw e_1.error; }
41 }
42 if (onLoad) {
43 onLoad(googleAnalytics);
44 }
45 };
46 return _this;
47 }
48 UniversalGoogleAnalytics.prototype.render = function () {
49 var _a = this.props, debug = _a.debug, nonce = _a.nonce;
50 return (React.createElement(React.Fragment, null,
51 React.createElement("script", { id: "google-analytics-universal-script", dangerouslySetInnerHTML: { __html: exports.SETUP_SCRIPT }, nonce: nonce }),
52 React.createElement(react_import_remote_1.default, { preconnect: true, source: debug ? exports.UNIVERSAL_GA_DEBUG_SCRIPT : exports.UNIVERSAL_GA_SCRIPT, nonce: nonce, getImport: getUniversalAnalytics, onError: utilities_1.noop, onImported: this.setAnalytics })));
53 };
54 return UniversalGoogleAnalytics;
55}(React.PureComponent));
56exports.default = UniversalGoogleAnalytics;
57function getUniversalAnalytics(window) {
58 return window.ga;
59}