|
1 | 1 | (function (global, factory) {
|
2 |
| - typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : |
3 |
| - typeof define === 'function' && define.amd ? define(['exports'], factory) : |
| 2 | + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('cross-fetch/polyfill')) : |
| 3 | + typeof define === 'function' && define.amd ? define(['exports', 'cross-fetch/polyfill'], factory) : |
4 | 4 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.freenalytics = {}));
|
5 | 5 | })(this, (function (exports) { 'use strict';
|
6 | 6 |
|
7 |
| - var testing = function () { |
8 |
| - console.log('hello'); |
9 |
| - }; |
| 7 | + /*! ***************************************************************************** |
| 8 | + Copyright (c) Microsoft Corporation. |
| 9 | +
|
| 10 | + Permission to use, copy, modify, and/or distribute this software for any |
| 11 | + purpose with or without fee is hereby granted. |
| 12 | +
|
| 13 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 14 | + REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 15 | + AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 16 | + INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 17 | + LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 18 | + OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 19 | + PERFORMANCE OF THIS SOFTWARE. |
| 20 | + ***************************************************************************** */ |
| 21 | + |
| 22 | + function __awaiter(thisArg, _arguments, P, generator) { |
| 23 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 24 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 25 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 26 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 27 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 28 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + function __generator(thisArg, body) { |
| 33 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 34 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 35 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 36 | + function step(op) { |
| 37 | + if (f) throw new TypeError("Generator is already executing."); |
| 38 | + while (_) try { |
| 39 | + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; |
| 40 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 41 | + switch (op[0]) { |
| 42 | + case 0: case 1: t = op; break; |
| 43 | + case 4: _.label++; return { value: op[1], done: false }; |
| 44 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 45 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 46 | + default: |
| 47 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 48 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 49 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 50 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 51 | + if (t[2]) _.ops.pop(); |
| 52 | + _.trys.pop(); continue; |
| 53 | + } |
| 54 | + op = body.call(thisArg, _); |
| 55 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 56 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 57 | + } |
| 58 | + } |
10 | 59 |
|
11 |
| - exports.testing = testing; |
| 60 | + var IP_API_URL = 'https://api.ipify.org/?format=json'; |
| 61 | + var GEO_API_URL = 'https://reallyfreegeoip.org/json'; |
| 62 | + var getPublicIp = function () { return __awaiter(void 0, void 0, void 0, function () { |
| 63 | + var response, json; |
| 64 | + return __generator(this, function (_a) { |
| 65 | + switch (_a.label) { |
| 66 | + case 0: |
| 67 | + _a.trys.push([0, 3, , 4]); |
| 68 | + return [4 /*yield*/, fetch(IP_API_URL)]; |
| 69 | + case 1: |
| 70 | + response = _a.sent(); |
| 71 | + return [4 /*yield*/, response.json()]; |
| 72 | + case 2: |
| 73 | + json = _a.sent(); |
| 74 | + return [2 /*return*/, json.ip]; |
| 75 | + case 3: |
| 76 | + _a.sent(); |
| 77 | + return [2 /*return*/, '']; |
| 78 | + case 4: return [2 /*return*/]; |
| 79 | + } |
| 80 | + }); |
| 81 | + }); }; |
| 82 | + var getLocationForIp = function (ip) { return __awaiter(void 0, void 0, void 0, function () { |
| 83 | + var response, json; |
| 84 | + return __generator(this, function (_a) { |
| 85 | + switch (_a.label) { |
| 86 | + case 0: |
| 87 | + _a.trys.push([0, 3, , 4]); |
| 88 | + return [4 /*yield*/, fetch("".concat(GEO_API_URL, "/").concat(ip))]; |
| 89 | + case 1: |
| 90 | + response = _a.sent(); |
| 91 | + return [4 /*yield*/, response.json()]; |
| 92 | + case 2: |
| 93 | + json = _a.sent(); |
| 94 | + return [2 /*return*/, "".concat(json.city, ", ").concat(json.country_name)]; |
| 95 | + case 3: |
| 96 | + _a.sent(); |
| 97 | + return [2 /*return*/, 'N/A']; |
| 98 | + case 4: return [2 /*return*/]; |
| 99 | + } |
| 100 | + }); |
| 101 | + }); }; |
| 102 | + |
| 103 | + var PageHandler = /** @class */ (function () { |
| 104 | + function PageHandler(client) { |
| 105 | + this.client = client; |
| 106 | + this.visitTimestamp = Date.now(); |
| 107 | + this.numOfClicks = 0; |
| 108 | + this.scrolled = false; |
| 109 | + } |
| 110 | + PageHandler.prototype.registerEvents = function () { |
| 111 | + window.addEventListener('load', this.handleLoad.bind(this)); |
| 112 | + window.addEventListener('click', this.handleClick.bind(this)); |
| 113 | + window.addEventListener('beforeunload', this.handleBeforeUnload.bind(this)); |
| 114 | + window.addEventListener('wheel', this.handleScroll.bind(this), { once: true }); |
| 115 | + }; |
| 116 | + PageHandler.prototype.handleLoad = function () { |
| 117 | + return __awaiter(this, void 0, void 0, function () { |
| 118 | + var ip, location, _a; |
| 119 | + return __generator(this, function (_b) { |
| 120 | + switch (_b.label) { |
| 121 | + case 0: return [4 /*yield*/, getPublicIp()]; |
| 122 | + case 1: |
| 123 | + ip = _b.sent(); |
| 124 | + if (!ip) return [3 /*break*/, 3]; |
| 125 | + return [4 /*yield*/, getLocationForIp(ip)]; |
| 126 | + case 2: |
| 127 | + _a = _b.sent(); |
| 128 | + return [3 /*break*/, 4]; |
| 129 | + case 3: |
| 130 | + _a = 'N/A'; |
| 131 | + _b.label = 4; |
| 132 | + case 4: |
| 133 | + location = _a; |
| 134 | + return [2 /*return*/, this.client.postPayload({ |
| 135 | + page_title: document.title, |
| 136 | + url_route: window.location.pathname, |
| 137 | + user_first_visit: !document.referrer, |
| 138 | + user_location: location, |
| 139 | + referrer: document.referrer |
| 140 | + })]; |
| 141 | + } |
| 142 | + }); |
| 143 | + }); |
| 144 | + }; |
| 145 | + PageHandler.prototype.handleClick = function (event) { |
| 146 | + var _a, _b, _c; |
| 147 | + this.numOfClicks++; |
| 148 | + var element = event.composedPath()[0]; |
| 149 | + return this.client.postPayload({ |
| 150 | + element_clicked: { |
| 151 | + url_route: window.location.pathname, |
| 152 | + tag_name: (_a = element === null || element === void 0 ? void 0 : element.localName) !== null && _a !== void 0 ? _a : 'unknown', |
| 153 | + class_name: (_b = element === null || element === void 0 ? void 0 : element.className) !== null && _b !== void 0 ? _b : '', |
| 154 | + id: (_c = element === null || element === void 0 ? void 0 : element.id) !== null && _c !== void 0 ? _c : '', |
| 155 | + page_x: event.pageX, |
| 156 | + page_y: event.pageY, |
| 157 | + client_x: event.clientX, |
| 158 | + client_y: event.clientY |
| 159 | + } |
| 160 | + }); |
| 161 | + }; |
| 162 | + PageHandler.prototype.handleScroll = function () { |
| 163 | + this.scrolled = true; |
| 164 | + }; |
| 165 | + PageHandler.prototype.handleBeforeUnload = function () { |
| 166 | + var timeInPage = (Date.now() - this.visitTimestamp) / 1000; |
| 167 | + return this.client.postPayload({ |
| 168 | + user_time_in_page: timeInPage, |
| 169 | + user_scrolled: this.scrolled, |
| 170 | + num_of_clicks: this.numOfClicks |
| 171 | + }); |
| 172 | + }; |
| 173 | + return PageHandler; |
| 174 | + }()); |
| 175 | + |
| 176 | + var Client = /** @class */ (function () { |
| 177 | + function Client(options) { |
| 178 | + Client.validateOptions(options); |
| 179 | + this.apiUrl = options.apiUrl; |
| 180 | + this.domain = options.domain; |
| 181 | + this.pageHandler = null; |
| 182 | + } |
| 183 | + Client.validateOptions = function (options) { |
| 184 | + if (!options.apiUrl) { |
| 185 | + throw new Error('options.apiUrl needs to be specified.'); |
| 186 | + } |
| 187 | + if (!options.domain) { |
| 188 | + throw new Error('options.domain needs to be specified.'); |
| 189 | + } |
| 190 | + }; |
| 191 | + Client.prototype.initialize = function () { |
| 192 | + this.pageHandler = new PageHandler(this); |
| 193 | + this.pageHandler.registerEvents(); |
| 194 | + }; |
| 195 | + Client.prototype.postPayload = function (payload) { |
| 196 | + return __awaiter(this, void 0, void 0, function () { |
| 197 | + var error_1; |
| 198 | + return __generator(this, function (_a) { |
| 199 | + switch (_a.label) { |
| 200 | + case 0: |
| 201 | + _a.trys.push([0, 2, , 3]); |
| 202 | + return [4 /*yield*/, fetch("".concat(this.apiUrl, "/applications/").concat(this.domain, "/data"), { |
| 203 | + method: 'POST', |
| 204 | + headers: { |
| 205 | + Accept: 'application/json', |
| 206 | + 'Content-Type': 'application/json' |
| 207 | + }, |
| 208 | + body: JSON.stringify(payload), |
| 209 | + keepalive: true |
| 210 | + })]; |
| 211 | + case 1: |
| 212 | + _a.sent(); |
| 213 | + return [3 /*break*/, 3]; |
| 214 | + case 2: |
| 215 | + error_1 = _a.sent(); |
| 216 | + console.error(error_1); |
| 217 | + return [3 /*break*/, 3]; |
| 218 | + case 3: return [2 /*return*/]; |
| 219 | + } |
| 220 | + }); |
| 221 | + }); |
| 222 | + }; |
| 223 | + return Client; |
| 224 | + }()); |
| 225 | + |
| 226 | + exports.Client = Client; |
| 227 | + exports.PageHandler = PageHandler; |
12 | 228 |
|
13 | 229 | }));
|
0 commit comments