Skip to content

Commit

Permalink
fix: general analytics fixes (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinpaypal committed May 14, 2024
1 parent d3293c0 commit ff2ac5a
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 94 deletions.
12 changes: 6 additions & 6 deletions demo/src/main/java/com/paypal/messagesdemo/JetpackActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import com.paypal.messages.config.PayPalMessageOfferType
import com.paypal.messages.config.message.PayPalMessageConfig
import com.paypal.messages.config.message.PayPalMessageData
import com.paypal.messages.config.message.PayPalMessageViewStateCallbacks
import com.paypal.messages.config.message.style.PayPalMessageAlign
import com.paypal.messages.config.message.style.PayPalMessageAlignment
import com.paypal.messages.config.message.style.PayPalMessageColor
import com.paypal.messages.config.message.style.PayPalMessageLogoType
import com.paypal.messages.io.Api
Expand Down Expand Up @@ -84,9 +84,9 @@ class JetpackActivity : ComponentActivity() {

// Style Alignment
val alignmentGroupOptions = listOf(
PayPalMessageAlign.LEFT,
PayPalMessageAlign.CENTER,
PayPalMessageAlign.RIGHT,
PayPalMessageAlignment.LEFT,
PayPalMessageAlignment.CENTER,
PayPalMessageAlignment.RIGHT,
)
var messageAlignment by remember { mutableStateOf(alignmentGroupOptions[0]) }

Expand Down Expand Up @@ -143,7 +143,7 @@ class JetpackActivity : ComponentActivity() {
backgroundColor = if (messageColor === PayPalMessageColor.WHITE) Color.Black else Color.White
messageView.color = messageColor
messageView.logoType = messageLogo
messageView.textAlign = messageAlignment
messageView.textAlignment = messageAlignment

messageView.offerType = when (offerType) {
offerGroupOptions[0] -> PayPalMessageOfferType.PAY_LATER_SHORT_TERM
Expand Down Expand Up @@ -233,7 +233,7 @@ class JetpackActivity : ComponentActivity() {
RadioOptions(
logoGroupOptions = alignmentGroupOptions,
selected = messageAlignment,
onSelected = { text: PayPalMessageAlign ->
onSelected = { text: PayPalMessageAlignment ->
messageAlignment = text
},
)
Expand Down
17 changes: 9 additions & 8 deletions demo/src/main/java/com/paypal/messagesdemo/XmlActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.paypal.messages.config.PayPalMessagePageType
import com.paypal.messages.config.message.PayPalMessageConfig
import com.paypal.messages.config.message.PayPalMessageData
import com.paypal.messages.config.message.PayPalMessageViewStateCallbacks
import com.paypal.messages.config.message.style.PayPalMessageAlign
import com.paypal.messages.config.message.style.PayPalMessageAlignment
import com.paypal.messages.config.message.style.PayPalMessageColor
import com.paypal.messages.config.message.style.PayPalMessageLogoType
import com.paypal.messages.io.Api
Expand All @@ -26,7 +26,7 @@ class XmlActivity : AppCompatActivity() {
private val TAG = "PPM:XmlActivity"
private var color: PayPalMessageColor = PayPalMessageColor.BLACK
private var logoType: PayPalMessageLogoType = PayPalMessageLogoType.PRIMARY
private var textAlign: PayPalMessageAlign = PayPalMessageAlign.LEFT
private var textAlignment: PayPalMessageAlignment = PayPalMessageAlignment.LEFT
private var offerType: PayPalMessageOfferType? = null
private val environment = PayPalEnvironment.SANDBOX

Expand All @@ -45,6 +45,7 @@ class XmlActivity : AppCompatActivity() {
data = PayPalMessageData(
clientID = getString(R.string.client_id),
environment = environment,
pageType = PayPalMessagePageType.CART,
),
viewStateCallbacks = PayPalMessageViewStateCallbacks(
onLoading = {
Expand Down Expand Up @@ -105,11 +106,11 @@ class XmlActivity : AppCompatActivity() {

val alignmentRadioGroup = binding.alignmentRadioGroup
alignmentRadioGroup.setOnCheckedChangeListener { _, checkedId ->
textAlign = when (checkedId) {
R.id.styleLeft -> PayPalMessageAlign.LEFT
R.id.styleCenter -> PayPalMessageAlign.CENTER
R.id.styleRight -> PayPalMessageAlign.RIGHT
else -> PayPalMessageAlign.LEFT
textAlignment = when (checkedId) {
R.id.styleLeft -> PayPalMessageAlignment.LEFT
R.id.styleCenter -> PayPalMessageAlignment.CENTER
R.id.styleRight -> PayPalMessageAlignment.RIGHT
else -> PayPalMessageAlignment.LEFT
}
}

Expand Down Expand Up @@ -160,7 +161,7 @@ class XmlActivity : AppCompatActivity() {

payPalMessage.color = color
payPalMessage.logoType = logoType
payPalMessage.textAlign = textAlign
payPalMessage.textAlignment = textAlignment
}

// Restore default options and reset UI
Expand Down
10 changes: 5 additions & 5 deletions library/src/androidTest/java/com/paypal/messages/io/ApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,12 @@ class ApiTest {
{
"__shared__":{},
"amount":"null",
"channel":"NATIVE",
"channel":"UPSTREAM",
"component_events":[],
"style_color":"BLACK",
"style_logo_type":"PRIMARY",
"style_text_align":"LEFT",
"type":"MESSAGE"
"style_color":"black",
"style_logo_type":"primary",
"style_text_align":"left",
"type":"message"
}
],
"integration_type":"NATIVE_ANDROID",
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/paypal/messages/ModalFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ internal class ModalFragment(

private fun logEvent(event: AnalyticsEvent, dynamicKeys: MutableMap<String, Any>? = null) {
val component = AnalyticsComponent(
amount = this.amount.toString(),
amount = if (this.amount == null) "" else this.amount.toString(),
buyerCountryCode = this.buyerCountry,
type = ComponentType.MODAL.toString(),
instanceId = this.instanceId.toString(),
Expand Down
28 changes: 15 additions & 13 deletions library/src/main/java/com/paypal/messages/PayPalMessageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import com.paypal.messages.config.message.PayPalMessageData as MessageData
import com.paypal.messages.config.message.PayPalMessageEventsCallbacks as EventsCallbacks
import com.paypal.messages.config.message.PayPalMessageStyle as MessageStyle
import com.paypal.messages.config.message.PayPalMessageViewStateCallbacks as ViewStateCallbacks
import com.paypal.messages.config.message.style.PayPalMessageAlign as Align
import com.paypal.messages.config.message.style.PayPalMessageAlignment as Alignment
import com.paypal.messages.config.message.style.PayPalMessageColor as Color
import com.paypal.messages.config.message.style.PayPalMessageLogoType as LogoType

Expand Down Expand Up @@ -81,7 +81,7 @@ class PayPalMessageView @JvmOverloads constructor(
pageType = this.pageType,
environment = this.environment ?: PayPalEnvironment.SANDBOX,
),
style = MessageStyle(this.color, this.logoType, this.textAlign),
style = MessageStyle(this.color, this.logoType, this.textAlignment),
viewStateCallbacks = ViewStateCallbacks(this.onLoading, this.onSuccess, this.onError),
eventsCallbacks = EventsCallbacks(this.onClick, this.onApply),
)
Expand All @@ -97,7 +97,7 @@ class PayPalMessageView @JvmOverloads constructor(
pageType = config.data.pageType
color = config.style.color
logoType = config.style.logoType
textAlign = config.style.textAlign
textAlignment = config.style.textAlignment
onLoading = config.viewStateCallbacks?.onLoading ?: {}
onSuccess = config.viewStateCallbacks?.onSuccess ?: {}
onError = config.viewStateCallbacks?.onError ?: {}
Expand Down Expand Up @@ -207,7 +207,7 @@ class PayPalMessageView @JvmOverloads constructor(
debounceUpdateContent(Unit)
}
}
var textAlign: Align = config.style.textAlign
var textAlignment: Alignment = config.style.textAlignment
set(arg) {
if (field != arg) {
field = arg
Expand Down Expand Up @@ -343,14 +343,16 @@ class PayPalMessageView @JvmOverloads constructor(
}
// Apply disclaimer style
messageDisclaimer?.let { builder.setupDisclaimer(color, it) }
// TextView has textAlignment so this prevents clashing variables
val payPalMessageViewTextAlignment = textAlignment
// Apply everything to the text view
messageTextView.apply {
visibility = View.VISIBLE
setTextColor(ContextCompat.getColor(context, color.colorResId))
gravity = when (textAlign) {
Align.LEFT -> Gravity.START
Align.CENTER -> Gravity.CENTER_HORIZONTAL
Align.RIGHT -> Gravity.END
gravity = when (payPalMessageViewTextAlignment) {
Alignment.LEFT -> Gravity.START
Alignment.CENTER -> Gravity.CENTER_HORIZONTAL
Alignment.RIGHT -> Gravity.END
}
text = builder
}
Expand Down Expand Up @@ -426,10 +428,10 @@ class PayPalMessageView @JvmOverloads constructor(
}

if (typedArray.hasValue(R.styleable.PayPalMessageView_paypal_text_align)) {
textAlign = Align(
textAlignment = Alignment(
typedArray.getInt(
R.styleable.PayPalMessageView_paypal_text_align,
Align.LEFT.value,
Alignment.LEFT.value,
),
)
}
Expand Down Expand Up @@ -498,8 +500,8 @@ class PayPalMessageView @JvmOverloads constructor(
logEvent(
AnalyticsEvent(
eventType = EventType.MESSAGE_CLICKED,
pageViewLinkName = "banner_wrapper",
pageViewLinkSource = "message",
pageViewLinkName = if (messageDisclaimer != "") messageDisclaimer else "Learn more",
pageViewLinkSource = "learn_more",
),
)
showWebView(response)
Expand Down Expand Up @@ -650,7 +652,7 @@ class PayPalMessageView @JvmOverloads constructor(
buyerCountryCode = this.buyerCountry,
styleLogoType = this.logoType,
styleColor = this.color,
styleTextAlign = this.textAlign,
styleTextAlign = this.textAlignment,
messageType = this.messageDataResponse?.meta?.messageType,
fdata = this.messageDataResponse?.meta?.fdata,
debugId = this.messageDataResponse?.meta?.debugId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ package com.paypal.messages.analytics
import com.google.gson.annotations.SerializedName
import com.paypal.messages.config.PayPalMessageOfferType
import com.paypal.messages.config.PayPalMessagePageType
import com.paypal.messages.config.message.style.PayPalMessageAlign
import com.paypal.messages.config.message.PayPalMessageStyle
import com.paypal.messages.config.message.style.PayPalMessageAlignment
import com.paypal.messages.config.message.style.PayPalMessageColor
import com.paypal.messages.config.message.style.PayPalMessageLogoType

/**
* [AnalyticsComponent] holds data used for logging analytics information
*
* Note on styleTextAlign here vs [PayPalMessageStyle].textAlignment:
* To match with other integrations, the [PayPalMessageStyle] value will be textAlignment
* but for logging purposes, the key will be style_text_align
*/
data class AnalyticsComponent(
// Integration Details
@SerializedName("offer_type")
Expand All @@ -18,15 +26,15 @@ data class AnalyticsComponent(
@SerializedName("buyer_country_code")
val buyerCountryCode: String? = null,
@SerializedName("presentment_channel")
val channel: String? = "NATIVE",
val channel: String? = "UPSTREAM",

// Message Only
@SerializedName("style_logo_type")
val styleLogoType: PayPalMessageLogoType? = null,
@SerializedName("style_color")
val styleColor: PayPalMessageColor? = null,
@SerializedName("style_text_align")
val styleTextAlign: PayPalMessageAlign? = null,
val styleTextAlign: PayPalMessageAlignment? = null,
@SerializedName("message_type")
val messageType: String? = null,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.paypal.messages.analytics

import com.google.gson.annotations.SerializedName

enum class ComponentType {
@SerializedName("modal")
MODAL,

@SerializedName("message")
MESSAGE,
;

Expand Down
15 changes: 15 additions & 0 deletions library/src/main/java/com/paypal/messages/analytics/EventType.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package com.paypal.messages.analytics

import com.google.gson.annotations.SerializedName

enum class EventType {
@SerializedName("message_rendered")
MESSAGE_RENDERED,

@SerializedName("message_clicked")
MESSAGE_CLICKED,

@SerializedName("modal_rendered")
MODAL_RENDERED,

@SerializedName("modal_clicked")
MODAL_CLICKED,

@SerializedName("modal_viewed")
MODAL_VIEWED,

@SerializedName("modal_closed")
MODAL_CLOSED,

@SerializedName("modal_error")
MODAL_ERROR,
;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.paypal.messages.config.message

import com.paypal.messages.config.message.style.PayPalMessageAlign as Align
import com.paypal.messages.analytics.AnalyticsComponent
import com.paypal.messages.config.message.style.PayPalMessageAlignment as Align
import com.paypal.messages.config.message.style.PayPalMessageColor as Color
import com.paypal.messages.config.message.style.PayPalMessageLogoType as LogoType

/**
* [PayPalMessageStyle] holds data used to customize the style of a PayPalMessage component
*
* Note on textAlignment here vs [AnalyticsComponent].styleTextAlign:
* To match with other integrations, the value here will be textAlignment
* but for logging purposes, the [AnalyticsComponent] key will be style_text_align
*/
data class PayPalMessageStyle(
val color: Color = Color.BLACK,
val logoType: LogoType = LogoType.PRIMARY,
val textAlign: Align = Align.LEFT,
val textAlignment: Align = Align.LEFT,
) : Cloneable {
public override fun clone(): PayPalMessageStyle {
return super.clone() as PayPalMessageStyle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
package com.paypal.messages.config.message.style

import com.google.gson.annotations.SerializedName
import com.paypal.messages.utils.PayPalErrors

/**
* [PayPalMessageAlign] provides different variations of text alignments supported by the PayPalMessage component
* [PayPalMessageAlignment] provides different variations of text alignments supported by the PayPalMessage component
*
* @property value is the index identifier for returning a text alignment
*/
enum class PayPalMessageAlign(val value: Int) {
enum class PayPalMessageAlignment(val value: Int) {
@SerializedName("left")
LEFT(0),

@SerializedName("center")
CENTER(1),

@SerializedName("right")
RIGHT(2),
;

companion object {
/**
* Given an [attributeIndex] this will provide the correct [PayPalMessageAlign].
* Given an [attributeIndex] this will provide the correct [PayPalMessageAlignment].
*
* @throws [PayPalErrors.IllegalEnumArg] when an invalid index is provided.
*/
operator fun invoke(attributeIndex: Int): PayPalMessageAlign {
operator fun invoke(attributeIndex: Int): PayPalMessageAlignment {
return when (attributeIndex) {
LEFT.value -> LEFT
CENTER.value -> CENTER
RIGHT.value -> RIGHT
else -> throw PayPalErrors.IllegalEnumArg("LogoAlignment", 3)
else -> throw PayPalErrors.IllegalEnumArg("PayPalMessageAlignment", 3)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.paypal.messages.config.message.style

import com.google.gson.annotations.SerializedName
import com.paypal.messages.R
import com.paypal.messages.utils.PayPalErrors

Expand All @@ -12,9 +13,16 @@ enum class PayPalMessageColor(
val value: Int,
val colorResId: Int,
) {
@SerializedName("black")
BLACK(value = 0, colorResId = R.color.gray_700),

@SerializedName("white")
WHITE(value = 1, colorResId = R.color.white),

@SerializedName("monochrome")
MONOCHROME(value = 2, colorResId = R.color.black),

@SerializedName("grayscale")
GRAYSCALE(value = 3, colorResId = R.color.gray_700),
;

Expand Down

0 comments on commit ff2ac5a

Please sign in to comment.