|
@@ -1,5 +1,5 @@
|
|
// index.js
|
|
// index.js
|
|
-(function (root, factory) {
|
|
|
|
|
|
+(function (factory) {
|
|
if (typeof define === 'function' && define.amd) {
|
|
if (typeof define === 'function' && define.amd) {
|
|
// AMD
|
|
// AMD
|
|
define([], factory);
|
|
define([], factory);
|
|
@@ -7,12 +7,17 @@
|
|
// CommonJS
|
|
// CommonJS
|
|
module.exports = factory();
|
|
module.exports = factory();
|
|
} else {
|
|
} else {
|
|
- // 全局变量
|
|
|
|
- root.UtmTracker = factory();
|
|
|
|
|
|
+ // 浏览器全局变量
|
|
|
|
+ var globalObj = typeof globalThis !== 'undefined' ? globalThis
|
|
|
|
+ : typeof self !== 'undefined' ? self
|
|
|
|
+ : typeof window !== 'undefined' ? window
|
|
|
|
+ : typeof global !== 'undefined' ? global
|
|
|
|
+ : this;
|
|
|
|
+ globalObj.UtmTracker = factory();
|
|
}
|
|
}
|
|
-}(this, function () {
|
|
|
|
|
|
+}(function () {
|
|
'use strict';
|
|
'use strict';
|
|
-
|
|
|
|
|
|
+
|
|
// 工具函数
|
|
// 工具函数
|
|
function getUrlParam(name) {
|
|
function getUrlParam(name) {
|
|
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
|
|
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
|
|
@@ -20,7 +25,7 @@
|
|
if (r != null) return decodeURIComponent(r[2]);
|
|
if (r != null) return decodeURIComponent(r[2]);
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
// 获取浏览器信息
|
|
// 获取浏览器信息
|
|
function getBrowserInfo() {
|
|
function getBrowserInfo() {
|
|
var ua = navigator.userAgent;
|
|
var ua = navigator.userAgent;
|
|
@@ -86,14 +91,14 @@
|
|
osVersion: osVersion
|
|
osVersion: osVersion
|
|
};
|
|
};
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
|
|
|
|
//获取上一级url
|
|
//获取上一级url
|
|
function getPreviousUrl() {
|
|
function getPreviousUrl() {
|
|
let referrer = document.referrer;
|
|
let referrer = document.referrer;
|
|
if (referrer) {
|
|
if (referrer) {
|
|
return referrer;
|
|
return referrer;
|
|
- }
|
|
|
|
|
|
+ }
|
|
// else if (window.history.length > 1) {
|
|
// else if (window.history.length > 1) {
|
|
// // 如果有历史记录
|
|
// // 如果有历史记录
|
|
// window.history.back();
|
|
// window.history.back();
|
|
@@ -116,22 +121,22 @@
|
|
this.send();
|
|
this.send();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
// 获取UTM参数
|
|
// 获取UTM参数
|
|
- UtmTracker.prototype.getParams = function() {
|
|
|
|
|
|
+ UtmTracker.prototype.getParams = function () {
|
|
const browser = getBrowserInfo()
|
|
const browser = getBrowserInfo()
|
|
const params = {
|
|
const params = {
|
|
- utm_source: getUrlParam('utm_source') || '',
|
|
|
|
- utm_medium: getUrlParam('utm_medium') || '',
|
|
|
|
- utm_campaign: getUrlParam('utm_campaign') || '',
|
|
|
|
- utm_term: getUrlParam('utm_term') || '',
|
|
|
|
- utm_content: getUrlParam('utm_content') || '',
|
|
|
|
|
|
+ utmSource: getUrlParam('utm_source') || '',
|
|
|
|
+ utmMedium: getUrlParam('utm_medium') || '',
|
|
|
|
+ utmCampaign: getUrlParam('utm_campaign') || '',
|
|
|
|
+ utmTerm: getUrlParam('utm_term') || '',
|
|
|
|
+ utmContent: getUrlParam('utm_content') || '',
|
|
referrer: getPreviousUrl() || '',
|
|
referrer: getPreviousUrl() || '',
|
|
timestamp: new Date().toISOString(),
|
|
timestamp: new Date().toISOString(),
|
|
url: window.location.href,
|
|
url: window.location.href,
|
|
isMobile: browser.isMobile,
|
|
isMobile: browser.isMobile,
|
|
browser: browser.browser,
|
|
browser: browser.browser,
|
|
- userAgent: browser.ua,
|
|
|
|
|
|
+ userAgent: browser.userAgent,
|
|
osType: browser.osType,
|
|
osType: browser.osType,
|
|
osVersion: browser.osVersion
|
|
osVersion: browser.osVersion
|
|
};
|
|
};
|
|
@@ -150,8 +155,8 @@
|
|
UtmTracker.prototype.send = function (data) {
|
|
UtmTracker.prototype.send = function (data) {
|
|
const cfg = this.config || {};
|
|
const cfg = this.config || {};
|
|
const payload = data || this.getParams();
|
|
const payload = data || this.getParams();
|
|
- console.log(payload);
|
|
|
|
-
|
|
|
|
|
|
+ console.log(cfg);
|
|
|
|
+
|
|
if (!cfg.reportUrl) return;
|
|
if (!cfg.reportUrl) return;
|
|
fetch(cfg.reportUrl, {
|
|
fetch(cfg.reportUrl, {
|
|
method: 'POST',
|
|
method: 'POST',
|
|
@@ -162,51 +167,59 @@
|
|
// 假设res为Response对象,需要解析json
|
|
// 假设res为Response对象,需要解析json
|
|
if (typeof res.json === 'function') {
|
|
if (typeof res.json === 'function') {
|
|
res.json().then(data => {
|
|
res.json().then(data => {
|
|
- if (data.downloadUrl) {
|
|
|
|
|
|
+ console.log(data);
|
|
|
|
|
|
- // 倒计时弹窗
|
|
|
|
- let countdown = 3;
|
|
|
|
- const countdownMask = document.createElement('div');
|
|
|
|
- countdownMask.style.position = 'fixed';
|
|
|
|
- countdownMask.style.top = 0;
|
|
|
|
- countdownMask.style.left = 0;
|
|
|
|
- countdownMask.style.width = '100vw';
|
|
|
|
- countdownMask.style.height = '100vh';
|
|
|
|
- countdownMask.style.background = 'rgba(0,0,0,0.5)';
|
|
|
|
- countdownMask.style.zIndex = 9999;
|
|
|
|
- countdownMask.style.display = 'flex';
|
|
|
|
- countdownMask.style.alignItems = 'center';
|
|
|
|
- countdownMask.style.justifyContent = 'center';
|
|
|
|
|
|
+ if (data.data.downloadUrl) {
|
|
|
|
+ console.log(111);
|
|
|
|
|
|
- const countdownModal = document.createElement('div');
|
|
|
|
- countdownModal.style.background = '#fff';
|
|
|
|
- countdownModal.style.borderRadius = '8px';
|
|
|
|
- countdownModal.style.padding = '32px 48px';
|
|
|
|
- countdownModal.style.fontSize = '20px';
|
|
|
|
- countdownModal.style.boxShadow = '0 2px 16px rgba(0,0,0,0.2)';
|
|
|
|
- countdownModal.style.display = 'flex';
|
|
|
|
- countdownModal.style.flexDirection = 'column';
|
|
|
|
- countdownModal.style.alignItems = 'center';
|
|
|
|
|
|
+ if (data.data.type === 'link') {
|
|
|
|
+ // 倒计时弹窗
|
|
|
|
+ let countdown = 3;
|
|
|
|
+ const countdownMask = document.createElement('div');
|
|
|
|
+ countdownMask.style.position = 'fixed';
|
|
|
|
+ countdownMask.style.top = 0;
|
|
|
|
+ countdownMask.style.left = 0;
|
|
|
|
+ countdownMask.style.width = '100vw';
|
|
|
|
+ countdownMask.style.height = '100vh';
|
|
|
|
+ countdownMask.style.background = 'rgba(0,0,0,0.5)';
|
|
|
|
+ countdownMask.style.zIndex = 9999;
|
|
|
|
+ countdownMask.style.display = 'flex';
|
|
|
|
+ countdownMask.style.alignItems = 'center';
|
|
|
|
+ countdownMask.style.justifyContent = 'center';
|
|
|
|
|
|
- const text = document.createElement('div');
|
|
|
|
- text.innerText = `即将跳转,${countdown}秒...`;
|
|
|
|
- countdownModal.appendChild(text);
|
|
|
|
- countdownMask.appendChild(countdownModal);
|
|
|
|
- document.body.appendChild(countdownMask);
|
|
|
|
|
|
+ const countdownModal = document.createElement('div');
|
|
|
|
+ countdownModal.style.background = '#fff';
|
|
|
|
+ countdownModal.style.borderRadius = '8px';
|
|
|
|
+ countdownModal.style.padding = '32px 48px';
|
|
|
|
+ countdownModal.style.fontSize = '20px';
|
|
|
|
+ countdownModal.style.boxShadow = '0 2px 16px rgba(0,0,0,0.2)';
|
|
|
|
+ countdownModal.style.display = 'flex';
|
|
|
|
+ countdownModal.style.flexDirection = 'column';
|
|
|
|
+ countdownModal.style.alignItems = 'center';
|
|
|
|
|
|
- const timer = setInterval(() => {
|
|
|
|
- countdown--;
|
|
|
|
|
|
+ const text = document.createElement('div');
|
|
text.innerText = `即将跳转,${countdown}秒...`;
|
|
text.innerText = `即将跳转,${countdown}秒...`;
|
|
- if (countdown <= 0) {
|
|
|
|
- clearInterval(timer);
|
|
|
|
- document.body.removeChild(countdownMask);
|
|
|
|
- // 跳转
|
|
|
|
- console.log(data.downloadUrl);
|
|
|
|
- window.location.href = data.downloadUrl
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- }, 1000);
|
|
|
|
- return
|
|
|
|
|
|
+ countdownModal.appendChild(text);
|
|
|
|
+ countdownMask.appendChild(countdownModal);
|
|
|
|
+ document.body.appendChild(countdownMask);
|
|
|
|
+
|
|
|
|
+ const timer = setInterval(() => {
|
|
|
|
+ countdown--;
|
|
|
|
+ text.innerText = `即将跳转,${countdown}秒...`;
|
|
|
|
+ if (countdown <= 0) {
|
|
|
|
+ clearInterval(timer);
|
|
|
|
+ document.body.removeChild(countdownMask);
|
|
|
|
+ // 跳转
|
|
|
|
+ console.log(data.data.downloadUrl);
|
|
|
|
+ window.location.href = data.data.downloadUrl
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }, 1000);
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
// 创建遮罩
|
|
// 创建遮罩
|
|
const mask = document.createElement('div');
|
|
const mask = document.createElement('div');
|
|
mask.style.position = 'fixed';
|
|
mask.style.position = 'fixed';
|
|
@@ -252,27 +265,27 @@
|
|
|
|
|
|
// 内容区
|
|
// 内容区
|
|
let contentEl;
|
|
let contentEl;
|
|
- if (data.type === 'video') {
|
|
|
|
|
|
+ if (data.data.type === 'video') {
|
|
contentEl = document.createElement('video');
|
|
contentEl = document.createElement('video');
|
|
- contentEl.src = data.downloadUrl;
|
|
|
|
|
|
+ contentEl.src = data.data.downloadUrl;
|
|
contentEl.controls = true;
|
|
contentEl.controls = true;
|
|
contentEl.autoplay = true;
|
|
contentEl.autoplay = true;
|
|
contentEl.style.maxWidth = '80vw';
|
|
contentEl.style.maxWidth = '80vw';
|
|
contentEl.style.maxHeight = '70vh';
|
|
contentEl.style.maxHeight = '70vh';
|
|
- } else if (data.type === 'audio') {
|
|
|
|
|
|
+ } else if (data.data.type === 'audio') {
|
|
contentEl = document.createElement('audio');
|
|
contentEl = document.createElement('audio');
|
|
- contentEl.src = data.downloadUrl;
|
|
|
|
|
|
+ contentEl.src = data.data.downloadUrl;
|
|
contentEl.controls = true;
|
|
contentEl.controls = true;
|
|
contentEl.autoplay = true;
|
|
contentEl.autoplay = true;
|
|
contentEl.style.width = '100%';
|
|
contentEl.style.width = '100%';
|
|
- } else if (data.type === 'image') {
|
|
|
|
|
|
+ } else if (data.data.type === 'image') {
|
|
contentEl = document.createElement('img');
|
|
contentEl = document.createElement('img');
|
|
- contentEl.src = data.downloadUrl;
|
|
|
|
|
|
+ contentEl.src = data.data.downloadUrl;
|
|
contentEl.style.maxWidth = '80vw';
|
|
contentEl.style.maxWidth = '80vw';
|
|
contentEl.style.maxHeight = '70vh';
|
|
contentEl.style.maxHeight = '70vh';
|
|
} else {
|
|
} else {
|
|
contentEl = document.createElement('a');
|
|
contentEl = document.createElement('a');
|
|
- contentEl.href = data.downloadUrl;
|
|
|
|
|
|
+ contentEl.href = data.data.downloadUrl;
|
|
contentEl.innerText = '下载文件';
|
|
contentEl.innerText = '下载文件';
|
|
contentEl.target = '_blank';
|
|
contentEl.target = '_blank';
|
|
}
|
|
}
|
|
@@ -288,7 +301,7 @@
|
|
|
|
|
|
});
|
|
});
|
|
};
|
|
};
|
|
-
|
|
|
|
|
|
+
|
|
// 静态方法:快速获取(无需实例化)
|
|
// 静态方法:快速获取(无需实例化)
|
|
UtmTracker.get = function (config) {
|
|
UtmTracker.get = function (config) {
|
|
const tracker = new UtmTracker(config);
|
|
const tracker = new UtmTracker(config);
|
|
@@ -298,6 +311,12 @@
|
|
}
|
|
}
|
|
return params;
|
|
return params;
|
|
};
|
|
};
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ // 新增静态send方法
|
|
|
|
+ UtmTracker.send = function (data, config) {
|
|
|
|
+ const tracker = new UtmTracker(config);
|
|
|
|
+ return tracker.send(data);
|
|
|
|
+ };
|
|
|
|
+
|
|
return UtmTracker;
|
|
return UtmTracker;
|
|
}));
|
|
}));
|