@clamp-sh/analytics 0.1.0 → 0.2.0

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/README.md CHANGED
@@ -61,7 +61,7 @@ Server events require an API key (found in your project settings).
61
61
  ## Script tag
62
62
 
63
63
  ```html
64
- <script src="https://cdn.clamp.sh/v1.js"></script>
64
+ <script src="https://cdn.jsdelivr.net/npm/@clamp-sh/analytics@0.2.0/dist/cdn.global.js"></script>
65
65
  <script>
66
66
  clamp.init("proj_xxx")
67
67
  </script>
@@ -0,0 +1 @@
1
+ "use strict";var clamp=(()=>{var a=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var g=(n,t)=>{for(var e in t)a(n,e,{get:t[e],enumerable:!0})},c=(n,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of u(t))!l.call(n,s)&&s!==e&&a(n,s,{get:()=>t[s],enumerable:!(i=h(t,s))||i.enumerable});return n};var y=n=>c(a({},"__esModule",{value:!0}),n);var E={};g(E,{getAnonymousId:()=>m,init:()=>f,track:()=>I});var v="https://api.clamp.sh",d=class{constructor(){this.projectId=null;this.endpoint=v;this.sessionId=null;this.anonymousId=null;this.queue=[];this.preInitQueue=[];this.timer=null;this.initialized=!1}init(t,e){if(typeof window>"u")return;this.projectId=t,e?.endpoint&&(this.endpoint=e.endpoint),this.initialized=!0,this.sessionId=sessionStorage.getItem("clamp_sid")??this.newId("ses"),sessionStorage.setItem("clamp_sid",this.sessionId),this.anonymousId=localStorage.getItem("clamp_aid")??this.newId("anon"),localStorage.setItem("clamp_aid",this.anonymousId);for(let r of this.preInitQueue)this.track(r.name,r.props);this.preInitQueue=[],this.pageview();let i=history.pushState.bind(history),s=history.replaceState.bind(history);history.pushState=(...r)=>{i(...r),this.pageview()},history.replaceState=(...r)=>{s(...r),this.pageview()},window.addEventListener("popstate",()=>this.pageview()),this.timer=setInterval(()=>this.flush(),5e3);let o=()=>this.flush(!0);document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&o()}),window.addEventListener("pagehide",o)}track(t,e){if(!this.initialized){this.preInitQueue.push({name:t,props:e});return}let i={name:t,url:location.href,referrer:document.referrer,sessionId:this.sessionId,anonymousId:this.anonymousId,timestamp:new Date().toISOString(),screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,platform:"web",properties:e};this.queue.push(i)}getAnonymousId(){return this.anonymousId}pageview(){this.track("pageview")}flush(t=!1){if(!this.projectId||this.queue.length===0)return;let e=this.queue.splice(0,100),i={p:this.projectId,events:e},s=`${this.endpoint}/e/batch`,o=JSON.stringify(i);t&&typeof navigator.sendBeacon=="function"?navigator.sendBeacon(s,o):fetch(s,{method:"POST",headers:{"content-type":"application/json"},body:o,keepalive:!0}).catch(()=>{}),this.queue.length>0&&this.flush(t)}newId(t){let e=Math.random().toString(36).slice(2,10),i=Date.now().toString(36);return`${t}_${i}${e}`}},p=new d;function f(n,t){p.init(n,t)}function I(n,t){p.track(n,t)}function m(){return p.getAnonymousId()}return y(E);})();
package/package.json CHANGED
@@ -1,14 +1,22 @@
1
1
  {
2
2
  "name": "@clamp-sh/analytics",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Lightweight analytics SDK for Clamp. Auto-pageviews, sessions, batching. Browser, server, and React.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://clamp.sh",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "https://github.com/clamp-sh/analytics"
9
+ "url": "git+https://github.com/clamp-sh/clamp.git",
10
+ "directory": "packages/sdk"
10
11
  },
11
- "keywords": ["analytics", "tracking", "pageviews", "sessions", "react", "nextjs"],
12
+ "keywords": [
13
+ "analytics",
14
+ "tracking",
15
+ "pageviews",
16
+ "sessions",
17
+ "react",
18
+ "nextjs"
19
+ ],
12
20
  "main": "dist/index.js",
13
21
  "module": "dist/index.mjs",
14
22
  "types": "dist/index.d.ts",
@@ -29,7 +37,9 @@
29
37
  "require": "./dist/react.js"
30
38
  }
31
39
  },
32
- "files": ["dist"],
40
+ "files": [
41
+ "dist"
42
+ ],
33
43
  "sideEffects": false,
34
44
  "scripts": {
35
45
  "build": "tsup",
@@ -40,7 +50,9 @@
40
50
  "react": ">=18"
41
51
  },
42
52
  "peerDependenciesMeta": {
43
- "react": { "optional": true }
53
+ "react": {
54
+ "optional": true
55
+ }
44
56
  },
45
57
  "devDependencies": {
46
58
  "react": "^19",