Skip to content

Commit

Permalink
Merge pull request #141 from agrim-deriv/Agrim/networkinfo
Browse files Browse the repository at this point in the history
Agrim/networkinfo
  • Loading branch information
prince-deriv authored Nov 5, 2024
2 parents 538fb0b + 3215b1f commit e31b0dd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { RudderStack } from './rudderstack'
import Cookies from 'js-cookie'
import { TCoreAttributes, TEvents, TGrowthbookAttributes, TGrowthbookOptions } from './types'

declare global {
interface Window {
AnalyticsInstance: ReturnType<typeof createAnalyticsInstance>
}
}
type Options = {
growthbookKey?: string
growthbookDecryptionKey?: string
Expand Down Expand Up @@ -80,6 +85,15 @@ export function createAnalyticsInstance(options?: Options) {
...(growthbookOptions?.attributes?.email_hash && {
email_hash: growthbookOptions?.attributes.email_hash,
}),
...(growthbookOptions?.attributes?.network_type && {
network_type: growthbookOptions?.attributes.network_type,
}),
...(growthbookOptions?.attributes?.network_rtt && {
network_rtt: growthbookOptions?.attributes.network_rtt,
}),
...(growthbookOptions?.attributes?.network_downlink && {
network_downlink: growthbookOptions?.attributes.network_downlink,
}),
}
growthbookOptions ??= {}
growthbookOptions.attributes ??= {}
Expand Down Expand Up @@ -120,6 +134,9 @@ export function createAnalyticsInstance(options?: Options) {
domain,
geo_location,
loggedIn,
network_downlink,
network_rtt,
network_type,
}: TCoreAttributes) => {
if (!_growthbook && !_rudderstack) return

Expand Down Expand Up @@ -156,6 +173,9 @@ export function createAnalyticsInstance(options?: Options) {
...(device_type && { device_type }),
...(url && { url }),
...(loggedIn && { loggedIn }),
...(network_downlink && { network_downlink }),
...(network_rtt && { network_rtt }),
...(network_type && { network_type }),
}
}

Expand Down Expand Up @@ -210,7 +230,7 @@ export function createAnalyticsInstance(options?: Options) {

const getInstances = () => ({ ab: _growthbook, tracking: _rudderstack })

return {
const AnalyticsInstance = {
initialise,
setAttributes,
identifyEvent,
Expand All @@ -225,6 +245,12 @@ export function createAnalyticsInstance(options?: Options) {
pageView,
reset,
}

if (typeof window !== 'undefined') {
window.AnalyticsInstance = AnalyticsInstance
}

return AnalyticsInstance
}

export const Analytics = createAnalyticsInstance()
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export type TCoreAttributes = {
residence_country?: string
geo_location?: string
email_hash?: string
network_type?: string
network_rtt?: number
network_downlink?: number
} & Partial<TGrowthbookAttributes>

type SignupProvider = 'email' | 'phone' | 'google' | 'facebook' | 'apple'
Expand Down

0 comments on commit e31b0dd

Please sign in to comment.