@nextlytics/core 0.2.1 → 0.2.2-canary.64

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.
@@ -23,15 +23,14 @@ __export(segment_exports, {
23
23
  module.exports = __toCommonJS(segment_exports);
24
24
  function segmentBackend(config) {
25
25
  const host = (config.host ?? "https://api.segment.io").replace(/\/$/, "");
26
- const authHeader = "Basic " + btoa(config.writeKey + ":");
27
- async function send(endpoint, payload) {
28
- const res = await fetch(`${host}${endpoint}`, {
26
+ async function send(batch) {
27
+ const res = await fetch(`${host}/v1/batch`, {
29
28
  method: "POST",
30
29
  headers: {
31
30
  "Content-Type": "application/json",
32
- Authorization: authHeader
31
+ "X-Write-Key": config.writeKey
33
32
  },
34
- body: JSON.stringify(payload)
33
+ body: JSON.stringify({ batch })
35
34
  });
36
35
  if (!res.ok) {
37
36
  const text = await res.text();
@@ -80,26 +79,18 @@ function segmentBackend(config) {
80
79
  async onEvent(event) {
81
80
  const context = buildContext(event);
82
81
  const properties = buildProperties(event);
82
+ const basePayload = {
83
+ messageId: event.eventId,
84
+ anonymousId: event.anonymousUserId,
85
+ userId: event.userContext?.userId,
86
+ timestamp: event.collectedAt,
87
+ context,
88
+ properties
89
+ };
83
90
  if (event.type === "pageView") {
84
- await send("/v1/page", {
85
- messageId: event.eventId,
86
- anonymousId: event.anonymousUserId,
87
- userId: event.userContext?.userId,
88
- name: event.serverContext.path,
89
- timestamp: event.collectedAt,
90
- context,
91
- properties
92
- });
91
+ await send([{ type: "page", name: event.serverContext.path, ...basePayload }]);
93
92
  } else {
94
- await send("/v1/track", {
95
- messageId: event.eventId,
96
- anonymousId: event.anonymousUserId,
97
- userId: event.userContext?.userId,
98
- event: event.type,
99
- timestamp: event.collectedAt,
100
- context,
101
- properties
102
- });
93
+ await send([{ type: "track", event: event.type, ...basePayload }]);
103
94
  }
104
95
  },
105
96
  updateEvent() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nextlytics/core",
3
- "version": "0.2.1",
3
+ "version": "0.2.2-canary.64",
4
4
  "description": "Analytics library for Next.js",
5
5
  "license": "MIT",
6
6
  "repository": {