referrers for all platforms (#4514)
This commit is contained in:
parent
83e8522e0a
commit
8b121af2e4
12 changed files with 213 additions and 34 deletions
|
@ -1,5 +1,8 @@
|
|||
package expo.modules.blueskyswissarmy.referrer
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import com.android.installreferrer.api.InstallReferrerClient
|
||||
import com.android.installreferrer.api.InstallReferrerStateListener
|
||||
|
@ -8,10 +11,53 @@ import expo.modules.kotlin.modules.Module
|
|||
import expo.modules.kotlin.modules.ModuleDefinition
|
||||
|
||||
class ExpoBlueskyReferrerModule : Module() {
|
||||
private var intent: Intent? = null
|
||||
private var activityReferrer: Uri? = null
|
||||
|
||||
override fun definition() =
|
||||
ModuleDefinition {
|
||||
Name("ExpoBlueskyReferrer")
|
||||
|
||||
OnNewIntent {
|
||||
intent = it
|
||||
activityReferrer = appContext.currentActivity?.referrer
|
||||
}
|
||||
|
||||
AsyncFunction("getReferrerInfoAsync") {
|
||||
val intentReferrer =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
intent?.getParcelableExtra(Intent.EXTRA_REFERRER, Uri::class.java)
|
||||
} else {
|
||||
intent?.getParcelableExtra(Intent.EXTRA_REFERRER)
|
||||
}
|
||||
|
||||
// Some apps explicitly set a referrer, like Chrome. In these cases, we prefer this since
|
||||
// it's the actual website that the user came from rather than the app.
|
||||
if (intentReferrer is Uri) {
|
||||
val res =
|
||||
mapOf(
|
||||
"referrer" to intentReferrer.toString(),
|
||||
"hostname" to intentReferrer.host,
|
||||
)
|
||||
intent = null
|
||||
return@AsyncFunction res
|
||||
}
|
||||
|
||||
// In all other cases, we'll just record the app that sent the intent.
|
||||
if (activityReferrer != null) {
|
||||
// referrer could become null here. `.toString()` though can be called on null
|
||||
val res =
|
||||
mapOf(
|
||||
"referrer" to activityReferrer.toString(),
|
||||
"hostname" to (activityReferrer?.host ?: ""),
|
||||
)
|
||||
activityReferrer = null
|
||||
return@AsyncFunction res
|
||||
}
|
||||
|
||||
return@AsyncFunction null
|
||||
}
|
||||
|
||||
AsyncFunction("getGooglePlayReferrerInfoAsync") { promise: Promise ->
|
||||
val referrerClient = InstallReferrerClient.newBuilder(appContext.reactContext).build()
|
||||
referrerClient.startConnection(
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
import {requireNativeModule} from 'expo'
|
||||
|
||||
import {GooglePlayReferrerInfo} from './types'
|
||||
import {GooglePlayReferrerInfo, ReferrerInfo} from './types'
|
||||
|
||||
export const NativeModule = requireNativeModule('ExpoBlueskyReferrer')
|
||||
|
||||
export function getGooglePlayReferrerInfoAsync(): Promise<GooglePlayReferrerInfo> {
|
||||
export function getGooglePlayReferrerInfoAsync(): Promise<GooglePlayReferrerInfo | null> {
|
||||
return NativeModule.getGooglePlayReferrerInfoAsync()
|
||||
}
|
||||
|
||||
export function getReferrerInfoAsync(): Promise<ReferrerInfo | null> {
|
||||
return NativeModule.getReferrerInfoAsync()
|
||||
}
|
||||
|
|
37
modules/expo-bluesky-swiss-army/src/Referrer/index.ios.ts
Normal file
37
modules/expo-bluesky-swiss-army/src/Referrer/index.ios.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import {SharedPrefs} from '../../index'
|
||||
import {NotImplementedError} from '../NotImplemented'
|
||||
import {GooglePlayReferrerInfo, ReferrerInfo} from './types'
|
||||
|
||||
export function getGooglePlayReferrerInfoAsync(): Promise<GooglePlayReferrerInfo> {
|
||||
throw new NotImplementedError()
|
||||
}
|
||||
|
||||
export function getReferrerInfoAsync(): Promise<ReferrerInfo | null> {
|
||||
const referrer = SharedPrefs.getString('referrer')
|
||||
if (referrer) {
|
||||
SharedPrefs.removeValue('referrer')
|
||||
try {
|
||||
const url = new URL(referrer)
|
||||
return {
|
||||
referrer,
|
||||
hostname: url.hostname,
|
||||
}
|
||||
} catch (e) {
|
||||
return {
|
||||
referrer,
|
||||
hostname: undefined,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const referrerApp = SharedPrefs.getString('referrerApp')
|
||||
if (referrerApp) {
|
||||
SharedPrefs.removeValue('referrerApp')
|
||||
return {
|
||||
referrer: referrerApp,
|
||||
hostname: referrerApp,
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
|
@ -1,7 +1,10 @@
|
|||
import {NotImplementedError} from '../NotImplemented'
|
||||
import {GooglePlayReferrerInfo} from './types'
|
||||
import {GooglePlayReferrerInfo, ReferrerInfo} from './types'
|
||||
|
||||
// @ts-ignore throws
|
||||
export function getGooglePlayReferrerInfoAsync(): Promise<GooglePlayReferrerInfo> {
|
||||
throw new NotImplementedError()
|
||||
}
|
||||
|
||||
export function getReferrerInfoAsync(): Promise<ReferrerInfo | null> {
|
||||
throw new NotImplementedError()
|
||||
}
|
||||
|
|
34
modules/expo-bluesky-swiss-army/src/Referrer/index.web.ts
Normal file
34
modules/expo-bluesky-swiss-army/src/Referrer/index.web.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import {Platform} from 'react-native'
|
||||
|
||||
import {NotImplementedError} from '../NotImplemented'
|
||||
import {GooglePlayReferrerInfo, ReferrerInfo} from './types'
|
||||
|
||||
export function getGooglePlayReferrerInfoAsync(): Promise<GooglePlayReferrerInfo> {
|
||||
throw new NotImplementedError()
|
||||
}
|
||||
|
||||
export function getReferrerInfoAsync(): Promise<ReferrerInfo | null> {
|
||||
if (
|
||||
Platform.OS === 'web' &&
|
||||
// for ssr
|
||||
typeof document !== 'undefined' &&
|
||||
document != null &&
|
||||
document.referrer
|
||||
) {
|
||||
try {
|
||||
const url = new URL(document.referrer)
|
||||
if (url.hostname !== 'bsky.app') {
|
||||
return {
|
||||
referrer: url.href,
|
||||
hostname: url.hostname,
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// If something happens to the URL parsing, we don't want to actually cause any problems for the user. Just
|
||||
// log the error so we might catch it
|
||||
console.error('Failed to parse referrer URL')
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
|
@ -1,7 +1,10 @@
|
|||
export type GooglePlayReferrerInfo =
|
||||
| {
|
||||
installReferrer?: string
|
||||
clickTimestamp?: number
|
||||
installTimestamp?: number
|
||||
}
|
||||
| undefined
|
||||
export type GooglePlayReferrerInfo = {
|
||||
installReferrer?: string
|
||||
clickTimestamp?: number
|
||||
installTimestamp?: number
|
||||
}
|
||||
|
||||
export type ReferrerInfo = {
|
||||
referrer: string
|
||||
hostname: string
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue