Skip to content

Commit

Permalink
Add campaign_ids processing flow
Browse files Browse the repository at this point in the history
Summary: We're adding `campaign_ids` processing flow to save `campaign_ids` from inbound applinks and attach the info to event payload.

Reviewed By: jjiang10, xta0

Differential Revision:
D61232599

Privacy Context Container: L1275109

fbshipit-source-id: 0dc2be09a64e4106f232d9a29974fde9f15c977a
  • Loading branch information
Zilin Zhang authored and facebook-github-bot committed Aug 25, 2024
1 parent 2fa38b2 commit 838c52c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
import android.os.Bundle
import com.facebook.FacebookSdk
import com.facebook.internal.instrument.crashshield.AutoHandleExceptions
import org.json.JSONObject

@AutoHandleExceptions
class AppLinkManager private constructor() {
Expand All @@ -25,6 +27,8 @@ class AppLinkManager private constructor() {

companion object {
const val APPLINK_INFO = "com.facebook.sdk.APPLINK_INFO"
const val APPLINK_DATA_KEY = "al_applink_data"
const val CAMPAIGN_IDS_KEY = "campaign_ids"

@Volatile
private var instance: AppLinkManager? = null
Expand All @@ -38,7 +42,19 @@ class AppLinkManager private constructor() {
}
}

fun handleURL(activity: Activity) {}
fun handleURL(activity: Activity) {
val uri = activity.intent.data ?: return
processCampaignIds(uri)
}

fun processCampaignIds(uri: Uri) {
val applinkData = uri.getQueryParameter(APPLINK_DATA_KEY) ?: return
try {
val json = JSONObject(applinkData)
val campaignIDs = json.getString(CAMPAIGN_IDS_KEY) ?: return
preferences.edit().putString(CAMPAIGN_IDS_KEY, campaignIDs).apply()
} catch (_: Exception) {}
}

fun getInfo(key: String): String? {
return preferences.getString(key, null)
Expand Down
7 changes: 6 additions & 1 deletion facebook-core/src/main/java/com/facebook/internal/Utility.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.facebook.FacebookSdk
import com.facebook.GraphRequest
import com.facebook.HttpMethod
import com.facebook.appevents.UserDataStore
import com.facebook.appevents.internal.AppLinkManager
import com.facebook.internal.ProfileInformationCache.getProfileInformation
import com.facebook.internal.ProfileInformationCache.putProfileInformation
import com.facebook.internal.instrument.crashshield.AutoHandleExceptions
Expand Down Expand Up @@ -618,7 +619,11 @@ object Utility {
params.put("installer_package", attributionIdentifiers.androidInstallerPackage)
}
}
} /* no op */
val campaignIDs = AppLinkManager.getInstance()?.getInfo(AppLinkManager.CAMPAIGN_IDS_KEY)
if (campaignIDs != null) {
params.put("campaign_ids", campaignIDs)
}
}

/**
* Get the app version of the app, as specified by the manifest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

package com.facebook.appevents.internal

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import com.facebook.FacebookPowerMockTestCase
import com.facebook.FacebookSdk
import com.facebook.MockSharedPreference
Expand All @@ -20,16 +23,20 @@ import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

@PrepareForTest(FacebookSdk::class)
class AppLinkManagerTest: FacebookPowerMockTestCase() {

private lateinit var mockApplicationContext: Context
private lateinit var mockSharedPreference: SharedPreferences
private lateinit var mockActivity: Activity

@Before
fun init() {
mockActivity = mock()
mockApplicationContext = mock()
mockSharedPreference = MockSharedPreference()
PowerMockito.mockStatic(FacebookSdk::class.java)
Expand All @@ -44,4 +51,33 @@ class AppLinkManagerTest: FacebookPowerMockTestCase() {
fun testGetInstance() {
assertNotNull(AppLinkManager.getInstance())
}

@Test
fun testHandleInvalidAppLinkData() {
val intent = Intent()
whenever(mockActivity.intent).thenReturn(intent)
intent.setData(Uri.parse("fb123://test.com?al_applink_data=123"))
AppLinkManager.getInstance()?.handleURL(mockActivity)

assertNull(mockSharedPreference.getString("campaign_ids", null))
}

@Test
fun testHandleNullCampaignIDs() {
val intent = Intent()
whenever(mockActivity.intent).thenReturn(intent)
AppLinkManager.getInstance()?.handleURL(mockActivity)

assertNull(mockSharedPreference.getString("campaign_ids", null))
}

@Test
fun testHandleNonnullCampaignIDs() {
val intent = Intent()
whenever(mockActivity.intent).thenReturn(intent)
intent.setData(Uri.parse("fb123://test.com?al_applink_data=%7B%22acs_token%22%3A+%22test_token_1234567%22%2C+%22campaign_ids%22%3A+%22test_campaign_1234%22%2C+%22advertiser_id%22%3A+%22test_advertiserid_12345%22%7D"))
AppLinkManager.getInstance()?.handleURL(mockActivity)

assertEquals("test_campaign_1234", mockSharedPreference.getString("campaign_ids", null))
}
}

0 comments on commit 838c52c

Please sign in to comment.