@github/hydro-analytics-client 2.3.1 → 2.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analytics-client.d.ts +16 -0
- package/dist/analytics-client.js +65 -0
- package/dist/client-id.d.ts +1 -0
- package/dist/client-id.js +50 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/marketing-context.d.ts +10 -0
- package/dist/marketing-context.js +17 -0
- package/dist/meta-helpers.d.ts +2 -0
- package/dist/meta-helpers.js +22 -0
- package/dist/request-context.d.ts +11 -0
- package/dist/request-context.js +69 -0
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare type Context = Record<string, string>;
|
|
2
|
+
export interface AnalyticsClientOptions {
|
|
3
|
+
collectorUrl: string;
|
|
4
|
+
clientId?: string;
|
|
5
|
+
baseContext?: Context;
|
|
6
|
+
}
|
|
7
|
+
export declare class AnalyticsClient {
|
|
8
|
+
readonly options: AnalyticsClientOptions;
|
|
9
|
+
constructor(options: AnalyticsClientOptions);
|
|
10
|
+
get collectorUrl(): string;
|
|
11
|
+
get clientId(): string;
|
|
12
|
+
private createEvent;
|
|
13
|
+
sendPageView(context?: Context): void;
|
|
14
|
+
sendEvent(type: string, context: Context): void;
|
|
15
|
+
private send;
|
|
16
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { getRequestContext } from './request-context.js';
|
|
2
|
+
import { getMarketingContext } from './marketing-context.js';
|
|
3
|
+
import { getOrCreateClientId } from './client-id.js';
|
|
4
|
+
export class AnalyticsClient {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.options = options;
|
|
7
|
+
}
|
|
8
|
+
get collectorUrl() {
|
|
9
|
+
return this.options.collectorUrl;
|
|
10
|
+
}
|
|
11
|
+
get clientId() {
|
|
12
|
+
if (this.options.clientId) {
|
|
13
|
+
return this.options.clientId;
|
|
14
|
+
}
|
|
15
|
+
return getOrCreateClientId();
|
|
16
|
+
}
|
|
17
|
+
createEvent(context) {
|
|
18
|
+
return {
|
|
19
|
+
page: location.href,
|
|
20
|
+
title: document.title,
|
|
21
|
+
context: {
|
|
22
|
+
...this.options.baseContext,
|
|
23
|
+
...getMarketingContext(),
|
|
24
|
+
...context
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
sendPageView(context) {
|
|
29
|
+
const pageView = this.createEvent(context);
|
|
30
|
+
this.send({ page_views: [pageView] });
|
|
31
|
+
}
|
|
32
|
+
sendEvent(type, context) {
|
|
33
|
+
const event = {
|
|
34
|
+
...this.createEvent(context),
|
|
35
|
+
type
|
|
36
|
+
};
|
|
37
|
+
this.send({ events: [event] });
|
|
38
|
+
}
|
|
39
|
+
send({ page_views, events }) {
|
|
40
|
+
const payload = {
|
|
41
|
+
client_id: this.clientId,
|
|
42
|
+
page_views,
|
|
43
|
+
events,
|
|
44
|
+
request_context: getRequestContext()
|
|
45
|
+
};
|
|
46
|
+
const body = JSON.stringify(payload);
|
|
47
|
+
try {
|
|
48
|
+
if (navigator.sendBeacon) {
|
|
49
|
+
navigator.sendBeacon(this.collectorUrl, body);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
}
|
|
55
|
+
fetch(this.collectorUrl, {
|
|
56
|
+
method: 'POST',
|
|
57
|
+
cache: 'no-cache',
|
|
58
|
+
headers: {
|
|
59
|
+
'Content-Type': 'application/json'
|
|
60
|
+
},
|
|
61
|
+
body,
|
|
62
|
+
keepalive: false
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getOrCreateClientId(): string;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
let fallbackClientId;
|
|
2
|
+
function generateClientId() {
|
|
3
|
+
return `${Math.round(Math.random() * (Math.pow(2, 31) - 1))}.${Math.round(Date.now() / 1000)}`;
|
|
4
|
+
}
|
|
5
|
+
function setClientIdCookie(clientId) {
|
|
6
|
+
const value = `GH1.1.${clientId}`;
|
|
7
|
+
const now = Date.now();
|
|
8
|
+
const expires = new Date(now + 1 * 365 * 86400 * 1000).toUTCString();
|
|
9
|
+
let { domain } = document;
|
|
10
|
+
if (domain.endsWith('.github.com')) {
|
|
11
|
+
domain = 'github.com';
|
|
12
|
+
}
|
|
13
|
+
document.cookie = `_octo=${value}; expires=${expires}; path=/; domain=${domain}; secure; samesite=lax`;
|
|
14
|
+
}
|
|
15
|
+
function getClientIdFromCookie() {
|
|
16
|
+
let clientId;
|
|
17
|
+
const cookie = document.cookie;
|
|
18
|
+
const matches = cookie.match(/_octo=([^;]+)/g);
|
|
19
|
+
if (!matches) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
let latestVersion = [0, 0];
|
|
23
|
+
for (const match of matches) {
|
|
24
|
+
const [, value] = match.split('=');
|
|
25
|
+
const [, version, ...clientIdPortions] = value.split('.');
|
|
26
|
+
const semanticVersion = version.split('-').map(Number);
|
|
27
|
+
if (semanticVersion > latestVersion) {
|
|
28
|
+
latestVersion = semanticVersion;
|
|
29
|
+
clientId = clientIdPortions.join('.');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return clientId;
|
|
33
|
+
}
|
|
34
|
+
export function getOrCreateClientId() {
|
|
35
|
+
try {
|
|
36
|
+
const existingId = getClientIdFromCookie();
|
|
37
|
+
if (existingId) {
|
|
38
|
+
return existingId;
|
|
39
|
+
}
|
|
40
|
+
const clientId = generateClientId();
|
|
41
|
+
setClientIdCookie(clientId);
|
|
42
|
+
return clientId;
|
|
43
|
+
}
|
|
44
|
+
catch (_) {
|
|
45
|
+
if (!fallbackClientId) {
|
|
46
|
+
fallbackClientId = generateClientId();
|
|
47
|
+
}
|
|
48
|
+
return fallbackClientId;
|
|
49
|
+
}
|
|
50
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const MARKETING_PARAMS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'scid'];
|
|
2
|
+
export function getMarketingContext() {
|
|
3
|
+
const marketingContext = {};
|
|
4
|
+
try {
|
|
5
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
6
|
+
for (const [key, value] of urlParams) {
|
|
7
|
+
const normalizedKey = key.toLowerCase();
|
|
8
|
+
if (MARKETING_PARAMS.includes(normalizedKey)) {
|
|
9
|
+
marketingContext[normalizedKey] = value;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return marketingContext;
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
return {};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function getOptionsFromMeta(prefix = 'ha') {
|
|
2
|
+
let collectorUrl;
|
|
3
|
+
const baseContext = {};
|
|
4
|
+
const metaTags = document.head.querySelectorAll(`meta[name^="${prefix}-"]`);
|
|
5
|
+
for (const meta of Array.from(metaTags)) {
|
|
6
|
+
const { name: originalName, content } = meta;
|
|
7
|
+
const name = originalName.replace(`${prefix}-`, '').replace(/-/g, '_');
|
|
8
|
+
if (name === 'url') {
|
|
9
|
+
collectorUrl = content;
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
baseContext[name] = content;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
if (!collectorUrl) {
|
|
16
|
+
throw new Error(`AnalyticsClient ${prefix}-url meta tag not found`);
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
collectorUrl,
|
|
20
|
+
...(Object.keys(baseContext).length > 0 ? { baseContext } : {})
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface RequestContext {
|
|
2
|
+
referrer?: string;
|
|
3
|
+
user_agent: string;
|
|
4
|
+
screen_resolution: string;
|
|
5
|
+
browser_resolution: string;
|
|
6
|
+
browser_languages: string;
|
|
7
|
+
pixel_ratio: number;
|
|
8
|
+
timestamp: number;
|
|
9
|
+
tz_seconds: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function getRequestContext(): RequestContext;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
function getReferrer() {
|
|
2
|
+
let referrer;
|
|
3
|
+
try {
|
|
4
|
+
referrer = window.top.document.referrer;
|
|
5
|
+
}
|
|
6
|
+
catch (error) {
|
|
7
|
+
if (window.parent) {
|
|
8
|
+
try {
|
|
9
|
+
referrer = window.parent.document.referrer;
|
|
10
|
+
}
|
|
11
|
+
catch (_) {
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
if (referrer === '') {
|
|
16
|
+
referrer = document.referrer;
|
|
17
|
+
}
|
|
18
|
+
return referrer;
|
|
19
|
+
}
|
|
20
|
+
function getScreenResolution() {
|
|
21
|
+
try {
|
|
22
|
+
return `${screen.width}x${screen.height}`;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
return 'unknown';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function getBrowserResolution() {
|
|
29
|
+
let height = 0;
|
|
30
|
+
let width = 0;
|
|
31
|
+
try {
|
|
32
|
+
if (typeof window.innerWidth === 'number') {
|
|
33
|
+
width = window.innerWidth;
|
|
34
|
+
height = window.innerHeight;
|
|
35
|
+
}
|
|
36
|
+
else if (document.documentElement != null && document.documentElement.clientWidth != null) {
|
|
37
|
+
width = document.documentElement.clientWidth;
|
|
38
|
+
height = document.documentElement.clientHeight;
|
|
39
|
+
}
|
|
40
|
+
else if (document.body != null && document.body.clientWidth != null) {
|
|
41
|
+
width = document.body.clientWidth;
|
|
42
|
+
height = document.body.clientHeight;
|
|
43
|
+
}
|
|
44
|
+
return `${width}x${height}`;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
return 'unknown';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function getBrowserLanguages() {
|
|
51
|
+
if (navigator.languages) {
|
|
52
|
+
return navigator.languages.join(',');
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return navigator.language || '';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export function getRequestContext() {
|
|
59
|
+
return {
|
|
60
|
+
referrer: getReferrer(),
|
|
61
|
+
user_agent: navigator.userAgent,
|
|
62
|
+
screen_resolution: getScreenResolution(),
|
|
63
|
+
browser_resolution: getBrowserResolution(),
|
|
64
|
+
browser_languages: getBrowserLanguages(),
|
|
65
|
+
pixel_ratio: window.devicePixelRatio,
|
|
66
|
+
timestamp: Date.now(),
|
|
67
|
+
tz_seconds: new Date().getTimezoneOffset() * -60
|
|
68
|
+
};
|
|
69
|
+
}
|