Skip to content

Commit

Permalink
fix: Implement GA4 360 Parameters Properties Limits
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi Pandya authored and Mansi-mParticle committed Mar 6, 2024
1 parent f3eb779 commit e5e2046
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 11 deletions.
56 changes: 45 additions & 11 deletions src/main/kotlin/com/mparticle/kits/GoogleAnalyticsFirebaseGA4Kit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,21 @@ class GoogleAnalyticsFirebaseGA4Kit : KitIntegration(), KitIntegration.EventList
products?.let {
val bundles = arrayOfNulls<Bundle>(products.size)
for ((i, product) in products.withIndex()) {
val bundle = getBundle(product)
bundles[i] = bundle.bundle
val bundle = getHashmap(product)
val bundleSize = bundle.size
if (bundleSize > itemMaxParameter) {
val additionalParameterSize = bundleSize - itemMaxParameter
val keysToRemove = bundle
.keys
.sortedWith(String.CASE_INSENSITIVE_ORDER)
.takeLast(additionalParameterSize)
keysToRemove.forEach { key ->
bundle.remove(key)
}
}
bundles[i] = hashMapToBundle(bundle).bundle
}
return products.map { getBundle(it).bundle }.toTypedArray()

return products.map { hashMapToBundle(getHashmap(it)).bundle }.toTypedArray()
}
return arrayOf()
}
Expand All @@ -465,13 +475,26 @@ class GoogleAnalyticsFirebaseGA4Kit : KitIntegration(), KitIntegration.EventList

}

private fun getBundle(product: Product): PickyBundle {
return PickyBundle()
.putLong(FirebaseAnalytics.Param.QUANTITY, product.quantity.toLong())
.putString(FirebaseAnalytics.Param.ITEM_ID, product.sku)
.putString(FirebaseAnalytics.Param.ITEM_NAME, product.name)
.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, product.category)
.putDouble(FirebaseAnalytics.Param.PRICE, product.unitPrice)
private fun hashMapToBundle(hashMap: HashMap<String, Any>): PickyBundle {
val bundle = PickyBundle()
for ((key, value) in hashMap) {
when (value) {
is String -> bundle.putString(key, value)
is Int -> bundle.putInt(key, value)
is Double -> bundle.putDouble(key, value)
is Long -> bundle.putLong(key, value)
}
}
return bundle
}

private fun getHashmap(product: Product): HashMap<String, Any> {
return hashMapOf<String, Any>().apply {
put(FirebaseAnalytics.Param.QUANTITY, product.quantity.toLong())
put(FirebaseAnalytics.Param.ITEM_ID, product.sku)
put(FirebaseAnalytics.Param.ITEM_NAME, product.name)
product.category?.let { put(FirebaseAnalytics.Param.ITEM_CATEGORY, it) }
}
}

private fun getValue(commerceEvent: CommerceEvent): Double? {
Expand Down Expand Up @@ -568,9 +591,20 @@ class GoogleAnalyticsFirebaseGA4Kit : KitIntegration(), KitIntegration.EventList
attributeCopy[it] = standardizeValue(value, event)
}
}
limitAttributes(attributeCopy, eventMaxParameterProperty)
return attributeCopy
}

private fun limitAttributes(attributeCopy: HashMap<String, String>, maxCount: Int) {
if (!attributeCopy.isNullOrEmpty() && attributeCopy.size > maxCount) {
val dictionaryKeys = ArrayList(attributeCopy.keys)
dictionaryKeys.sortWith(String.CASE_INSENSITIVE_ORDER)
for (i in maxCount until dictionaryKeys.size) {
attributeCopy.remove(dictionaryKeys[i])
}
}
}

fun standardizeValue(valueIn: String?, event: Boolean): String {
var value = valueIn ?: return ""
if (event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import junit.framework.TestCase
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito
import org.mockito.Mockito.mock
import java.lang.ref.WeakReference
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import java.util.*

Expand Down Expand Up @@ -452,6 +454,90 @@ class GoogleAnalyticsFirebaseGA4KitTest {
firebaseSdk.clearLoggedEvents()
}

@Test
fun testStandardizeAttributes() {
val attributeCopy = hashMapOf(
"test" to "test",
"test1" to "test1",
"test2" to "test2",
"test3" to "test4",
"test5" to "test5"
)
val eventMaxParameterProperty = 3
val method: Method = GoogleAnalyticsFirebaseGA4Kit::class.java.getDeclaredMethod(
"limitAttributes",
HashMap::class.java,
Int::class.javaPrimitiveType
)
method.isAccessible = true

method.invoke(kitInstance, attributeCopy, eventMaxParameterProperty)
Assert.assertEquals(eventMaxParameterProperty, attributeCopy.size)
}
@Test
fun testStandardizeAttributes_attribute_isNull() {
val attributeCopy = HashMap<String, String>()

val eventMaxParameterProperty = 3
val method: Method = GoogleAnalyticsFirebaseGA4Kit::class.java.getDeclaredMethod(
"limitAttributes",
HashMap::class.java,
Int::class.javaPrimitiveType
)
method.isAccessible = true
method.invoke(kitInstance, null, eventMaxParameterProperty)
Assert.assertEquals(0, attributeCopy.size)
}

@Test
fun testStandardizeAttributes_attribute_empty() {
val attributeCopy = HashMap<String, String>()
val eventMaxParameterProperty = 3
val method: Method = GoogleAnalyticsFirebaseGA4Kit::class.java.getDeclaredMethod(
"limitAttributes",
HashMap::class.java,
Int::class.javaPrimitiveType
)
method.isAccessible = true
method.invoke(kitInstance, attributeCopy, eventMaxParameterProperty)
Assert.assertEquals(0, attributeCopy.size)
}

@Test
fun testStandardizeAttributes_attribute_less_than_max_size() {
val attributeCopy = hashMapOf(
"test" to "test",
"test1" to "test1",
)
val eventMaxParameterProperty = 3
val method: Method = GoogleAnalyticsFirebaseGA4Kit::class.java.getDeclaredMethod(
"limitAttributes",
HashMap::class.java,
Int::class.javaPrimitiveType
)
method.isAccessible = true
method.invoke(kitInstance, attributeCopy, eventMaxParameterProperty)
Assert.assertEquals(2, attributeCopy.size)
}

@Test
fun testStandardizeAttributes_attribute_equal_to_max_size() {
val attributeCopy = hashMapOf(
"test" to "test",
"test1" to "test1",
"test2" to "test4",
)
val eventMaxParameterProperty = 3
val method: Method = GoogleAnalyticsFirebaseGA4Kit::class.java.getDeclaredMethod(
"limitAttributes",
HashMap::class.java,
Int::class.javaPrimitiveType
)
method.isAccessible = true
method.invoke(kitInstance, attributeCopy, eventMaxParameterProperty)
Assert.assertEquals(eventMaxParameterProperty, attributeCopy.size)
}

@Test
fun testScreenNameSanitized() {
kitInstance.logScreen("Some long Screen name", null)
Expand Down

0 comments on commit e5e2046

Please sign in to comment.