core-outline 1.1.2 → 1.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-outline",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "A React component for Core&Outline",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es.js",
@@ -46,6 +46,7 @@
46
46
  "dependencies": {
47
47
  "react-error-boundary": "^5.0.0",
48
48
  "react-tracking": "^9.3.2",
49
- "rrweb": "^2.0.0-alpha.4"
49
+ "rrweb": "^2.0.0-alpha.4",
50
+ "socket.io-client": "^4.8.1"
50
51
  }
51
52
  }
@@ -1,19 +1,61 @@
1
- import React, { useEffect, useRef, useState } from 'react';
2
- import { record } from 'rrweb';
1
+ import React, { useEffect, useRef, useState } from "react";
2
+ import { record } from "rrweb";
3
+ import { v4 } from "uuid";
4
+ import io from "socket.io-client";
5
+ const socket = io("http://localhost:4000");
3
6
 
4
- const CoreOutline = ({ children }) => {
7
+ const CoreOutline = ({ children, data_source_id }) => {
5
8
  const [events, setEvents] = useState([]);
6
9
  const recorderActive = useRef(false);
7
10
  const [currentPage, setCurrentPage] = useState(window.location.href);
11
+ const [location, setLocation] = useState({ latitude: null, longitude: null });
12
+ const [browser, setBrowser] = useState(getBrowserInfo());
13
+ const sessionId = v4();
14
+
15
+ useEffect(() => {
16
+ socket.emit("dataEvent", {
17
+ topic: "session-data",
18
+ data_source_id: data_source_id,
19
+ session_id: sessionId,
20
+ start_date: new Date().toLocaleString(),
21
+ latitude: location.latitude,
22
+ longitude: location.longitude,
23
+ browser: browser,
24
+ event_id: "SESSION_START",
25
+ });
26
+
27
+ return () => {
28
+ socket.emit("dataEvent", {
29
+ topic: "session-data",
30
+ data_source_id: data_source_id,
31
+ session_id: sessionId,
32
+ start_date: new Date().toLocaleString(),
33
+ latitude: location.latitude,
34
+ longitude: location.longitude,
35
+ browser: browser,
36
+ event_id: "SESSION_END",
37
+ });
38
+ };
39
+ }, []);
8
40
 
9
41
  useEffect(() => {
10
- console.log(currentPage);
11
42
  const handleNavigation = () => {
12
43
  setCurrentPage(window.location.href);
13
- saveEvents(JSON.stringify(events));
44
+ socket.emit("dataEvent", {
45
+ topic: "session-data",
46
+ data_source_id: data_source_id,
47
+ session_id: sessionId,
48
+ start_date: new Date().toLocaleString(),
49
+ latitude: location.latitude,
50
+ longitude: location.longitude,
51
+ browser: browser,
52
+ page_name: window.location.href,
53
+ event_id: "PAGE_NAVIGATION",
54
+ });
55
+ saveEvents(events);
14
56
  };
15
57
 
16
- window.addEventListener('popstate', handleNavigation);
58
+ window.addEventListener("popstate", handleNavigation);
17
59
  const originalPushState = window.history.pushState;
18
60
  window.history.pushState = function (...args) {
19
61
  originalPushState.apply(this, args);
@@ -21,11 +63,16 @@ const CoreOutline = ({ children }) => {
21
63
  };
22
64
 
23
65
  return () => {
24
- window.removeEventListener('popstate', handleNavigation);
66
+ window.removeEventListener("popstate", handleNavigation);
25
67
  window.history.pushState = originalPushState;
26
68
  };
27
69
  }, [currentPage]);
28
70
 
71
+ useEffect(() => {
72
+ console.log("Location", location);
73
+ getLocation();
74
+ }, []);
75
+
29
76
  useEffect(() => {
30
77
  let stopFn;
31
78
  if (!recorderActive.current) {
@@ -41,22 +88,47 @@ const CoreOutline = ({ children }) => {
41
88
  return () => {
42
89
  if (stopFn) {
43
90
  stopFn();
91
+ saveEvents(events);
44
92
 
45
93
  recorderActive.current = false;
46
94
  }
47
95
  };
48
96
  }, []);
49
97
 
98
+ useEffect(() => {
99
+ const handleClick = (event) => {
100
+ console.log("Button clicked:", event.target.id);
101
+ // const itemId = event.target.id;
102
+ socket.emit("dataEvent", {
103
+ topic: "session-data",
104
+ data_source_id: data_source_id,
105
+ session_id: sessionId,
106
+ start_date: new Date().toLocaleString(),
107
+ latitude: location.latitude,
108
+ longitude: location.longitude,
109
+ browser: browser,
110
+ event_id: "ITEM_CLICKED",
111
+ item_clicked: event.target,
112
+ });
113
+ };
114
+
115
+ document.addEventListener("click", handleClick);
116
+
117
+ return () => {
118
+ document.removeEventListener("click", handleClick);
119
+ };
120
+ }, []);
121
+
50
122
  function saveEvents(events) {
51
123
  const requestOptions = {
52
- method: 'PUT',
124
+ method: "PUT",
53
125
  headers: {
54
- 'Content-Type': 'application/json',
126
+ "Content-Type": "application/json",
55
127
  },
56
128
  body: JSON.stringify(events),
57
129
  };
58
130
 
59
- fetch('http://localhost:8000/generate-s3-url')
131
+ fetch(`http://localhost:8000/generate-s3-url/${data_source_id}/${sessionId}`)
60
132
  .then((response) => response.json())
61
133
  .then((data) => {
62
134
  const uploadURL = data.signed_url;
@@ -64,6 +136,49 @@ const CoreOutline = ({ children }) => {
64
136
  });
65
137
  }
66
138
 
139
+ function getBrowserInfo() {
140
+ const userAgent = navigator.userAgent;
141
+ let browserName = "Unknown";
142
+
143
+ if (userAgent.indexOf("Firefox") > -1) {
144
+ browserName = "Firefox";
145
+ } else if (userAgent.indexOf("Opera") > -1 || userAgent.indexOf("OPR") > -1) {
146
+ browserName = "Opera";
147
+ } else if (userAgent.indexOf("Chrome") > -1) {
148
+ browserName = "Chrome";
149
+ } else if (userAgent.indexOf("Safari") > -1) {
150
+ browserName = "Safari";
151
+ } else if (userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident/") > -1) {
152
+ browserName = "Internet Explorer";
153
+ }
154
+
155
+ return browserName;
156
+ }
157
+
158
+ useEffect(() => {
159
+ if (events.length > 0) {
160
+ socket.emit("record-event", { sessionId, events });
161
+ }
162
+ }, [events]);
163
+
164
+ function getLocation() {
165
+ if (navigator.geolocation) {
166
+ navigator.geolocation.getCurrentPosition(
167
+ (position) => {
168
+ setLocation({
169
+ latitude: position.coords.latitude,
170
+ longitude: position.coords.longitude,
171
+ });
172
+ },
173
+ (error) => {
174
+ console.error("Error getting location:", error);
175
+ }
176
+ );
177
+ } else {
178
+ console.error("Geolocation is not supported by this browser.");
179
+ }
180
+ }
181
+
67
182
  return <div>{children}</div>;
68
183
  };
69
184