Skip to content

Commit

Permalink
feat: update modal tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinpaypal committed Sep 11, 2024
1 parent cf10b46 commit c67416d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import androidx.core.content.res.use
import com.paypal.messages.analytics.AnalyticsComponent
import com.paypal.messages.analytics.AnalyticsEvent
import com.paypal.messages.analytics.AnalyticsLogger
import com.paypal.messages.analytics.ComponentType
import com.paypal.messages.analytics.EventType
import com.paypal.messages.config.PayPalEnvironment
import com.paypal.messages.config.PayPalMessageOfferType
import com.paypal.messages.config.PayPalMessagePageType
import com.paypal.messages.config.modal.ModalCloseButton
import com.paypal.messages.config.modal.ModalConfig
import com.paypal.messages.config.modal.ModalEvents
import com.paypal.messages.io.Api
import com.paypal.messages.io.ApiResult
import com.paypal.messages.io.OnActionCompleted
import com.paypal.messages.utils.LogCat
import com.paypal.messages.utils.PayPalErrors
import kotlinx.coroutines.CoroutineScope
Expand All @@ -45,8 +45,8 @@ class PayPalMessagesModalFragment @JvmOverloads constructor(
attributeSet: AttributeSet? = null,
defStyleAttr: Int = 0,
config: MessagesModalConfig = MessagesModalConfig(clientID = ""),
) : FrameLayout(context, attributeSet, defStyleAttr), OnActionCompleted {
private val TAG = "PayPalMessagesModalView"
) : FrameLayout(context, attributeSet, defStyleAttr) {
private val TAG = "PayPalMessagesModalFragment"
private var instanceId = UUID.randomUUID()

fun getConfig(): MessagesModalConfig {
Expand Down Expand Up @@ -239,13 +239,21 @@ class PayPalMessagesModalFragment @JvmOverloads constructor(
private var modal: ModalFragment? = null

init {
context.obtainStyledAttributes(attributeSet, R.styleable.PayPalMessagesModalView).use { typedArray ->
context.obtainStyledAttributes(attributeSet, R.styleable.PayPalMessagesModalFragment).use { typedArray ->
updateFromAttributes(typedArray)
}
if (config.clientID === "") LogCat.error(TAG, "ClientID is an empty string")
}

fun show() {
// Since show is called as part of a merchant's onClickListener, we log that event here
logEvent(
AnalyticsEvent(
eventType = EventType.STANDALONE_MODAL_CLICKED,
pageViewLinkName = "Standalone modal, text set by merchant",
pageViewLinkSource = "merchant_text",
),
)
val modal = modal ?: run {
val modal = ModalFragment(clientID)
// Build modal config
Expand Down Expand Up @@ -296,69 +304,54 @@ class PayPalMessagesModalFragment @JvmOverloads constructor(
* This function will update the local config related values based on what is provided from the [PayPalMessageView] xml custom view.
*/
private fun updateFromAttributes(typedArray: TypedArray) {
if (typedArray.hasValue(R.styleable.PayPalMessagesModalView_paypal_client_id)) {
clientID = typedArray.getString(R.styleable.PayPalMessagesModalView_paypal_client_id).toString()
if (typedArray.hasValue(R.styleable.PayPalMessagesModalFragment_paypal_client_id)) {
clientID = typedArray.getString(R.styleable.PayPalMessagesModalFragment_paypal_client_id).toString()
}

if (typedArray.hasValue(R.styleable.PayPalMessagesModalView_paypal_amount)) {
if (typedArray.hasValue(R.styleable.PayPalMessagesModalFragment_paypal_amount)) {
amount = try {
typedArray.getFloatOrThrow(R.styleable.PayPalMessagesModalView_paypal_amount).toDouble()
typedArray.getFloatOrThrow(R.styleable.PayPalMessagesModalFragment_paypal_amount).toDouble()
}
catch (ex: Exception) {
LogCat.error(TAG, "Error parsing amount attribute")
null
}
}

if (typedArray.hasValue(R.styleable.PayPalMessagesModalView_paypal_offer_type)) {
if (typedArray.hasValue(R.styleable.PayPalMessagesModalFragment_paypal_offer_type)) {
offerType = try {
PayPalMessageOfferType(typedArray.getIntOrThrow(R.styleable.PayPalMessagesModalView_paypal_offer_type))
PayPalMessageOfferType(typedArray.getIntOrThrow(R.styleable.PayPalMessagesModalFragment_paypal_offer_type))
}
catch (ex: Exception) {
LogCat.error(TAG, "Error parsing offer_type attribute")
null
}
}

if (typedArray.hasValue(R.styleable.PayPalMessageView_paypal_page_type)) {
if (typedArray.hasValue(R.styleable.PayPalMessagesModalFragment_paypal_page_type)) {
pageType = try {
PayPalMessagePageType(typedArray.getIntOrThrow(R.styleable.PayPalMessageView_paypal_page_type))
PayPalMessagePageType(typedArray.getIntOrThrow(R.styleable.PayPalMessagesModalFragment_paypal_page_type))
}
catch (ex: Exception) {
LogCat.error(TAG, "Error parsing page_type attribute")
null
}
}

if (typedArray.hasValue(R.styleable.PayPalMessageView_paypal_buyer_country)) {
buyerCountry = typedArray.getString(R.styleable.PayPalMessageView_paypal_buyer_country)
}
}

override fun onActionCompleted(result: ApiResult) {
when (result) {
is ApiResult.Success<*> -> {
LogCat.debug(TAG, "onActionCompleted Success")
}

is ApiResult.Failure<*> -> {
LogCat.debug(TAG, "onActionCompleted Failure")
// If we encountered a failure, we expect an exception to be returned.
result.error?.let { this.onError(it) }
}
if (typedArray.hasValue(R.styleable.PayPalMessagesModalFragment_paypal_buyer_country)) {
buyerCountry = typedArray.getString(R.styleable.PayPalMessagesModalFragment_paypal_buyer_country)
}
}

@Suppress("unused")
private fun logEvent(event: AnalyticsEvent) {
val component = AnalyticsComponent(
offerType = this.offerType,
amount = this.amount.toString(),
pageType = this.pageType,
buyerCountryCode = this.buyerCountry,
instanceId = this.instanceId.toString(),
originatingInstanceId = Api.originatingInstanceId.toString(),
// TODO: what type to use
// type = ComponentType.MESSAGE.toString(),
type = ComponentType.STANDALONE_MODAL.toString(),
componentEvents = mutableListOf(event),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ enum class ComponentType {

@SerializedName("message")
MESSAGE,

@SerializedName("standalone_modal")
STANDALONE_MODAL,
;

override fun toString(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ enum class EventType {

@SerializedName("modal_error")
MODAL_ERROR,

@SerializedName("standalone_modal_clicked")
STANDALONE_MODAL_CLICKED,
;

override fun toString(): String {
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

<attr name="paypal_buyer_country" />
</declare-styleable>
<declare-styleable name="PayPalMessagesModalView">
<declare-styleable name="PayPalMessagesModalFragment">
<attr name="paypal_client_id" />

<attr name="paypal_offer_type" />
Expand Down

0 comments on commit c67416d

Please sign in to comment.