Tracking purchases

  • Hello. I am trying to integrate a marketing automation tool to a Runway site and looking for the best place to put the POST request to their API to record a purchase. I'm a frontend person understanding PHP but not too familiar!


    My first thought was to put it right in the payment success page but I don't think this is the best option since this page is a regular page accessible from anywhere and could potentially trigger more requests than purchases. I thought I could somehow block access to this page unless user is coming from checkout but I don't know how to do it :(


    Next thought was to put it in the checkout page where there is the following piece of code (I'm using Braintree by the way)

    Code
    1.  if (perch_member_logged_in() && perch_post('payment_method_nonce')) {
    2. // your 'success' return URL
    3. $return_url = '/paymentsuccess';
    4. perch_shop_checkout('braintree', [
    5. 'return_url' => $return_url,
    6. 'token' => perch_post('payment_method_nonce')
    7. ]);
    8. }

    My understanding is that this perch_shop_checkout is what actually performs the checkout so I could maybe put the POST right before it. Do you think this is how it should be done? And if so, what is the PHP equivalent to javascript Promises where I could do the something like postTrackingData().then(perch_shop_checkout)


    Thank you very much for any advice!

  • drewm

    Approved the thread.
  • Hello,


    perch_shop_checkout() handles failed payments too.


    I think the ideal approach would be to write a Perch app that listens to the event Perch Shop fires when an order was successfully paid for. You can learn more about event hooks in the documentation.


    Another option would be to use perch_shop_order_successful() to check for a successful order (in your successful payment page for example).


    PHP
    1. if (perch_shop_order_successful()) {
    2. // you have a successful order
    3. }


    Though I think this may evaluate to true for the duration of the user's session. So if the user refreshes the successful payment page, you may be posting duplicate purchases to your marketing/analytics tool.

  • Thanks hus_hmd! Yes, the case with perch_shop_order_successful() is exactly as you said. I will have a look at the custom solution you suggested. I also found out that a way to avoid recording duplicate purchase events is passing a transaction ID to the request. Then the receiving service should have a way to ignore duplicates. Google analytics does it but not sure about the marketing tool I plan to use. Some more investigation for me. Thank you again.