@edulib-france/expo-yawl 0.0.6 → 0.0.7

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.
@@ -53,6 +53,7 @@ export default class Yawl {
53
53
  } | undefined;
54
54
  setVisitId: (visitId: string) => Promise<void>;
55
55
  setViewTracker: (viewTracker: () => YawlView) => () => YawlView;
56
+ resetVisitor: () => Promise<void>;
56
57
  private loadVisitorId;
57
58
  private getVisitData;
58
59
  private initConnection;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,GAAG,EAAoB,MAAM,OAAO,CAAC;AA6B9C,MAAM,CAAC,OAAO,OAAO,IAAI;IACvB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,WAAW,CAAkB;IACrC,OAAO,CAAC,iBAAiB,CAA6B;IACtD,OAAO,CAAC,KAAK,CAAM;IACnB,OAAO,CAAC,GAAG,CAAU;IACrB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,WAAW,CAAC,CAAiB;gBAEzB,EAAE,MAAM,EAAE,GAAY,EAAE,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,GAAG,CAAA;KAAE;IAOnE,IAAI,sBAMF;IAEF,KAAK,GAAI,OAAO,SAAS;;;;;;;;;;;;MAgBvB;IAEF;;;;;;;;OAQG;IACH,SAAS,GAAI,OAAO,QAAQ;;;;;;;;;;;;;;kBAuB1B;IAEF,UAAU,GAAU,SAAS,MAAM,KAAG,OAAO,CAAC,IAAI,CAAC,CAMjD;IAEF,cAAc,GAAI,aAAa,MAAM,QAAQ,WAAR,QAAQ,CACV;YAErB,aAAa;YASb,YAAY;IAU1B,OAAO,CAAC,cAAc,CA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Qf;IAEF,OAAO,CAAC,iBAAiB,CAUvB;IAEF,OAAO,CAAC,iBAAiB,CA4BvB;IAEF,OAAO,CAAC,gBAAgB,CA6BtB;CACH"}
@@ -27,6 +27,7 @@ const WORKERS_OPTIONS = {
27
27
  attempts: 1000,
28
28
  concurrency: 1,
29
29
  };
30
+ const VISITOR_ID_KEY = "yawl_visitorId";
30
31
  export default class Yawl {
31
32
  visitId;
32
33
  visitorId;
@@ -104,11 +105,18 @@ export default class Yawl {
104
105
  }
105
106
  };
106
107
  setViewTracker = (viewTracker) => (this.viewTracker = viewTracker);
108
+ resetVisitor = async () => {
109
+ if (this.queue) {
110
+ await this.queue.flushQueue();
111
+ }
112
+ this.visitorId = "";
113
+ this.visitId = "";
114
+ await AsyncStorage.removeItem(VISITOR_ID_KEY);
115
+ };
107
116
  async loadVisitorId() {
108
- const visitorIdKey = "yawl_visitorId";
109
- const visitorId = await AsyncStorage.getItem(visitorIdKey);
117
+ const visitorId = await AsyncStorage.getItem(VISITOR_ID_KEY);
110
118
  if (!visitorId)
111
- return await AsyncStorage.setItem(visitorIdKey, this.visitorId);
119
+ return await AsyncStorage.setItem(VISITOR_ID_KEY, this.visitorId);
112
120
  this.visitorId = visitorId;
113
121
  }
114
122
  async getVisitData() {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,2CAA2C,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAC7E,OAAO,YAAY,MAAM,oBAAoB,CAAC;AAG9C,OAAO,EAAgB,OAAO,EAAE,MAAM,OAAO,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C;;;;;;;;;;;;;;;EAeE;AAEF,MAAM,WAAW,GAAG,SAAS,CAAC;AAC9B,MAA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,EAAE;gBACpC,OAAO,CAAC,KAAK,CAAC,2CAA2C,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;gBACtE,wDAAwD;YAC1D,CAAC;YACD,QAAQ,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;gBACnC,OAAO,CAAC,KAAK,CAAC,0CAA0C,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;gBACrE,uDAAuD;YACzD,CAAC;YACD,GAAG,eAAe;SACnB,CACF,CAAC;IACJ,CAAC,CAAC;CACH","sourcesContent":["import AsyncStorage from \"@react-native-async-storage/async-storage\";\nimport { addNetworkStateListener, getNetworkStateAsync } from \"expo-network\";\nimport queueFactory from \"react-native-queue\";\n\nimport { YawlEvent, YawlView } from \"../Yawl.types\";\nimport { Env, YawlApi, yawlApi } from \"./api\";\nimport { getDeviceInfo } from \"./deviceInfo\";\nimport { generateUUID } from \"./generateUUID\";\n\n/*\n\nINPUT PARAMETERS:\n{\n visitorId: string - visitor id\n visitData: object - extra visit data to be sent to ahoy server\n onTracking: {\n started: async (event) - function that will be invoked after tracking start\n succeeded: async (event) - function that will be invoked when tracking success\n failure: async (event) - function that will be invoked when tracking failure (before failed)\n failed: async (event) - function that will be invoked if tracking fails\n error: async ({ name, error }) - function that will be invoked when error occured\n }\n offlineMode: boolean - ONLY FOR TESTING PURPOSES, indicates if you want to test with no internet\n}\n*/\n\nconst JOB_VISITOR = \"visitor\";\nconst JOB_TRACKING = \"tracking\";\ntype JOB_TYPES = typeof JOB_TRACKING | typeof JOB_VISITOR;\nconst WORKERS_OPTIONS = {\n timeout: 20000,\n attempts: 1000,\n concurrency: 1,\n};\nexport default class Yawl {\n private visitId: string;\n private visitorId: string;\n private offlineMode: boolean = false;\n private hasInternetAccess: boolean | undefined = true;\n private queue: any;\n private api: YawlApi;\n private baseUrl: string;\n private viewTracker?: () => YawlView;\n\n constructor({ apiKey, env = \"prod\" }: { apiKey: string; env?: Env }) {\n this.api = yawlApi({ apiKey, env });\n this.visitId = generateUUID();\n this.visitorId = generateUUID();\n this.baseUrl = `react-native-${apiKey}`;\n }\n\n init = async () => {\n await this.initConnection();\n await this.initQueue();\n await this.loadVisitorId();\n const data = await this.getVisitData();\n this.createJob(JOB_VISITOR, data);\n };\n\n track = (event: YawlEvent) => {\n const _event = {\n event: {\n id: generateUUID(),\n visit_token: this.visitId,\n visitor_token: this.visitorId,\n timestamp: new Date().getTime() / 1000.0,\n ...event,\n // properties: this.prepareProperties(event.properties),\n },\n };\n // Send event to all services before ahoy queue\n // await this.onTrackingInvoke(\"started\", event);\n // Create ahoy job and add to queue\n this.createJob(JOB_TRACKING, _event);\n return _event;\n };\n\n /**\n * Track a view event.\n * `viewTracker` object takes precedence over the `view` parameter.\n * Properties from both `view` and `viewTracker` are merged.\n * If `viewTracker` is not provided, the `view` parameter must contain a page.\n *\n * @param view - Optional view object containing page, title, and properties. Must be set if viewTracker is not provided, otherwise the event will not be sent.\n * @returns the event object with the tracking data.\n */\n trackView = (view?: YawlView) => {\n const trackedView = this.viewTracker?.();\n if (!view?.page && !trackedView?.page) {\n console.warn(\n \"Yawl: trackView() - page is required; Either set it in the view param or yawl.setViewTracker(); Event not sent\"\n );\n return;\n }\n const _event = {\n event: {\n name: \"$view\",\n url: this.baseUrl,\n ...view,\n ...trackedView,\n properties: { ...view?.properties, ...trackedView?.properties },\n id: generateUUID(),\n visit_token: this.visitId,\n visitor_token: this.visitorId,\n timestamp: new Date().getTime() / 1000.0,\n },\n };\n this.createJob(JOB_TRACKING, _event);\n return _event;\n };\n\n setVisitId = async (visitId: string): Promise<void> => {\n this.visitId = visitId;\n\n if (this.hasInternetAccess) {\n this.trackVisit(await this.getVisitData());\n }\n };\n\n setViewTracker = (viewTracker: () => YawlView) =>\n (this.viewTracker = viewTracker);\n\n private async loadVisitorId(): Promise<void> {\n const visitorIdKey = \"yawl_visitorId\";\n const visitorId = await AsyncStorage.getItem(visitorIdKey);\n if (!visitorId)\n return await AsyncStorage.setItem(visitorIdKey, this.visitorId);\n\n this.visitorId = visitorId;\n }\n\n private async getVisitData(): Promise<object> {\n return {\n visit: {\n visit_token: this.visitId,\n visitor_token: this.visitorId,\n ...getDeviceInfo(),\n },\n };\n }\n\n private initConnection = async () => {\n const state = await getNetworkStateAsync();\n this.hasInternetAccess = state.isConnected;\n addNetworkStateListener(\n (state) => (this.hasInternetAccess = state.isConnected)\n );\n };\n\n private initQueue = async () => {\n this.queue = await queueFactory(true);\n this.addTrackingWorker();\n this.addVisitorWorker();\n };\n\n private trackVisit = (event) => this.api.sendVisit(event);\n\n private createJob = (name: JOB_TYPES, event: object) => {\n if (this.offlineMode) {\n this.hasInternetAccess = false;\n }\n\n if (this.queue) {\n this.queue.createJob(name, event);\n }\n };\n\n private prepareProperties = (inter: object) => {\n const properties = {\n // applicationBundleId: this.applicationBundleId,\n };\n Object.keys(inter).map((name, key) => {\n const k = name.replace(/[^a-z0-9_]/gi, \"\");\n const val = inter[name];\n properties[k] = typeof val === \"boolean\" ? val.toString() : val;\n });\n return properties;\n };\n\n private addTrackingWorker = () => {\n this.queue.addWorker(\n JOB_TRACKING,\n async (id, event) => {\n if (this.hasInternetAccess) {\n const res = await this.api.sendEvent(event);\n // TODO: parse response\n console.debug(\"🚀 ===> ~ res:\", res);\n return { ok: true };\n }\n throw new Error(\"Network request failed\");\n },\n {\n onSuccess: async (id, event) => {\n console.debug(\"🚀 ===> JOB_TRACKING onSuccess\", id, event);\n // await this.onTrackingInvoke(\"succeeded\", event);\n },\n onFailure: async (id, event, error) => {\n console.debug(\"🚀 ===> JOB_TRACKING onFailure\", id, event, error);\n // await this.onTrackingInvoke(\"failure\", event, error);\n },\n onFailed: async (id, event, error) => {\n console.debug(\"🚀 ===> JOB_TRACKING onFailed\", id, event, error);\n // await this.onTrackingInvoke(\"failed\", event, error);\n },\n ...WORKERS_OPTIONS,\n }\n );\n };\n\n private addVisitorWorker = () => {\n this.queue.addWorker(\n JOB_VISITOR,\n async (id, event) => {\n if (this.hasInternetAccess) {\n const res = await this.trackVisit(event);\n // TODO: parse response\n console.debug(\"🚀 ===> ~ res:\", res);\n return { ok: true };\n }\n\n throw new Error(\"Network request failed\");\n },\n {\n onSuccess: async (id, event) => {\n console.debug(\"🚀 ===> JOB_VISITOR ~ onSuccess: ~ event:\", id, event);\n // await this.onTrackingInvoke(\"succeeded\", event);\n },\n onFailure: async (id, event, error) => {\n console.debug(\"🚀 ===> JOB_VISITOR ~ onFailure: ~ error:\", id, error);\n // await this.onTrackingInvoke(\"failure\", event, error);\n },\n onFailed: async (id, event, error) => {\n console.debug(\"🚀 ===> JOB_VISITOR ~ onFailed: ~ error:\", id, error);\n // await this.onTrackingInvoke(\"failed\", event, error);\n },\n ...WORKERS_OPTIONS,\n }\n );\n };\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,2CAA2C,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAC7E,OAAO,YAAY,MAAM,oBAAoB,CAAC;AAG9C,OAAO,EAAgB,OAAO,EAAE,MAAM,OAAO,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C;;;;;;;;;;;;;;;EAeE;AAEF,MAAM,WAAW,GAAG,SAAS,CAAC;AAC9B,MAAM,YAAY,GAAG,UAAU,CAAC;AAEhC,MAAM,eAAe,GAAG;IACtB,OAAO,EAAE,KAAK;IACd,QAAQ,EAAE,IAAI;IACd,WAAW,EAAE,CAAC;CACf,CAAC;AACF,MAAM,cAAc,GAAG,gBAAgB,CAAC;AACxC,MAAM,CAAC,OAAO,OAAO,IAAI;IACf,OAAO,CAAS;IAChB,SAAS,CAAS;IAClB,WAAW,GAAY,KAAK,CAAC;IAC7B,iBAAiB,GAAwB,IAAI,CAAC;IA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ACpC,OAAO,CAAC,KAAK,CAAC,2CAA2C,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;gBACtE,wDAAwD;YAC1D,CAAC;YACD,QAAQ,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;gBACnC,OAAO,CAAC,KAAK,CAAC,0CAA0C,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;gBACrE,uDAAuD;YACzD,CAAC;YACD,GAAG,eAAe;SACnB,CACF,CAAC;IACJ,CAAC,CAAC;CACH","sourcesContent":["import AsyncStorage from \"@react-native-async-storage/async-storage\";\nimport { addNetworkStateListener, getNetworkStateAsync } from \"expo-network\";\nimport queueFactory from \"react-native-queue\";\n\nimport { YawlEvent, YawlView } from \"../Yawl.types\";\nimport { Env, YawlApi, yawlApi } from \"./api\";\nimport { getDeviceInfo } from \"./deviceInfo\";\nimport { generateUUID } from \"./generateUUID\";\n\n/*\n\nINPUT PARAMETERS:\n{\n visitorId: string - visitor id\n visitData: object - extra visit data to be sent to ahoy server\n onTracking: {\n started: async (event) - function that will be invoked after tracking start\n succeeded: async (event) - function that will be invoked when tracking success\n failure: async (event) - function that will be invoked when tracking failure (before failed)\n failed: async (event) - function that will be invoked if tracking fails\n error: async ({ name, error }) - function that will be invoked when error occured\n }\n offlineMode: boolean - ONLY FOR TESTING PURPOSES, indicates if you want to test with no internet\n}\n*/\n\nconst JOB_VISITOR = \"visitor\";\nconst JOB_TRACKING = \"tracking\";\ntype JOB_TYPES = typeof JOB_TRACKING | typeof JOB_VISITOR;\nconst WORKERS_OPTIONS = {\n timeout: 20000,\n attempts: 1000,\n concurrency: 1,\n};\nconst VISITOR_ID_KEY = \"yawl_visitorId\";\nexport default class Yawl {\n private visitId: string;\n private visitorId: string;\n private offlineMode: boolean = false;\n private hasInternetAccess: boolean | undefined = true;\n private queue: any;\n private api: YawlApi;\n private baseUrl: string;\n private viewTracker?: () => YawlView;\n\n constructor({ apiKey, env = \"prod\" }: { apiKey: string; env?: Env }) {\n this.api = yawlApi({ apiKey, env });\n this.visitId = generateUUID();\n this.visitorId = generateUUID();\n this.baseUrl = `react-native-${apiKey}`;\n }\n\n init = async () => {\n await this.initConnection();\n await this.initQueue();\n await this.loadVisitorId();\n const data = await this.getVisitData();\n this.createJob(JOB_VISITOR, data);\n };\n\n track = (event: YawlEvent) => {\n const _event = {\n event: {\n id: generateUUID(),\n visit_token: this.visitId,\n visitor_token: this.visitorId,\n timestamp: new Date().getTime() / 1000.0,\n ...event,\n // properties: this.prepareProperties(event.properties),\n },\n };\n // Send event to all services before ahoy queue\n // await this.onTrackingInvoke(\"started\", event);\n // Create ahoy job and add to queue\n this.createJob(JOB_TRACKING, _event);\n return _event;\n };\n\n /**\n * Track a view event.\n * `viewTracker` object takes precedence over the `view` parameter.\n * Properties from both `view` and `viewTracker` are merged.\n * If `viewTracker` is not provided, the `view` parameter must contain a page.\n *\n * @param view - Optional view object containing page, title, and properties. Must be set if viewTracker is not provided, otherwise the event will not be sent.\n * @returns the event object with the tracking data.\n */\n trackView = (view?: YawlView) => {\n const trackedView = this.viewTracker?.();\n if (!view?.page && !trackedView?.page) {\n console.warn(\n \"Yawl: trackView() - page is required; Either set it in the view param or yawl.setViewTracker(); Event not sent\"\n );\n return;\n }\n const _event = {\n event: {\n name: \"$view\",\n url: this.baseUrl,\n ...view,\n ...trackedView,\n properties: { ...view?.properties, ...trackedView?.properties },\n id: generateUUID(),\n visit_token: this.visitId,\n visitor_token: this.visitorId,\n timestamp: new Date().getTime() / 1000.0,\n },\n };\n this.createJob(JOB_TRACKING, _event);\n return _event;\n };\n\n setVisitId = async (visitId: string): Promise<void> => {\n this.visitId = visitId;\n\n if (this.hasInternetAccess) {\n this.trackVisit(await this.getVisitData());\n }\n };\n\n setViewTracker = (viewTracker: () => YawlView) =>\n (this.viewTracker = viewTracker);\n\n resetVisitor = async () => {\n if (this.queue) {\n await this.queue.flushQueue();\n }\n\n this.visitorId = \"\";\n this.visitId = \"\";\n\n await AsyncStorage.removeItem(VISITOR_ID_KEY);\n };\n\n private async loadVisitorId(): Promise<void> {\n const visitorId = await AsyncStorage.getItem(VISITOR_ID_KEY);\n if (!visitorId)\n return await AsyncStorage.setItem(VISITOR_ID_KEY, this.visitorId);\n\n this.visitorId = visitorId;\n }\n\n private async getVisitData(): Promise<object> {\n return {\n visit: {\n visit_token: this.visitId,\n visitor_token: this.visitorId,\n ...getDeviceInfo(),\n },\n };\n }\n\n private initConnection = async () => {\n const state = await getNetworkStateAsync();\n this.hasInternetAccess = state.isConnected;\n addNetworkStateListener(\n (state) => (this.hasInternetAccess = state.isConnected)\n );\n };\n\n private initQueue = async () => {\n this.queue = await queueFactory(true);\n this.addTrackingWorker();\n this.addVisitorWorker();\n };\n\n private trackVisit = (event) => this.api.sendVisit(event);\n\n private createJob = (name: JOB_TYPES, event: object) => {\n if (this.offlineMode) {\n this.hasInternetAccess = false;\n }\n\n if (this.queue) {\n this.queue.createJob(name, event);\n }\n };\n\n private prepareProperties = (inter: object) => {\n const properties = {\n // applicationBundleId: this.applicationBundleId,\n };\n Object.keys(inter).map((name, key) => {\n const k = name.replace(/[^a-z0-9_]/gi, \"\");\n const val = inter[name];\n properties[k] = typeof val === \"boolean\" ? val.toString() : val;\n });\n return properties;\n };\n\n private addTrackingWorker = () => {\n this.queue.addWorker(\n JOB_TRACKING,\n async (id, event) => {\n if (this.hasInternetAccess) {\n const res = await this.api.sendEvent(event);\n // TODO: parse response\n console.debug(\"🚀 ===> ~ res:\", res);\n return { ok: true };\n }\n throw new Error(\"Network request failed\");\n },\n {\n onSuccess: async (id, event) => {\n console.debug(\"🚀 ===> JOB_TRACKING onSuccess\", id, event);\n // await this.onTrackingInvoke(\"succeeded\", event);\n },\n onFailure: async (id, event, error) => {\n console.debug(\"🚀 ===> JOB_TRACKING onFailure\", id, event, error);\n // await this.onTrackingInvoke(\"failure\", event, error);\n },\n onFailed: async (id, event, error) => {\n console.debug(\"🚀 ===> JOB_TRACKING onFailed\", id, event, error);\n // await this.onTrackingInvoke(\"failed\", event, error);\n },\n ...WORKERS_OPTIONS,\n }\n );\n };\n\n private addVisitorWorker = () => {\n this.queue.addWorker(\n JOB_VISITOR,\n async (id, event) => {\n if (this.hasInternetAccess) {\n const res = await this.trackVisit(event);\n // TODO: parse response\n console.debug(\"🚀 ===> ~ res:\", res);\n return { ok: true };\n }\n\n throw new Error(\"Network request failed\");\n },\n {\n onSuccess: async (id, event) => {\n console.debug(\"🚀 ===> JOB_VISITOR ~ onSuccess: ~ event:\", id, event);\n // await this.onTrackingInvoke(\"succeeded\", event);\n },\n onFailure: async (id, event, error) => {\n console.debug(\"🚀 ===> JOB_VISITOR ~ onFailure: ~ error:\", id, error);\n // await this.onTrackingInvoke(\"failure\", event, error);\n },\n onFailed: async (id, event, error) => {\n console.debug(\"🚀 ===> JOB_VISITOR ~ onFailed: ~ error:\", id, error);\n // await this.onTrackingInvoke(\"failed\", event, error);\n },\n ...WORKERS_OPTIONS,\n }\n );\n };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edulib-france/expo-yawl",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -38,21 +38,21 @@
38
38
  "license": "MIT",
39
39
  "homepage": "https://github.com/edulib-france/expo-yawl#readme",
40
40
  "dependencies": {
41
- "@react-native-async-storage/async-storage": "1.23.1",
42
- "expo": "~52.0.46",
41
+ "@react-native-async-storage/async-storage": "2.2.0",
42
+ "expo": "~53.0.0",
43
43
  "expo-application": "~6.0.2",
44
44
  "expo-crypto": "~14.0.2",
45
45
  "expo-device": "~7.0.3",
46
46
  "expo-network": "~7.0.5",
47
47
  "react-native-queue": "git+https://github.com/edulib-france/react-native-queue-asyncstorage.git#005bb418d27f1a93f4372ba6764292a71d954b6e",
48
- "wretch": "^2.11.0"
48
+ "wretch": "^3.0.0"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@types/react": "~18.3.12",
52
- "expo": "~52.0.0",
52
+ "expo": "~53.0.0",
53
53
  "expo-module-scripts": "^4.0.4",
54
54
  "react-native": "0.76.0",
55
- "vitest": "^3.1.1"
55
+ "vitest": "^4.0.0"
56
56
  },
57
57
  "peerDependencies": {
58
58
  "expo": "*",
package/src/Yawl.types.ts DELETED
@@ -1,21 +0,0 @@
1
- export type YawlEvent = {
2
- name: string;
3
- ean?: string;
4
- establishment_account_id?: string;
5
- properties?: Record<string, unknown>;
6
- user_type?: string;
7
- };
8
-
9
- /**
10
- * YawlView is a type that represents a view tracking event.
11
- * It contains optional properties for page, title, and additional properties.
12
- *
13
- * @param page - Optional path of the page being viewed. ie. "/home". Ignored if viewTracker is set.
14
- * @param title - Optional title of the page being viewed. ie. "Home". Ignored if viewTracker is set.
15
- * @param properties - Optional additional properties to be sent with the event. Merged with viewTracker's properties.
16
- */
17
- export type YawlView = {
18
- page?: string;
19
- title?: string;
20
- properties?: Record<string, unknown>;
21
- };
package/src/YawlModule.ts DELETED
@@ -1 +0,0 @@
1
- export { default } from "./core/index";
@@ -1,18 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
-
3
- import { yawlApi } from "./api";
4
-
5
- describe("API Tests", () => {
6
- const apiConfig = { apiKey: "test-key", env: "staging" as const };
7
- const data = { visit: { id: "test-id", visitor_id: "test-visitor-id" } };
8
- it("should failed when wrong key", async () => {
9
- const api = yawlApi(apiConfig);
10
- await expect(api.sendVisit(data)).rejects.toThrowError("Unauthorized");
11
- });
12
- it("should send visit", async () => {
13
- const api = yawlApi(apiConfig);
14
- await expect(api.sendVisit(data)).resolves.toMatchObject({
15
- message: "Ahoy::Visit created",
16
- });
17
- });
18
- });
package/src/core/api.ts DELETED
@@ -1,32 +0,0 @@
1
- import wretch from "wretch";
2
-
3
- export type Env = "prod" | "staging";
4
- export const API_URL: { [key in Env]: string } = {
5
- prod: "https://www.edulib.fr",
6
- staging: "https://staging.edulib.fr",
7
- } as const;
8
-
9
- export type YawlApi = {
10
- sendVisit: (data: object) => Promise<any>;
11
- sendEvent: (data: object) => Promise<any>;
12
- };
13
-
14
- export const yawlApi = ({
15
- apiKey,
16
- env = "prod",
17
- }: {
18
- apiKey: string;
19
- env?: Env;
20
- }): YawlApi => {
21
- async function fetchGuard(url: string, data: object) {
22
- return await wretch(url).headers({ "Api-Key": apiKey }).post(data).json();
23
- }
24
- async function sendVisit(data: object) {
25
- return fetchGuard(`${API_URL[env]}/ahoy/visits`, data);
26
- }
27
-
28
- async function sendEvent(data: object) {
29
- return fetchGuard(`${API_URL[env]}/ahoy/events`, data);
30
- }
31
- return { sendVisit, sendEvent };
32
- };
@@ -1,19 +0,0 @@
1
- import { nativeApplicationVersion } from "expo-application";
2
- import { deviceType, DeviceType, osName, osVersion } from "expo-device";
3
-
4
- export type YawlDeviceInfo = {
5
- app_version?: string;
6
- device_type?: string;
7
- os?: string;
8
- os_version?: string;
9
- platform: string;
10
- };
11
- export const getDeviceInfo = (): YawlDeviceInfo => {
12
- return {
13
- app_version: nativeApplicationVersion ?? undefined,
14
- device_type: DeviceType[deviceType ?? 0],
15
- os: osName ?? undefined,
16
- os_version: osVersion ?? undefined,
17
- platform: "react-native",
18
- };
19
- };
@@ -1,11 +0,0 @@
1
- import { randomUUID } from "expo-crypto";
2
-
3
- export const generateUUID = () => randomUUID();
4
-
5
- export const unsecureGenerateUUID = () =>
6
- "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
7
- const r = (Math.random() * 16) | 0;
8
- const v = c === "x" ? r : (r & 0x3) | 0x8;
9
-
10
- return v.toString(16);
11
- });
package/src/core/index.ts DELETED
@@ -1,240 +0,0 @@
1
- import AsyncStorage from "@react-native-async-storage/async-storage";
2
- import { addNetworkStateListener, getNetworkStateAsync } from "expo-network";
3
- import queueFactory from "react-native-queue";
4
-
5
- import { YawlEvent, YawlView } from "../Yawl.types";
6
- import { Env, YawlApi, yawlApi } from "./api";
7
- import { getDeviceInfo } from "./deviceInfo";
8
- import { generateUUID } from "./generateUUID";
9
-
10
- /*
11
-
12
- INPUT PARAMETERS:
13
- {
14
- visitorId: string - visitor id
15
- visitData: object - extra visit data to be sent to ahoy server
16
- onTracking: {
17
- started: async (event) - function that will be invoked after tracking start
18
- succeeded: async (event) - function that will be invoked when tracking success
19
- failure: async (event) - function that will be invoked when tracking failure (before failed)
20
- failed: async (event) - function that will be invoked if tracking fails
21
- error: async ({ name, error }) - function that will be invoked when error occured
22
- }
23
- offlineMode: boolean - ONLY FOR TESTING PURPOSES, indicates if you want to test with no internet
24
- }
25
- */
26
-
27
- const JOB_VISITOR = "visitor";
28
- const JOB_TRACKING = "tracking";
29
- type JOB_TYPES = typeof JOB_TRACKING | typeof JOB_VISITOR;
30
- const WORKERS_OPTIONS = {
31
- timeout: 20000,
32
- attempts: 1000,
33
- concurrency: 1,
34
- };
35
- export default class Yawl {
36
- private visitId: string;
37
- private visitorId: string;
38
- private offlineMode: boolean = false;
39
- private hasInternetAccess: boolean | undefined = true;
40
- private queue: any;
41
- private api: YawlApi;
42
- private baseUrl: string;
43
- private viewTracker?: () => YawlView;
44
-
45
- constructor({ apiKey, env = "prod" }: { apiKey: string; env?: Env }) {
46
- this.api = yawlApi({ apiKey, env });
47
- this.visitId = generateUUID();
48
- this.visitorId = generateUUID();
49
- this.baseUrl = `react-native-${apiKey}`;
50
- }
51
-
52
- init = async () => {
53
- await this.initConnection();
54
- await this.initQueue();
55
- await this.loadVisitorId();
56
- const data = await this.getVisitData();
57
- this.createJob(JOB_VISITOR, data);
58
- };
59
-
60
- track = (event: YawlEvent) => {
61
- const _event = {
62
- event: {
63
- id: generateUUID(),
64
- visit_token: this.visitId,
65
- visitor_token: this.visitorId,
66
- timestamp: new Date().getTime() / 1000.0,
67
- ...event,
68
- // properties: this.prepareProperties(event.properties),
69
- },
70
- };
71
- // Send event to all services before ahoy queue
72
- // await this.onTrackingInvoke("started", event);
73
- // Create ahoy job and add to queue
74
- this.createJob(JOB_TRACKING, _event);
75
- return _event;
76
- };
77
-
78
- /**
79
- * Track a view event.
80
- * `viewTracker` object takes precedence over the `view` parameter.
81
- * Properties from both `view` and `viewTracker` are merged.
82
- * If `viewTracker` is not provided, the `view` parameter must contain a page.
83
- *
84
- * @param view - Optional view object containing page, title, and properties. Must be set if viewTracker is not provided, otherwise the event will not be sent.
85
- * @returns the event object with the tracking data.
86
- */
87
- trackView = (view?: YawlView) => {
88
- const trackedView = this.viewTracker?.();
89
- if (!view?.page && !trackedView?.page) {
90
- console.warn(
91
- "Yawl: trackView() - page is required; Either set it in the view param or yawl.setViewTracker(); Event not sent"
92
- );
93
- return;
94
- }
95
- const _event = {
96
- event: {
97
- name: "$view",
98
- url: this.baseUrl,
99
- ...view,
100
- ...trackedView,
101
- properties: { ...view?.properties, ...trackedView?.properties },
102
- id: generateUUID(),
103
- visit_token: this.visitId,
104
- visitor_token: this.visitorId,
105
- timestamp: new Date().getTime() / 1000.0,
106
- },
107
- };
108
- this.createJob(JOB_TRACKING, _event);
109
- return _event;
110
- };
111
-
112
- setVisitId = async (visitId: string): Promise<void> => {
113
- this.visitId = visitId;
114
-
115
- if (this.hasInternetAccess) {
116
- this.trackVisit(await this.getVisitData());
117
- }
118
- };
119
-
120
- setViewTracker = (viewTracker: () => YawlView) =>
121
- (this.viewTracker = viewTracker);
122
-
123
- private async loadVisitorId(): Promise<void> {
124
- const visitorIdKey = "yawl_visitorId";
125
- const visitorId = await AsyncStorage.getItem(visitorIdKey);
126
- if (!visitorId)
127
- return await AsyncStorage.setItem(visitorIdKey, this.visitorId);
128
-
129
- this.visitorId = visitorId;
130
- }
131
-
132
- private async getVisitData(): Promise<object> {
133
- return {
134
- visit: {
135
- visit_token: this.visitId,
136
- visitor_token: this.visitorId,
137
- ...getDeviceInfo(),
138
- },
139
- };
140
- }
141
-
142
- private initConnection = async () => {
143
- const state = await getNetworkStateAsync();
144
- this.hasInternetAccess = state.isConnected;
145
- addNetworkStateListener(
146
- (state) => (this.hasInternetAccess = state.isConnected)
147
- );
148
- };
149
-
150
- private initQueue = async () => {
151
- this.queue = await queueFactory(true);
152
- this.addTrackingWorker();
153
- this.addVisitorWorker();
154
- };
155
-
156
- private trackVisit = (event) => this.api.sendVisit(event);
157
-
158
- private createJob = (name: JOB_TYPES, event: object) => {
159
- if (this.offlineMode) {
160
- this.hasInternetAccess = false;
161
- }
162
-
163
- if (this.queue) {
164
- this.queue.createJob(name, event);
165
- }
166
- };
167
-
168
- private prepareProperties = (inter: object) => {
169
- const properties = {
170
- // applicationBundleId: this.applicationBundleId,
171
- };
172
- Object.keys(inter).map((name, key) => {
173
- const k = name.replace(/[^a-z0-9_]/gi, "");
174
- const val = inter[name];
175
- properties[k] = typeof val === "boolean" ? val.toString() : val;
176
- });
177
- return properties;
178
- };
179
-
180
- private addTrackingWorker = () => {
181
- this.queue.addWorker(
182
- JOB_TRACKING,
183
- async (id, event) => {
184
- if (this.hasInternetAccess) {
185
- const res = await this.api.sendEvent(event);
186
- // TODO: parse response
187
- console.debug("🚀 ===> ~ res:", res);
188
- return { ok: true };
189
- }
190
- throw new Error("Network request failed");
191
- },
192
- {
193
- onSuccess: async (id, event) => {
194
- console.debug("🚀 ===> JOB_TRACKING onSuccess", id, event);
195
- // await this.onTrackingInvoke("succeeded", event);
196
- },
197
- onFailure: async (id, event, error) => {
198
- console.debug("🚀 ===> JOB_TRACKING onFailure", id, event, error);
199
- // await this.onTrackingInvoke("failure", event, error);
200
- },
201
- onFailed: async (id, event, error) => {
202
- console.debug("🚀 ===> JOB_TRACKING onFailed", id, event, error);
203
- // await this.onTrackingInvoke("failed", event, error);
204
- },
205
- ...WORKERS_OPTIONS,
206
- }
207
- );
208
- };
209
-
210
- private addVisitorWorker = () => {
211
- this.queue.addWorker(
212
- JOB_VISITOR,
213
- async (id, event) => {
214
- if (this.hasInternetAccess) {
215
- const res = await this.trackVisit(event);
216
- // TODO: parse response
217
- console.debug("🚀 ===> ~ res:", res);
218
- return { ok: true };
219
- }
220
-
221
- throw new Error("Network request failed");
222
- },
223
- {
224
- onSuccess: async (id, event) => {
225
- console.debug("🚀 ===> JOB_VISITOR ~ onSuccess: ~ event:", id, event);
226
- // await this.onTrackingInvoke("succeeded", event);
227
- },
228
- onFailure: async (id, event, error) => {
229
- console.debug("🚀 ===> JOB_VISITOR ~ onFailure: ~ error:", id, error);
230
- // await this.onTrackingInvoke("failure", event, error);
231
- },
232
- onFailed: async (id, event, error) => {
233
- console.debug("🚀 ===> JOB_VISITOR ~ onFailed: ~ error:", id, error);
234
- // await this.onTrackingInvoke("failed", event, error);
235
- },
236
- ...WORKERS_OPTIONS,
237
- }
238
- );
239
- };
240
- }
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- // Reexport the native module. On web, it will be resolved to YawlModule.web.ts
2
- // and on native platforms to YawlModule.ts
3
- export { default } from "./YawlModule";
4
- export * from "./Yawl.types";