Use IP-based geo
This commit is contained in:
@@ -24,7 +24,7 @@ import {
|
|||||||
import {type Metrics, metrics} from '#/analytics/metrics'
|
import {type Metrics, metrics} from '#/analytics/metrics'
|
||||||
import * as refParams from '#/analytics/misc/refParams'
|
import * as refParams from '#/analytics/misc/refParams'
|
||||||
import * as env from '#/env'
|
import * as env from '#/env'
|
||||||
import {useGeolocation} from '#/geolocation'
|
import {useGeolocationServiceResponse} from '#/geolocation/service'
|
||||||
import {device} from '#/storage'
|
import {device} from '#/storage'
|
||||||
|
|
||||||
export * as utils from '#/analytics/utils'
|
export * as utils from '#/analytics/utils'
|
||||||
@@ -104,7 +104,7 @@ const Context = createContext<AnalyticsBaseContextType>({
|
|||||||
referrerSrc: refParams.src,
|
referrerSrc: refParams.src,
|
||||||
referrerUrl: refParams.url,
|
referrerUrl: refParams.url,
|
||||||
},
|
},
|
||||||
geolocation: device.get(['mergedGeolocation']) || {
|
geolocation: device.get(['geolocationServiceResponse']) || {
|
||||||
countryCode: '',
|
countryCode: '',
|
||||||
regionCode: '',
|
regionCode: '',
|
||||||
},
|
},
|
||||||
@@ -137,7 +137,7 @@ export function AnalyticsContext({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const sessionId = useSessionId()
|
const sessionId = useSessionId()
|
||||||
const geolocation = useGeolocation()
|
const geolocation = useGeolocationServiceResponse()
|
||||||
const parentContext = useContext(Context)
|
const parentContext = useContext(Context)
|
||||||
const childContext = useMemo(() => {
|
const childContext = useMemo(() => {
|
||||||
const combinedMetadata = {
|
const combinedMetadata = {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
type MergeableMetadata,
|
type MergeableMetadata,
|
||||||
type SessionMetadata,
|
type SessionMetadata,
|
||||||
} from '#/analytics/metadata'
|
} from '#/analytics/metadata'
|
||||||
|
import {getIPGeolocationString} from '#/geolocation/util'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Thin `useMemo` wrapper that marks the metadata as memoized and provides a
|
* Thin `useMemo` wrapper that marks the metadata as memoized and provides a
|
||||||
@@ -47,5 +48,6 @@ export function getAnalyticsHeaders() {
|
|||||||
return {
|
return {
|
||||||
'X-Bsky-Device-Id': getDeviceId(),
|
'X-Bsky-Device-Id': getDeviceId(),
|
||||||
'X-Bsky-Session-Id': getSessionId(),
|
'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
|
* "countryCode-regionCode", or just "countryCode" if regionCode is not
|
||||||
* available.
|
* 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() {
|
export function getIPGeolocationString() {
|
||||||
const geo = device.get(['mergedGeolocation'])
|
const geo = device.get(['geolocationServiceResponse'])
|
||||||
if (!geo) return
|
if (!geo) return
|
||||||
const {countryCode, regionCode} = geo
|
const {countryCode, regionCode} = geo
|
||||||
if (countryCode) {
|
if (countryCode) {
|
||||||
|
|||||||
Reference in New Issue
Block a user