From dde80e697a320dfefade4aa232199f496e6c8f4c Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 5 Feb 2025 13:51:10 +0100 Subject: [PATCH 1/3] chore: set locale and timezoen for rn libs --- examples/example_rn/package.json | 1 + posthog-ai/src/anthropic/index.ts | 2 +- posthog-ai/src/vercel/middleware.ts | 4 +- posthog-react-native/package.json | 1 + posthog-react-native/src/native-deps.tsx | 22 +++++ .../optional/OptionalReactNativeLocalize.ts | 21 +++++ yarn.lock | 84 +++++++++++-------- 7 files changed, 99 insertions(+), 36 deletions(-) create mode 100644 posthog-react-native/src/optional/OptionalReactNativeLocalize.ts diff --git a/examples/example_rn/package.json b/examples/example_rn/package.json index 8c46589f..e40135c6 100644 --- a/examples/example_rn/package.json +++ b/examples/example_rn/package.json @@ -24,6 +24,7 @@ "react-native": "0.73.2", "react-native-device-info": "^10.12.0", "react-native-navigation": "^7.37.2", + "react-native-localize": "^3.0.0", "posthog-react-native-session-replay": "^0.1" }, "devDependencies": { diff --git a/posthog-ai/src/anthropic/index.ts b/posthog-ai/src/anthropic/index.ts index 278c2ef4..45a36c8f 100644 --- a/posthog-ai/src/anthropic/index.ts +++ b/posthog-ai/src/anthropic/index.ts @@ -72,7 +72,7 @@ export class WrappedMessages extends AnthropicOriginal.Messages { return parentPromise.then((value) => { const passThroughStream = new PassThrough({ objectMode: true }) let accumulatedContent = '' - let usage: { inputTokens: number; outputTokens: number } = { + const usage: { inputTokens: number; outputTokens: number } = { inputTokens: 0, outputTokens: 0, } diff --git a/posthog-ai/src/vercel/middleware.ts b/posthog-ai/src/vercel/middleware.ts index 17b901c3..61ab338f 100644 --- a/posthog-ai/src/vercel/middleware.ts +++ b/posthog-ai/src/vercel/middleware.ts @@ -83,7 +83,7 @@ export const createInstrumentationMiddleware = ( const middleware: LanguageModelV1Middleware = { wrapGenerate: async ({ doGenerate, params }) => { const startTime = Date.now() - let mergedParams = { + const mergedParams = { ...options, ...mapVercelParams(params), } @@ -143,7 +143,7 @@ export const createInstrumentationMiddleware = ( const startTime = Date.now() let generatedText = '' let usage: { inputTokens?: number; outputTokens?: number } = {} - let mergedParams = { + const mergedParams = { ...options, ...mapVercelParams(params), } diff --git a/posthog-react-native/package.json b/posthog-react-native/package.json index 10304d2b..60846072 100644 --- a/posthog-react-native/package.json +++ b/posthog-react-native/package.json @@ -33,6 +33,7 @@ "react-native": "^0.69.1", "react-native-device-info": "^10.3.0", "react-native-navigation": "^6.0.0", + "react-native-localize": "^3.0.0", "posthog-react-native-session-replay": "^0.1" }, "peerDependencies": { diff --git a/posthog-react-native/src/native-deps.tsx b/posthog-react-native/src/native-deps.tsx index 62ee425b..2687e22e 100644 --- a/posthog-react-native/src/native-deps.tsx +++ b/posthog-react-native/src/native-deps.tsx @@ -6,6 +6,7 @@ import { OptionalExpoFileSystem } from './optional/OptionalExpoFileSystem' import { OptionalExpoLocalization } from './optional/OptionalExpoLocalization' import { OptionalReactNativeDeviceInfo } from './optional/OptionalReactNativeDeviceInfo' import { PostHogCustomAppProperties, PostHogCustomStorage } from './types' +import { OptionalReactNativeLocalize } from './optional/OptionalReactNativeLocalize' export const getAppProperties = (): PostHogCustomAppProperties => { let deviceType = 'Mobile' @@ -58,6 +59,27 @@ export const getAppProperties = (): PostHogCustomAppProperties => { if (OptionalExpoLocalization) { properties.$locale = OptionalExpoLocalization.locale properties.$timezone = OptionalExpoLocalization.timezone + } else if (OptionalReactNativeLocalize) { + const localesFn = OptionalReactNativeLocalize.getLocales + if (localesFn) { + const locales = localesFn() + + if (locales && locales.length > 0) { + const languageTag = locales[0].languageTag + if (languageTag) { + properties.$locale = languageTag + } + } + } + + const timezoneFn = OptionalReactNativeLocalize.getTimeZone + if (timezoneFn) { + const timezone = timezoneFn() + + if (timezone) { + properties.$timezone = timezone + } + } } return properties diff --git a/posthog-react-native/src/optional/OptionalReactNativeLocalize.ts b/posthog-react-native/src/optional/OptionalReactNativeLocalize.ts new file mode 100644 index 00000000..0a6886bc --- /dev/null +++ b/posthog-react-native/src/optional/OptionalReactNativeLocalize.ts @@ -0,0 +1,21 @@ +interface ReactNativeLocalize { + getLocales: () => { + languageCode: string + scriptCode?: string + countryCode: string + languageTag: string + isRTL: boolean + }[] + + getTimeZone(): string +} + +import type ReactNativeLocalize from 'react-native-localize' + +export let OptionalReactNativeLocalize: typeof ReactNativeLocalize | undefined = undefined + +// web support requires webpack +// https://github.com/zoontek/react-native-localize#web-support +try { + OptionalReactNativeLocalize = require('react-native-localize') +} catch (e) {} diff --git a/yarn.lock b/yarn.lock index 9cdd0067..f859a86c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7,40 +7,40 @@ resolved "https://registry.yarnpkg.com/@0no-co/graphql.web/-/graphql.web-1.0.9.tgz#b0d053a5b892ea3e2f42a4ea2a92315b517d65d3" integrity sha512-lXSg4bDHvP8CiMdpQf9f/rca12IIjXHN/p0Rc5mgzgLe4JBlIoA1zFa9NKhfG1bW0OyI2hgaOldFCfkEQwZuEQ== -"@ai-sdk/provider-utils@2.1.0": - version "2.1.0" - resolved "https://registry.yarnpkg.com/@ai-sdk/provider-utils/-/provider-utils-2.1.0.tgz#40f060252a192a31063300dd277a04df0eba604e" - integrity sha512-rBUabNoyB25PBUjaiMSk86fHNSCqTngNZVvXxv8+6mvw47JX5OexW+ZHRsEw8XKTE8+hqvNFVzctaOrRZ2i9Zw== +"@ai-sdk/provider-utils@2.1.6": + version "2.1.6" + resolved "https://registry.yarnpkg.com/@ai-sdk/provider-utils/-/provider-utils-2.1.6.tgz#5b2bff2d44ffa8ec629109ea2b28d2a7528104b4" + integrity sha512-Pfyaj0QZS22qyVn5Iz7IXcJ8nKIKlu2MeSAdKJzTwkAks7zdLaKVB+396Rqcp1bfQnxl7vaduQVMQiXUrgK8Gw== dependencies: - "@ai-sdk/provider" "1.0.4" + "@ai-sdk/provider" "1.0.7" eventsource-parser "^3.0.0" nanoid "^3.3.8" secure-json-parse "^2.7.0" -"@ai-sdk/provider@1.0.4": - version "1.0.4" - resolved "https://registry.yarnpkg.com/@ai-sdk/provider/-/provider-1.0.4.tgz#fe041f09e053235922ab9706be29fedf63c53bd0" - integrity sha512-lJi5zwDosvvZER3e/pB8lj1MN3o3S7zJliQq56BRr4e9V3fcRyFtwP0JRxaRS5vHYX3OJ154VezVoQNrk0eaKw== +"@ai-sdk/provider@1.0.7": + version "1.0.7" + resolved "https://registry.yarnpkg.com/@ai-sdk/provider/-/provider-1.0.7.tgz#66e07407678153b514aebf3f1c3c035e5e833380" + integrity sha512-q1PJEZ0qD9rVR+8JFEd01/QM++csMT5UVwYXSN2u54BrVw/D8TZLTeg2FEfKK00DgAx0UtWd8XOhhwITP9BT5g== dependencies: json-schema "^0.4.0" -"@ai-sdk/react@1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@ai-sdk/react/-/react-1.1.0.tgz#7059fb3981818667856d3f9afff312e429c0d6b8" - integrity sha512-U5lBbLyf1pw79xsk5dgHSkBv9Jta3xzWlOLpxsmHlxh1X94QOH3e1gm+nioQ/JvTuHLm23j2tz3i4MpMdchwXQ== +"@ai-sdk/react@1.1.9": + version "1.1.9" + resolved "https://registry.yarnpkg.com/@ai-sdk/react/-/react-1.1.9.tgz#9ec1f2637071a8e65108d10c4774e2cd31f1f013" + integrity sha512-2si293+NYs3WbPfHXSZ4/71NtYV0zxYhhHSL4H1EPyHU9Gf/H81rhjsslvt45mguPecPkMG19/VIXDjJ4uTwsw== dependencies: - "@ai-sdk/provider-utils" "2.1.0" - "@ai-sdk/ui-utils" "1.1.0" + "@ai-sdk/provider-utils" "2.1.6" + "@ai-sdk/ui-utils" "1.1.9" swr "^2.2.5" throttleit "2.1.0" -"@ai-sdk/ui-utils@1.1.0": - version "1.1.0" - resolved "https://registry.yarnpkg.com/@ai-sdk/ui-utils/-/ui-utils-1.1.0.tgz#4c31029bae0d06fd2c2530dd214eff434c5a3787" - integrity sha512-ETXwdHaHwzC7NIehbthDFGwsTFk+gNtRL/lm85nR4WDFvvYQptoM/7wTANs0p0H7zumB3Ep5hKzv0Encu8vSRw== +"@ai-sdk/ui-utils@1.1.9": + version "1.1.9" + resolved "https://registry.yarnpkg.com/@ai-sdk/ui-utils/-/ui-utils-1.1.9.tgz#37d9125c359c5de85026a9c5cb17b42e81d9dcd6" + integrity sha512-o0tDopdtHqgr9FAx0qSkdwPUDSdX+4l42YOn70zvs6+O+PILeTpf2YYV5Xr32TbNfSUq1DWLLhU1O7/3Dsxm1Q== dependencies: - "@ai-sdk/provider" "1.0.4" - "@ai-sdk/provider-utils" "2.1.0" + "@ai-sdk/provider" "1.0.7" + "@ai-sdk/provider-utils" "2.1.6" zod-to-json-schema "^3.24.1" "@ampproject/remapping@^2.2.0": @@ -51,6 +51,19 @@ "@jridgewell/gen-mapping" "^0.3.5" "@jridgewell/trace-mapping" "^0.3.24" +"@anthropic-ai/sdk@^0.36.3": + version "0.36.3" + resolved "https://registry.yarnpkg.com/@anthropic-ai/sdk/-/sdk-0.36.3.tgz#0ca754f047e2847c8b1d064550f05e9a8e7e16b3" + integrity sha512-+c0mMLxL/17yFZ4P5+U6bTWiCSFZUKJddrv01ud2aFBWnTPLdRncYV76D3q1tqfnL7aCnhRtykFnoCFzvr4U3Q== + dependencies: + "@types/node" "^18.11.18" + "@types/node-fetch" "^2.6.4" + abort-controller "^3.0.0" + agentkeepalive "^4.2.1" + form-data-encoder "1.7.2" + formdata-node "^4.3.2" + node-fetch "^2.6.7" + "@babel/cli@^7.19.3": version "7.25.9" resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.25.9.tgz#51036166fd0e9cfb26eee1b9ddc264a0d6d5f843" @@ -2927,15 +2940,15 @@ aggregate-error@^3.0.0: clean-stack "^2.0.0" indent-string "^4.0.0" -ai@^4.1.0: - version "4.1.0" - resolved "https://registry.yarnpkg.com/ai/-/ai-4.1.0.tgz#3150a91da72fb9dd7df25a781554f9a77114ca4a" - integrity sha512-95nI9hBSSAKPrMnpJbaB3yqvh+G8BS4/EtFz3HR0HgEDJpxC0R6JAlB8+B/BXHd/roNGBrS08Z3Zain/6OFSYA== +ai@^4.0.0: + version "4.1.19" + resolved "https://registry.yarnpkg.com/ai/-/ai-4.1.19.tgz#bd67eb2d502c6df5c20f7fe4d18f3c8d2d9a564e" + integrity sha512-Xx498vbFVN4Y3F4kWF59ojLyn/d++NbSZwENq1zuSFW4OjwzTf79jtMxD+BYeMiDH+mgIrmROY/ONtqMOchZGw== dependencies: - "@ai-sdk/provider" "1.0.4" - "@ai-sdk/provider-utils" "2.1.0" - "@ai-sdk/react" "1.1.0" - "@ai-sdk/ui-utils" "1.1.0" + "@ai-sdk/provider" "1.0.7" + "@ai-sdk/provider-utils" "2.1.6" + "@ai-sdk/react" "1.1.9" + "@ai-sdk/ui-utils" "1.1.9" "@opentelemetry/api" "1.9.0" jsondiffpatch "0.6.0" @@ -8447,10 +8460,10 @@ open@^8.0.4, open@^8.3.0: is-docker "^2.1.1" is-wsl "^2.2.0" -openai@^4.79.1: - version "4.79.1" - resolved "https://registry.yarnpkg.com/openai/-/openai-4.79.1.tgz#2976f4d20d577cb5f9b7c6664de08745348d5461" - integrity sha512-M7P5/PKnT/S/B5v0D64giC9mjyxFYkqlCuQFzR5hkdzMdqUuHf8T1gHhPGPF5oAvu4+PO3TvJv/qhZoS2bqAkw== +openai@^4.0.0: + version "4.82.0" + resolved "https://registry.yarnpkg.com/openai/-/openai-4.82.0.tgz#9a28ba6552f94639c2de4278255ad669940b6267" + integrity sha512-1bTxOVGZuVGsKKUWbh3BEwX1QxIXUftJv+9COhhGGVDTFwiaOd4gWsMynF2ewj1mg6by3/O+U8+EEHpWRdPaJg== dependencies: "@types/node" "^18.11.18" "@types/node-fetch" "^2.6.4" @@ -8984,6 +8997,11 @@ react-native-gradle-plugin@^0.0.7: resolved "https://registry.yarnpkg.com/react-native-gradle-plugin/-/react-native-gradle-plugin-0.0.7.tgz#96602f909745239deab7b589443f14fce5da2056" integrity sha512-+4JpbIx42zGTONhBTIXSyfyHICHC29VTvhkkoUOJAh/XHPEixpuBduYgf6Y4y9wsN1ARlQhBBoptTvXvAFQf5g== +react-native-localize@^3.0.0: + version "3.4.1" + resolved "https://registry.yarnpkg.com/react-native-localize/-/react-native-localize-3.4.1.tgz#df07c13231b4e8cc949e68513b124105f861229c" + integrity sha512-NJqJGBUpHtD/MpLCCkrNiqNZ+xFwbHCivxaoN1Oeb8tBAiZr/IqgP3E+MgnqmmdTMOJ33llUfiW3EM6pEIb33w== + react-native-navigation@^6.0.0: version "6.12.2" resolved "https://registry.yarnpkg.com/react-native-navigation/-/react-native-navigation-6.12.2.tgz#1389534e868fe4774a1e567b232d59496d8c4c6f" From a1373c1747a601cc74a79fdb9374ea61aa448910 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 5 Feb 2025 13:55:18 +0100 Subject: [PATCH 2/3] changelog --- posthog-react-native/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/posthog-react-native/CHANGELOG.md b/posthog-react-native/CHANGELOG.md index bbfdd0fe..68ced2d1 100644 --- a/posthog-react-native/CHANGELOG.md +++ b/posthog-react-native/CHANGELOG.md @@ -1,5 +1,7 @@ # Next +1. chore: set locale and timezone using the react-native-localize library + # 3.6.4 - 2025-02-03 1. fix: improve session replay linked flag type handling From ff2a8f3db05cdd581c3cabb977b60728a8b2f671 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 5 Feb 2025 18:25:21 +0100 Subject: [PATCH 3/3] version --- posthog-react-native/CHANGELOG.md | 2 ++ posthog-react-native/package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/posthog-react-native/CHANGELOG.md b/posthog-react-native/CHANGELOG.md index 68ced2d1..6ab6062e 100644 --- a/posthog-react-native/CHANGELOG.md +++ b/posthog-react-native/CHANGELOG.md @@ -1,5 +1,7 @@ # Next +# 3.7.0 - 2025-02-05 + 1. chore: set locale and timezone using the react-native-localize library # 3.6.4 - 2025-02-03 diff --git a/posthog-react-native/package.json b/posthog-react-native/package.json index 60846072..e6b7e3cd 100644 --- a/posthog-react-native/package.json +++ b/posthog-react-native/package.json @@ -1,6 +1,6 @@ { "name": "posthog-react-native", - "version": "3.6.4", + "version": "3.7.0", "main": "lib/posthog-react-native/index.js", "files": [ "lib/"