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

[MBL 873] Implement TriggerThirdPartyEvent Mutation #1835

Merged
merged 9 commits into from Jul 25, 2023
Merged

Conversation

scottkicks
Copy link
Contributor

@scottkicks scottkicks commented Jul 17, 2023

📲 What

Replacing TriggerCapiEventInput calls with TriggerThirdPartyEvent

🤔 Why

We're going to handle Facebook CAPI and Google Analytics calls with a single mutation.
For more details see the engineering plan

🛠 How

Remove TriggerCapiEvent related files and references.

Replace current .triggerCapiEventInput calls with .triggerThirdPartyEvent
We’re currently triggering this mutation in the following ViewModels:

  • PledgePaymentMethodsViewModel
  • ProjectPageViewModel
  • RewardsCollectionViewModel
  • ThanksViewModel

Existing parameter differences below:

  • externalIddeviceId
  • eventNameeventName
  • projectIdprojectId
  • appDataappData (we can keep hardcoding these values to true, just like we do now because externalId is required)
  • we do not need to pass anything into firebaseScreen, firebasePreviousScreen
  • userIdAppEnvironment.current.currentUser?.id

New Parameters (These are only passed in on the ThanksViewModel):
We can use this existing code…

      if let checkoutDataValues = configData.checkoutData {
        ...
      }
  • pledgeAmount → checkoutDataValues.revenueInUsd + checkoutDataValues.bonusAmountInUsd + checkoutDataValues.addOnsMinimumUsd
  • shipping → checkoutDataValues.shippingAmountUsd
  • transactionId → checkoutDataValues.checkoutId
  • clientMutationId → ““

✅ Acceptance criteria

  • Use Charles Proxy, or something similar, to verify that these calls are being sent at the expected times with success 200 responses

@scottkicks scottkicks self-assigned this Jul 17, 2023
@scottkicks scottkicks added this to the release-5.9.0 milestone Jul 17, 2023
@codecov
Copy link

codecov bot commented Jul 24, 2023

Codecov Report

Merging #1835 (49e6889) into main (61ffb8b) will increase coverage by 0.05%.
The diff coverage is 79.34%.

@@            Coverage Diff             @@
##             main    #1835      +/-   ##
==========================================
+ Coverage   84.49%   84.54%   +0.05%     
==========================================
  Files        1277     1273       -4     
  Lines      115232   115190      -42     
  Branches    30674    30656      -18     
==========================================
+ Hits        97365    97389      +24     
+ Misses      16799    16732      -67     
- Partials     1068     1069       +1     
Files Changed Coverage Δ
KsApi/MockService.swift 13.21% <ø> (-0.01%) ⬇️
KsApi/Service.swift 9.39% <ø> (+0.14%) ⬆️
KsApi/ServiceType.swift 76.06% <ø> (ø)
...iewModels/PledgePaymentMethodsViewModelTests.swift 96.79% <ø> (-0.01%) ⬇️
...ary/ViewModels/PledgePaymentMethodsViewModel.swift 88.37% <5.55%> (-1.87%) ⬇️
Library/NSBundleType.swift 38.29% <50.00%> (+0.52%) ⬆️
Library/ViewModels/ThanksViewModel.swift 97.51% <96.87%> (+10.98%) ⬆️
Library/NSBundleType+Tests.swift 100.00% <100.00%> (ø)
Library/TestHelpers/MockBundle.swift 100.00% <100.00%> (ø)
Library/ViewModels/ProjectPageViewModel.swift 97.52% <100.00%> (+5.95%) ⬆️
... and 1 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

according to CAPI docs all values are required and must be in the given indexed order.
Comment on lines 387 to 404
extinfo: [
"i2",
"",
"",
"",
"\(ProcessInfo.processInfo.operatingSystemVersion)",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the CAPI documentation all values in extInfo are required and must be in the given indexed order.
I paired with Isa on this and this format is the only way we can get a successful 200 response for this mutation.

IMO we should only have to pass the used values (i2, OS version) to the backend and they should be responsible for formatting the data correctly.

Since the docs require this I chose to implement it this way on our end.

Screenshot 2023-07-17 at 2 21 13 PM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here's my recommendation for how this function should written. The big take-away is to process the value sent into the function outside the function, not inline. Keeps things cleaner.

        guard let externalId = AppEnvironment.current.appTrackingTransparency.advertisingIdentifier
        else { return }

        var userId = ""
        
        if let userValue = AppEnvironment.current.currentUser {
          userId = "\(userValue.id)"
        }
        
        let projectId = "\(project.id)"
        
        var extInfo = Array(repeating: "", count: 16)
        extInfo[0] = "i2"
        extInfo[4] = AppEnvironment.current.mainBundle.platformVersion
        
        _ = AppEnvironment
          .current
          .apiService
          .triggerThirdPartyEventInput(
            input: .init(
              deviceId: externalId,
              eventName: ThirdPartyEventInputName.AddNewPaymentMethod.rawValue,
              projectId: projectId,
              pledgeAmount: nil,
              shipping: nil,
              transactionId: nil,
              userId: userId,
              appData: .init(
                advertiserTrackingEnabled: true,
                applicationTrackingEnabled: true,
                extinfo: extInfo
              ),
              clientMutationId: ""
            )
          )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add this to NSBundleType

  public var platformVersion: String {
    return self.infoDictionary?["DTPlatformVersion"] as? String ?? "Unknown"
  }

And write a test for it in NSBundleType + Tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did I get the above?

Start with an example from in app ie. line 740 AppDelegateViewModel that leads toNSBundleType for things like appVersionString (similar property).

Build out that “OS version” here. The benefit of keeping it here as opposed to using ProcessInfo in multiple places, is its’ easy to change later and also test.

Why not use process info?

A lot properties in NSBundle+Type use “self.infoDictionary”. It turns out it is a provided property that contains the OS version in the key - "DTPlatformVersion" - at least on simulator this worked.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest following this pattern below in ProjectPageViewModel, RewardsColletionViewModel and ThanksViewModel.

@scottkicks scottkicks removed the WIP label Jul 24, 2023
@scottkicks scottkicks requested a review from msadoon July 24, 2023 17:32
@scottkicks scottkicks marked this pull request as ready for review July 24, 2023 17:32
@scottkicks scottkicks requested a review from ifosli July 24, 2023 17:41
Copy link
Contributor

@msadoon msadoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this is mostly fine.

I refactored a bit of the parameter passing to keep the code clean.

Normally it would be good to test these network requests wherever they are used in the view model. Oddly we didn't test the CAPI ones. Will enforce this in the eng. plan in the future, so no need to do it here.

The changes are all laid out so make those quickly and I'll approve tomorrow morning. Good work!

Library/Tracking/FacebookCAPIEventName.swift Show resolved Hide resolved
Comment on lines 387 to 404
extinfo: [
"i2",
"",
"",
"",
"\(ProcessInfo.processInfo.operatingSystemVersion)",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here's my recommendation for how this function should written. The big take-away is to process the value sent into the function outside the function, not inline. Keeps things cleaner.

        guard let externalId = AppEnvironment.current.appTrackingTransparency.advertisingIdentifier
        else { return }

        var userId = ""
        
        if let userValue = AppEnvironment.current.currentUser {
          userId = "\(userValue.id)"
        }
        
        let projectId = "\(project.id)"
        
        var extInfo = Array(repeating: "", count: 16)
        extInfo[0] = "i2"
        extInfo[4] = AppEnvironment.current.mainBundle.platformVersion
        
        _ = AppEnvironment
          .current
          .apiService
          .triggerThirdPartyEventInput(
            input: .init(
              deviceId: externalId,
              eventName: ThirdPartyEventInputName.AddNewPaymentMethod.rawValue,
              projectId: projectId,
              pledgeAmount: nil,
              shipping: nil,
              transactionId: nil,
              userId: userId,
              appData: .init(
                advertiserTrackingEnabled: true,
                applicationTrackingEnabled: true,
                extinfo: extInfo
              ),
              clientMutationId: ""
            )
          )

Comment on lines 387 to 404
extinfo: [
"i2",
"",
"",
"",
"\(ProcessInfo.processInfo.operatingSystemVersion)",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add this to NSBundleType

  public var platformVersion: String {
    return self.infoDictionary?["DTPlatformVersion"] as? String ?? "Unknown"
  }

And write a test for it in NSBundleType + Tests

Comment on lines 387 to 404
extinfo: [
"i2",
"",
"",
"",
"\(ProcessInfo.processInfo.operatingSystemVersion)",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did I get the above?

Start with an example from in app ie. line 740 AppDelegateViewModel that leads toNSBundleType for things like appVersionString (similar property).

Build out that “OS version” here. The benefit of keeping it here as opposed to using ProcessInfo in multiple places, is its’ easy to change later and also test.

Why not use process info?

A lot properties in NSBundle+Type use “self.infoDictionary”. It turns out it is a provided property that contains the OS version in the key - "DTPlatformVersion" - at least on simulator this worked.

Comment on lines 387 to 404
extinfo: [
"i2",
"",
"",
"",
"\(ProcessInfo.processInfo.operatingSystemVersion)",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest following this pattern below in ProjectPageViewModel, RewardsColletionViewModel and ThanksViewModel.

Library/ViewModels/ThanksViewModel.swift Outdated Show resolved Hide resolved
@scottkicks
Copy link
Contributor Author

scottkicks commented Jul 25, 2023

@msadoon I've addressed your comments. thanks for the feedback. I tried your your suggestion for calculating the pledgeAmount and didn't find that it added more accuracy. I think your solution makes more sense anyway though so I've made that change as well.

Copy link
Contributor

@msadoon msadoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were mistakes here I found, please correct before we can merge.

Library/ViewModels/ProjectPageViewModel.swift Show resolved Hide resolved
Library/ViewModels/ProjectPageViewModel.swift Outdated Show resolved Hide resolved
Library/ViewModels/ProjectPageViewModel.swift Outdated Show resolved Hide resolved
@scottkicks scottkicks requested a review from msadoon July 25, 2023 16:26
@scottkicks scottkicks merged commit 91d3abe into main Jul 25, 2023
7 checks passed
@scottkicks scottkicks deleted the scott/mbl-873 branch July 25, 2023 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants