Comparing version 1.10.1 to 1.11.0
{ | ||
"name": "zmp-ga4", | ||
"version": "1.10.1", | ||
"version": "1.11.0", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Nguyễn Hồng Phát", |
@@ -53,7 +53,2 @@ "use strict"; | ||
function AdaptiveAnalytics(propertyId, apiSecretKey, options) { | ||
if (options === void 0) { options = { | ||
useMeasurementProtocolWhen: function () { | ||
return ["http:", "https:"].indexOf(window.location.protocol) === -1; | ||
} | ||
}; } | ||
var _this = this; | ||
@@ -64,14 +59,15 @@ var _a; | ||
this.options = options; | ||
this.userId = ''; | ||
this.sessionId = ''; | ||
this.userId = ""; | ||
this.sessionId = ""; | ||
this.startTime = performance.now(); | ||
this.eventQueue = []; | ||
this.timeout = 0; | ||
this.referer = ''; | ||
this.screenResolution = ''; | ||
this.viewportSize = ''; | ||
this.referer = ""; | ||
this.screenResolution = ""; | ||
this.viewportSize = ""; | ||
this.collect = function () { | ||
var _a; | ||
if (_this.userId) { | ||
if (_this.eventQueue.length) { | ||
fetch("https://www.google-analytics.com/mp/collect?measurement_id=".concat(_this.propertyId, "&api_secret=").concat(_this.apiSecretKey), { | ||
fetch("https://www.google-analytics.com/".concat(((_a = _this.options) === null || _a === void 0 ? void 0 : _a.debug) ? "debug/" : "", "mp/collect?measurement_id=").concat(_this.propertyId, "&api_secret=").concat(_this.apiSecretKey), { | ||
method: "POST", | ||
@@ -103,2 +99,3 @@ body: JSON.stringify({ | ||
setTimeout(function () { | ||
var _a; | ||
_this.trackEvent("page_view", { | ||
@@ -109,3 +106,3 @@ page_path: pathname_1, | ||
}); | ||
if (_this.options.onPageView) { | ||
if ((_a = _this.options) === null || _a === void 0 ? void 0 : _a.onPageView) { | ||
_this.options.onPageView(pathname_1); | ||
@@ -133,2 +130,4 @@ } | ||
window.addEventListener("replacestate", handleLocationChange); | ||
// Track first vist | ||
this.trackEvent("first_visit", {}); | ||
} | ||
@@ -142,3 +141,3 @@ else { | ||
window.gtag("js", new Date()); | ||
window.gtag("config", propertyId, options.gtagConfig); | ||
window.gtag("config", propertyId, options === null || options === void 0 ? void 0 : options.gtagConfig); | ||
} | ||
@@ -170,3 +169,6 @@ } | ||
AdaptiveAnalytics.prototype.isMeasurementProtocolRequired = function () { | ||
return this.options.useMeasurementProtocolWhen(); | ||
if (this.options) { | ||
return this.options.useMeasurementProtocolWhen(); | ||
} | ||
return true; | ||
}; | ||
@@ -173,0 +175,0 @@ AdaptiveAnalytics.prototype.loadScript = function (url) { |
@@ -16,4 +16,4 @@ declare global { | ||
export class AdaptiveAnalytics { | ||
private userId = ''; | ||
private sessionId = ''; | ||
private userId = ""; | ||
private sessionId = ""; | ||
private startTime = performance.now(); | ||
@@ -25,5 +25,5 @@ private eventQueue: { | ||
private timeout = 0; | ||
private referer = ''; | ||
private screenResolution = ''; | ||
private viewportSize = ''; | ||
private referer = ""; | ||
private screenResolution = ""; | ||
private viewportSize = ""; | ||
@@ -33,10 +33,8 @@ constructor( | ||
private apiSecretKey: string, | ||
private options: { | ||
private options?: { | ||
useMeasurementProtocolWhen: () => boolean; | ||
gtagConfig?: Record<string, unknown>; | ||
onPageView?: (pagePath: string) => void; | ||
} = { | ||
useMeasurementProtocolWhen: () => | ||
["http:", "https:"].indexOf(window.location.protocol) === -1, | ||
} | ||
debug?: boolean; | ||
} | ||
) { | ||
@@ -62,3 +60,3 @@ if (this.isMeasurementProtocolRequired()) { | ||
}); | ||
if (this.options.onPageView) { | ||
if (this.options?.onPageView) { | ||
this.options.onPageView(pathname); | ||
@@ -75,3 +73,6 @@ } | ||
history.pushState = function () { | ||
originalPushState.apply(this, arguments as unknown as Parameters<typeof history.pushState>); | ||
originalPushState.apply( | ||
this, | ||
arguments as unknown as Parameters<typeof history.pushState> | ||
); | ||
window.dispatchEvent(new Event("pushstate")); | ||
@@ -82,3 +83,6 @@ }; | ||
history.replaceState = function () { | ||
originalReplaceState.apply(this, arguments as unknown as Parameters<typeof history.replaceState>); | ||
originalReplaceState.apply( | ||
this, | ||
arguments as unknown as Parameters<typeof history.replaceState> | ||
); | ||
window.dispatchEvent(new Event("replacestate")); | ||
@@ -90,2 +94,5 @@ }; | ||
window.addEventListener("replacestate", handleLocationChange); | ||
// Track first vist | ||
this.trackEvent("first_visit", {}); | ||
} else { | ||
@@ -100,3 +107,3 @@ this.loadScript( | ||
window.gtag("js", new Date()); | ||
window.gtag("config", propertyId, options.gtagConfig); | ||
window.gtag("config", propertyId, options?.gtagConfig); | ||
} | ||
@@ -129,3 +136,6 @@ } | ||
private isMeasurementProtocolRequired() { | ||
return this.options.useMeasurementProtocolWhen(); | ||
if (this.options) { | ||
return this.options.useMeasurementProtocolWhen(); | ||
} | ||
return true; | ||
} | ||
@@ -161,3 +171,7 @@ | ||
fetch( | ||
`https://www.google-analytics.com/mp/collect?measurement_id=${this.propertyId}&api_secret=${this.apiSecretKey}`, | ||
`https://www.google-analytics.com/${ | ||
this.options?.debug ? "debug/" : "" | ||
}mp/collect?measurement_id=${this.propertyId}&api_secret=${ | ||
this.apiSecretKey | ||
}`, | ||
{ | ||
@@ -181,5 +195,3 @@ method: "POST", | ||
const endTime = performance.now(); | ||
const engagement_time_msec = String( | ||
Math.round(endTime - this.startTime) | ||
); | ||
const engagement_time_msec = String(Math.round(endTime - this.startTime)); | ||
this.startTime = endTime; | ||
@@ -186,0 +198,0 @@ this.eventQueue.unshift({ |
19239
408