Use IP-based geo
This commit is contained in:
@@ -24,7 +24,7 @@ import {
|
||||
import {type Metrics, metrics} from '#/analytics/metrics'
|
||||
import * as refParams from '#/analytics/misc/refParams'
|
||||
import * as env from '#/env'
|
||||
import {useGeolocation} from '#/geolocation'
|
||||
import {useGeolocationServiceResponse} from '#/geolocation/service'
|
||||
import {device} from '#/storage'
|
||||
|
||||
export * as utils from '#/analytics/utils'
|
||||
@@ -104,7 +104,7 @@ const Context = createContext<AnalyticsBaseContextType>({
|
||||
referrerSrc: refParams.src,
|
||||
referrerUrl: refParams.url,
|
||||
},
|
||||
geolocation: device.get(['mergedGeolocation']) || {
|
||||
geolocation: device.get(['geolocationServiceResponse']) || {
|
||||
countryCode: '',
|
||||
regionCode: '',
|
||||
},
|
||||
@@ -137,7 +137,7 @@ export function AnalyticsContext({
|
||||
}
|
||||
}
|
||||
const sessionId = useSessionId()
|
||||
const geolocation = useGeolocation()
|
||||
const geolocation = useGeolocationServiceResponse()
|
||||
const parentContext = useContext(Context)
|
||||
const childContext = useMemo(() => {
|
||||
const combinedMetadata = {
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
type MergeableMetadata,
|
||||
type SessionMetadata,
|
||||
} from '#/analytics/metadata'
|
||||
import {getIPGeolocationString} from '#/geolocation/util'
|
||||
|
||||
/**
|
||||
* Thin `useMemo` wrapper that marks the metadata as memoized and provides a
|
||||
@@ -47,5 +48,6 @@ export function getAnalyticsHeaders() {
|
||||
return {
|
||||
'X-Bsky-Device-Id': getDeviceId(),
|
||||
'X-Bsky-Session-Id': getSessionId(),
|
||||
'X-Bsky-IP-Geolocation': getIPGeolocationString(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,12 +128,17 @@ export function mergeGeolocations(
|
||||
}
|
||||
|
||||
/**
|
||||
* Get's the merged geolocation as a string in the format of
|
||||
* Get's the IP-based geolocation as a string in the format of
|
||||
* "countryCode-regionCode", or just "countryCode" if regionCode is not
|
||||
* available.
|
||||
*
|
||||
* IMPORTANT: this method should only return IP-based data, not the user's GPS
|
||||
* based data. IP-based data we can already infer from requests, but for
|
||||
* consistency between frontend and backend, we sometimes want to share the
|
||||
* value we have on the frontend with the backend.
|
||||
*/
|
||||
export function getGeolocationString() {
|
||||
const geo = device.get(['mergedGeolocation'])
|
||||
export function getIPGeolocationString() {
|
||||
const geo = device.get(['geolocationServiceResponse'])
|
||||
if (!geo) return
|
||||
const {countryCode, regionCode} = geo
|
||||
if (countryCode) {
|
||||
|
||||
Reference in New Issue
Block a user