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.FetchGaIdUseCase
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 fetchGaIdUseCase: FetchGaIdUseCase
looee1q marked this conversation as resolved.
Show resolved Hide resolved

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

CoroutineScope(Dispatchers.IO).launch {
fetchGaIdUseCase.invoke(context)
}
}

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

import com.personalization.sdk.data.repositories.gaid.GaIdDataSource
import com.personalization.sdk.data.repositories.network.NetworkDataSource
import com.personalization.sdk.data.repositories.notification.NotificationDataSource
import com.personalization.sdk.data.repositories.preferences.PreferencesDataSource
Expand Down Expand Up @@ -44,4 +45,8 @@ class DataSourcesModule {
) = NotificationDataSource(
preferencesDataSource = preferencesDataSource
)

@Provides
@Singleton
fun provideGaIdDataSource() = GaIdDataSource()
}
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.gaid.GaIdRepositoryImpl
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.GaIdRepository
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 bindGaIdRepository(impl: GaIdRepositoryImpl): GaIdRepository

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

import android.content.Context
import com.google.android.gms.ads.identifier.AdvertisingIdClient

class GaIdDataSource {
internal fun fetchGaId(context: Context): String =
TorinAsakura marked this conversation as resolved.
Show resolved Hide resolved
AdvertisingIdClient.getAdvertisingIdInfo(context).id
looee1q marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.personalization.sdk.data.repositories.gaid

import android.content.Context
import com.personalization.sdk.domain.repositories.GaIdRepository
import javax.inject.Inject

class GaIdRepositoryImpl @Inject constructor(
private val gaIdDataSource: GaIdDataSource
) : GaIdRepository {
override suspend fun fetchAdId(context: Context): String =
gaIdDataSource.fetchGaId(context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class UserSettingsDataSource @AssistedInject constructor(
internal fun saveDid(value: String) = preferencesDataSource.saveValue(DID_KEY, value)
internal fun removeDid() = preferencesDataSource.removeValue(DID_KEY)

internal fun saveGAID(value: String) = preferencesDataSource.saveValue(GAID_KEY, value)

internal fun getIsInitialized(): Boolean = isInitialized
internal fun setIsInitialized(value: Boolean) {
isInitialized = value
Expand All @@ -65,6 +67,7 @@ class UserSettingsDataSource @AssistedInject constructor(

private const val DID_KEY = "did"
private const val SID_KEY = "sid"
private const val GAID_KEY = "google_ad_id"
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,10 @@ class UserSettingsRepositoryImpl @Inject constructor(
userSettingsDataSource.setIsInitialized(value)
}

override fun saveGaId(value: String) {
userSettingsDataSource.saveGAID(value)
}

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

import android.content.Context

interface GaIdRepository {
suspend fun fetchAdId(context: Context): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface UserSettingsRepository {
fun getIsInitialized(): Boolean
fun updateIsInitialized(value: Boolean)

fun saveGaId(value: String)

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

import android.content.Context
looee1q marked this conversation as resolved.
Show resolved Hide resolved
import com.personalization.sdk.domain.repositories.GaIdRepository
import com.personalization.sdk.domain.repositories.UserSettingsRepository
import javax.inject.Inject

class FetchGaIdUseCase @Inject constructor(
private val gaIdRepository: GaIdRepository,
private val userSettingsRepository: UserSettingsRepository
) {
suspend fun invoke(context: Context) {
val gaId = gaIdRepository.fetchAdId(context)
userSettingsRepository.saveGaId(gaId)
looee1q marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading