UNPKG

6.78 kBJavaScriptView Raw
1"use strict";
2// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3// SPDX-License-Identifier: Apache-2.0
4Object.defineProperty(exports, "__esModule", { value: true });
5var tslib_1 = require("tslib");
6var MethodEmbed_1 = require("../utils/MethodEmbed");
7var core_1 = require("@aws-amplify/core");
8var logger = new core_1.ConsoleLogger('PageViewTracker');
9var PREV_URL_KEY = 'aws-amplify-analytics-prevUrl';
10var getUrl = function () {
11 if (!core_1.browserOrNode().isBrowser)
12 return '';
13 else
14 return window.location.origin + window.location.pathname;
15};
16var defaultOpts = {
17 enable: false,
18 provider: 'AWSPinpoint',
19 getUrl: getUrl,
20};
21var PageViewTracker = /** @class */ (function () {
22 function PageViewTracker(tracker, opts) {
23 logger.debug('initialize pageview tracker with opts', opts);
24 this._config = Object.assign({}, defaultOpts, opts);
25 this._tracker = tracker;
26 this._hasEnabled = false;
27 this._trackFunc = this._trackFunc.bind(this);
28 if (this._config.type === 'SPA') {
29 this._pageViewTrackSPA();
30 }
31 else {
32 this._pageViewTrackDefault();
33 }
34 }
35 PageViewTracker.prototype.configure = function (opts) {
36 Object.assign(this._config, opts);
37 // if spa, need to remove those listeners if disabled
38 if (this._config.type === 'SPA') {
39 this._pageViewTrackSPA();
40 }
41 return this._config;
42 };
43 PageViewTracker.prototype._isSameUrl = function () {
44 var prevUrl = sessionStorage.getItem(PREV_URL_KEY);
45 var curUrl = this._config.getUrl();
46 if (prevUrl === curUrl) {
47 logger.debug('the url is same');
48 return true;
49 }
50 else
51 return false;
52 };
53 PageViewTracker.prototype._pageViewTrackDefault = function () {
54 return tslib_1.__awaiter(this, void 0, void 0, function () {
55 var url, customAttrs, _a, attributes;
56 return tslib_1.__generator(this, function (_b) {
57 switch (_b.label) {
58 case 0:
59 if (!core_1.browserOrNode().isBrowser ||
60 !window.addEventListener ||
61 !window.sessionStorage) {
62 logger.debug('not in the supported web enviroment');
63 return [2 /*return*/];
64 }
65 url = this._config.getUrl();
66 if (!(typeof this._config.attributes === 'function')) return [3 /*break*/, 2];
67 return [4 /*yield*/, this._config.attributes()];
68 case 1:
69 _a = _b.sent();
70 return [3 /*break*/, 3];
71 case 2:
72 _a = this._config.attributes;
73 _b.label = 3;
74 case 3:
75 customAttrs = _a;
76 attributes = Object.assign({
77 url: url,
78 }, customAttrs);
79 if (this._config.enable && !this._isSameUrl()) {
80 this._tracker({
81 name: this._config.eventName || 'pageView',
82 attributes: attributes,
83 }, this._config.provider).catch(function (e) {
84 logger.debug('Failed to record the page view event', e);
85 });
86 sessionStorage.setItem(PREV_URL_KEY, url);
87 }
88 return [2 /*return*/];
89 }
90 });
91 });
92 };
93 PageViewTracker.prototype._trackFunc = function () {
94 return tslib_1.__awaiter(this, void 0, void 0, function () {
95 var url, customAttrs, _a, attributes;
96 return tslib_1.__generator(this, function (_b) {
97 switch (_b.label) {
98 case 0:
99 if (!core_1.browserOrNode().isBrowser ||
100 !window.addEventListener ||
101 !history.pushState ||
102 !window.sessionStorage) {
103 logger.debug('not in the supported web enviroment');
104 return [2 /*return*/];
105 }
106 url = this._config.getUrl();
107 if (!(typeof this._config.attributes === 'function')) return [3 /*break*/, 2];
108 return [4 /*yield*/, this._config.attributes()];
109 case 1:
110 _a = _b.sent();
111 return [3 /*break*/, 3];
112 case 2:
113 _a = this._config.attributes;
114 _b.label = 3;
115 case 3:
116 customAttrs = _a;
117 attributes = Object.assign({
118 url: url,
119 }, customAttrs);
120 if (!this._isSameUrl()) {
121 this._tracker({
122 name: this._config.eventName || 'pageView',
123 attributes: attributes,
124 }, this._config.provider).catch(function (e) {
125 logger.debug('Failed to record the page view event', e);
126 });
127 sessionStorage.setItem(PREV_URL_KEY, url);
128 }
129 return [2 /*return*/];
130 }
131 });
132 });
133 };
134 PageViewTracker.prototype._pageViewTrackSPA = function () {
135 if (!core_1.browserOrNode().isBrowser ||
136 !window.addEventListener ||
137 !history.pushState) {
138 logger.debug('not in the supported web enviroment');
139 return;
140 }
141 if (this._config.enable && !this._hasEnabled) {
142 MethodEmbed_1.MethodEmbed.add(history, 'pushState', this._trackFunc);
143 MethodEmbed_1.MethodEmbed.add(history, 'replaceState', this._trackFunc);
144 window.addEventListener('popstate', this._trackFunc);
145 this._trackFunc();
146 this._hasEnabled = true;
147 }
148 else {
149 MethodEmbed_1.MethodEmbed.remove(history, 'pushState');
150 MethodEmbed_1.MethodEmbed.remove(history, 'replaceState');
151 window.removeEventListener('popstate', this._trackFunc);
152 this._hasEnabled = false;
153 }
154 };
155 return PageViewTracker;
156}());
157exports.PageViewTracker = PageViewTracker;
158//# sourceMappingURL=PageViewTracker.js.map
\No newline at end of file