Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add GAID fetching and saving to google_ad_id #93

Merged
merged 9 commits into from
Jan 10, 2025
2 changes: 2 additions & 0 deletions personalization-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ dependencies {
implementation("androidx.test:rules:1.6.1")
implementation("androidx.test.ext:junit:1.2.1")

implementation("com.google.android.gms:play-services-ads-identifier:17.0.0")

kapt 'com.google.dagger:dagger-compiler:2.48'
}

Expand Down
1 change: 1 addition & 0 deletions personalization-sdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="com.google.android.gms.permission.AD_ID" />
<uses-permission android:name="android.permission.INTERNET" />

<application
Expand Down
10 changes: 10 additions & 0 deletions personalization-sdk/src/main/kotlin/com/personalization/SDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ import com.personalization.sdk.domain.usecases.preferences.InitPreferencesUseCas
import com.personalization.sdk.domain.usecases.recommendation.SetRecommendedByUseCase
import com.personalization.sdk.domain.usecases.userSettings.GetUserSettingsValueUseCase
import com.personalization.sdk.domain.usecases.userSettings.InitUserSettingsUseCase
import com.personalization.sdk.domain.usecases.userSettings.FetchAdvertisingIdUseCase
import com.personalization.stories.StoriesManager
import com.personalization.stories.views.StoriesView
import com.personalization.utils.DomainFormattingUtils.formatApiDomain
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Locale
import javax.inject.Inject
import org.json.JSONException
Expand Down Expand Up @@ -102,6 +106,8 @@ open class SDK {
@Inject
lateinit var notificationHelper: NotificationHelper

@Inject
lateinit var fetchAdvertisingIdUseCase: FetchAdvertisingIdUseCase

/**
* @param shopId Shop key
Expand Down Expand Up @@ -154,6 +160,10 @@ open class SDK {
autoSendPushToken = autoSendPushToken,
needReInitialization = needReInitialization
)

CoroutineScope(Dispatchers.IO).launch {
fetchAdvertisingIdUseCase.invoke()
}
}

private fun initNetworkUseCase(url: String?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.personalization.sdk.data.di

import com.personalization.sdk.data.repositories.advertising.AdvertisingRepositoryImpl
import com.personalization.sdk.data.repositories.network.NetworkRepositoryImpl
import com.personalization.sdk.data.repositories.notification.NotificationRepositoryImpl
import com.personalization.sdk.data.repositories.preferences.PreferencesRepositoryImpl
import com.personalization.sdk.data.repositories.recommendation.RecommendationRepositoryImpl
import com.personalization.sdk.data.repositories.userSettings.UserSettingsRepositoryImpl
import com.personalization.sdk.domain.repositories.AdvertisingRepository
import com.personalization.sdk.domain.repositories.NetworkRepository
import com.personalization.sdk.domain.repositories.NotificationRepository
import com.personalization.sdk.domain.repositories.PreferencesRepository
Expand Down Expand Up @@ -34,4 +36,7 @@ abstract class RepositoriesModule {
@Binds
abstract fun bindNotificationRepository(impl: NotificationRepositoryImpl): NotificationRepository

@Binds
abstract fun bindAdvertisingRepository(impl: AdvertisingRepositoryImpl): AdvertisingRepository

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.personalization.sdk.data.repositories.advertising

import android.content.Context
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.personalization.sdk.domain.repositories.AdvertisingRepository
import javax.inject.Inject

private const val DEFAULT_ADVERTISING_ID = "00000000-0000-0000-0000-000000000000"
TorinAsakura marked this conversation as resolved.
Show resolved Hide resolved

class AdvertisingRepositoryImpl @Inject constructor(
private val context: Context
) : AdvertisingRepository {
override suspend fun fetchAdvertisingId(): String =
try {
AdvertisingIdClient.getAdvertisingIdInfo(context).id
} catch (e: Exception) {
DEFAULT_ADVERTISING_ID
}

}
looee1q marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import org.json.JSONObject

private const val DEFAULT_DID = ""
private const val DEFAULT_SID = ""
private const val DEFAULT_ADVERTISING_ID = "00000000-0000-0000-0000-000000000000"
private const val DEFAULT_SID_LAST_ACT_TIME = 0L

private const val DID_KEY = "did"
private const val SID_KEY = "sid"
private const val ADVERTISING_ID_KEY = "google_ad_id"
private const val SID_LAST_ACT_KEY = "sid_last_act"

class UserSettingsDataSource @AssistedInject constructor(
private val preferencesDataSource: PreferencesDataSource,
@Assisted("shopId") private val shopId: String,
Expand Down Expand Up @@ -52,19 +62,15 @@ class UserSettingsDataSource @AssistedInject constructor(
internal fun saveDid(value: String) = preferencesDataSource.saveValue(DID_KEY, value)
internal fun removeDid() = preferencesDataSource.removeValue(DID_KEY)

internal fun saveAdvertisingId(value: String) =
preferencesDataSource.saveValue(ADVERTISING_ID_KEY, value)

internal fun getAdvertisingId() =
preferencesDataSource.getValue(ADVERTISING_ID_KEY, DEFAULT_ADVERTISING_ID)

internal fun getIsInitialized(): Boolean = isInitialized
internal fun setIsInitialized(value: Boolean) {
isInitialized = value
}

companion object {

private const val DEFAULT_DID = ""
private const val DEFAULT_SID = ""
private const val DEFAULT_SID_LAST_ACT_TIME = 0L

private const val DID_KEY = "did"
private const val SID_KEY = "sid"
private const val SID_LAST_ACT_KEY = "sid_last_act"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.personalization.sdk.data.repositories.userSettings
import com.personalization.sdk.data.di.DataSourcesModule
import com.personalization.sdk.domain.models.NotificationSource
import com.personalization.sdk.domain.repositories.UserSettingsRepository
import javax.inject.Inject
import org.json.JSONObject
import javax.inject.Inject

class UserSettingsRepositoryImpl @Inject constructor(
private val userSettingsDataSourceFactory: DataSourcesModule.UserSettingsDataSourceFactory,
Expand Down Expand Up @@ -59,6 +59,13 @@ class UserSettingsRepositoryImpl @Inject constructor(
userSettingsDataSource.setIsInitialized(value)
}

override fun saveAdvertisingId(value: String) {
userSettingsDataSource.saveAdvertisingId(value)
}

override fun getAdvertisingId(): String =
userSettingsDataSource.getAdvertisingId()

override fun addParams(
params: JSONObject,
notificationSource: NotificationSource?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.personalization.sdk.domain.repositories

interface AdvertisingRepository {
suspend fun fetchAdvertisingId(): String
}
TorinAsakura marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ interface UserSettingsRepository {
fun getIsInitialized(): Boolean
fun updateIsInitialized(value: Boolean)

fun saveAdvertisingId(value: String)
fun getAdvertisingId(): String

fun addParams(
params: JSONObject,
notificationSource: NotificationSource?,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.personalization.sdk.domain.usecases.userSettings

import com.personalization.sdk.domain.repositories.AdvertisingRepository
import com.personalization.sdk.domain.repositories.UserSettingsRepository
import javax.inject.Inject

class FetchAdvertisingIdUseCase @Inject constructor(
TorinAsakura marked this conversation as resolved.
Show resolved Hide resolved
private val advertisingRepository: AdvertisingRepository,
private val userSettingsRepository: UserSettingsRepository
) {
suspend fun invoke() {
val advertisingId = advertisingRepository.fetchAdvertisingId()
userSettingsRepository.saveAdvertisingId(advertisingId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ class GetUserSettingsValueUseCase @Inject constructor(
fun getSidLastActTime(): Long = userSettingsRepository.getSidLastActTime()

fun getIsInitialized(): Boolean = userSettingsRepository.getIsInitialized()

fun getAdvertisingId(): String = userSettingsRepository.getAdvertisingId()
}
Loading