@canonical/analytics-events
Advanced tools
+14
-16
@@ -37,11 +37,2 @@ // src/session.ts | ||
| } | ||
| function getDeviceInfo() { | ||
| if (typeof window === "undefined") return null; | ||
| const ua = navigator.userAgent; | ||
| return { | ||
| userAgent: ua, | ||
| language: navigator.language, | ||
| touchSupport: "ontouchstart" in window || navigator.maxTouchPoints > 0 | ||
| }; | ||
| } | ||
@@ -52,5 +43,9 @@ // src/sender.ts | ||
| var ENDPOINT = "/analytics/events"; | ||
| var appName = ""; | ||
| var eventQueue = []; | ||
| var batchTimeout = null; | ||
| var initialized = false; | ||
| function setAppName(name) { | ||
| appName = name; | ||
| } | ||
| function generateEvent({ | ||
@@ -63,12 +58,10 @@ element, | ||
| return { | ||
| type: eventType, | ||
| event_type: eventType, | ||
| session_id: getSessionId(), | ||
| target: targetItem, | ||
| url: getCurrentUrl(), | ||
| sessionID: getSessionId(), | ||
| timestamp: (/* @__PURE__ */ new Date()).toISOString(), | ||
| attributes: { | ||
| ...extractAnalyticsAttributes(element), | ||
| ...extraAttributes | ||
| }, | ||
| device: getDeviceInfo() | ||
| } | ||
| }; | ||
@@ -79,3 +72,7 @@ } | ||
| if (events.length === 0) return; | ||
| const payload = JSON.stringify(events); | ||
| const batch = { | ||
| app_name: appName, | ||
| events | ||
| }; | ||
| const payload = JSON.stringify(batch); | ||
| if (navigator.sendBeacon) { | ||
@@ -332,4 +329,5 @@ const blob = new Blob([payload], { type: "application/json" }); | ||
| } | ||
| function initAnalytics() { | ||
| function initAnalytics({ appName: appName2 }) { | ||
| try { | ||
| setAppName(appName2); | ||
| if (document.readyState === "loading") { | ||
@@ -336,0 +334,0 @@ document.addEventListener("DOMContentLoaded", () => { |
@@ -0,2 +1,3 @@ | ||
| import type { AnalyticsOptions } from "./types"; | ||
| import "./types"; | ||
| export declare function initAnalytics(): void; | ||
| export declare function initAnalytics({ appName }: AnalyticsOptions): void; |
| import type { RawEvent, UserEvent } from "./types"; | ||
| export declare function setAppName(name: string): void; | ||
| export declare function generateEvent({ element, eventType, extraAttributes, }: RawEvent): UserEvent; | ||
| export declare function enqueue(event: RawEvent): void; |
| declare global { | ||
| interface Window { | ||
| Analytics: { | ||
| init: () => void; | ||
| init: (options: AnalyticsOptions) => void; | ||
| }; | ||
| } | ||
| } | ||
| export interface AnalyticsOptions { | ||
| appName: string; | ||
| } | ||
| export type EventType = "click" | "hover" | "view"; | ||
@@ -17,15 +20,13 @@ export interface RawEvent { | ||
| export interface UserEvent { | ||
| type: EventType; | ||
| event_type: EventType; | ||
| session_id: string; | ||
| target: string | undefined; | ||
| url: string | null; | ||
| sessionID: string; | ||
| timestamp: string; | ||
| attributes: { | ||
| [key: string]: string | number; | ||
| }; | ||
| device: { | ||
| userAgent: string; | ||
| language: string; | ||
| touchSupport: boolean; | ||
| } | null; | ||
| } | ||
| export interface EventBatch { | ||
| app_name: string; | ||
| events: UserEvent[]; | ||
| } |
| export declare function extractAnalyticsAttributes(element: HTMLElement): Record<string, string>; | ||
| export declare function getCurrentUrl(): string | null; | ||
| export declare function getDeviceInfo(): { | ||
| userAgent: string; | ||
| language: string; | ||
| touchSupport: boolean; | ||
| } | null; |
+1
-1
| { | ||
| "name": "@canonical/analytics-events", | ||
| "version": "1.0.0", | ||
| "version": "1.0.1", | ||
| "description": "Lightweight analytics event tracking", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
+15
-17
@@ -12,3 +12,2 @@ # Analytics Events | ||
| - Event batching (10 events or 10s timeout, flushes on page hide) | ||
| - Device info collection (userAgent, language, touch support) | ||
| - Custom attributes via `data-analytics-*` | ||
@@ -39,3 +38,3 @@ | ||
| import { initAnalytics } from '@canonical/analytics-events'; | ||
| initAnalytics(); | ||
| initAnalytics({ appName: 'snapcraft' }); | ||
| </script> | ||
@@ -59,17 +58,16 @@ ``` | ||
| { | ||
| type: "click" | "hover" | "view", | ||
| target: "signup_btn", | ||
| url: "https://example.com/page", | ||
| sessionID: "session_abc123...", | ||
| timestamp: "2024-01-15T10:30:00.000Z", | ||
| attributes: { | ||
| // custom data-analytics-* attributes | ||
| duration_ms: 2500, // hover and view only | ||
| visibility_ratio: 0.85 // view only | ||
| }, | ||
| device: { | ||
| userAgent: "...", | ||
| language: "en-US", | ||
| touchSupport: false | ||
| } | ||
| app_name: "snapcraft", | ||
| events: [ | ||
| { | ||
| event_type: "click" | "hover" | "view", | ||
| session_id: "session_abc123...", | ||
| target: "signup_btn", | ||
| url: "https://example.com/page", | ||
| attributes: { | ||
| // custom data-analytics-* attributes | ||
| duration_ms: 2500, // hover and view only | ||
| visibility_ratio: 0.85 // view only | ||
| } | ||
| } | ||
| ] | ||
| } | ||
@@ -76,0 +74,0 @@ ``` |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
14149
-1.3%390
-1.02%95
-2.06%