Abandoned Cart Trigger (API Trigger)

Last Updated: 17/4/2024     Tags: abandon, cart, shopping, trigger, api
  • Switch Version
  • V5
  • V4

Abandoned cart triggers are useful for re-targeting customers in real time via your website/mobile app.

An abandoned cart is where someone visits your website, looks around, populates their shopping cart and gets part way through the checkout process and leaves. This shopping cart is called an abandoned cart.

This trigger will populate an email with products that have been 'abandoned' in the persons cart. You are required to set your own rules as to when the cart is considered 'abandoned' and when to trigger the email. These are two separate processes that can either be run in sequence or delayed.

st=>start: Begin e=>end: End op1=>operation: Lookup Customer Email op2=>operation: Update Subscriber Profile With JSON Data op3=>operation: Trigger Email st(right)->op1(right)->op2(right)->op3(right)->e

Requirements

  • Taguchi API (or optional wrapper);
  • A website with a shopping cart/commerce software;
  • Access to the Taguchi UI through acceptable user permissions OR an access token (contact Taguchi Support for token access);

Instructions

Firstly, we need to pair the customer on your website, with a subscriber profile within Taguchi. Usually the email address is the best primary key in this sense, so we will use it in this example. You can use other fields such as a phone number or external ID if you wish.

Step 1: Lookup A Subscriber

Using the subscriber resource, perform a GET (using the _method querystring) lookup for a subscriber matching the customers email address:

https://<client-name>.taguchimail.com/admin/api/<organization-id>/subscriber/?auth=<username>|<password>&_accept=application/json&_content_type=application/json&_method=GET&query=email-eq-<email>

Replace the following variables within the URL above with appropriate data:

Variable Description
<client-name> Your Taguchi client name (e.g. edm2). If you are unsure of your client name, contact Taguchi Support.
<organization-id> A numerical organization ID. If you are unsure of your organization ID, contact Taguchi Support.
<username>
<password>
Replace these with a Taguchi user account details (with restricted permissions for API use), alternatively you can use a token to authenticate.
<email> The customer email address to look up.

If an email matches with a subscriber record, the subscribers profile fields will be returned:

[
   {
      import_id: null,
      cluster_id: null,
      firstname: 'Example',
      extra: null,
      lastname: 'Person',
      address2: null,
      address3: null,
      campaign_id: null,
      lists: [
         {
            import_id: null,
            option: null,
            timestamp: '2010-04-11T00:00:00',
            campaign_id: null,
            unsubscribed: null,
            list_id: 5
         },
         {
            import_id: null,
            option: null,
            timestamp: '2010-04-11T00:00:00',
            campaign_id: null,
            unsubscribed: '2010-04-12T00:00:00',
            list_id: 6
         }
      ],
      organization_id: 1,
      phone: '0400312312',
      suburb: null,
      postcode: '3000',
      address: null,
      data: null,
      id: 2,
      custom_fields: [
         {
            field: 'examplekey',
            data: 'val'
         }
      ],
      bounced: null,
      title: 'Mr',
      dob: '1970-04-01',
      user_class_id: 1,
      state: null,
      unsubscribed: null,
      gender: 'M',
      country: 'AU',
      ref: null,
      email: 'person@example.org',
      notifications: null
   }
]

With the response data, you have now identified that the customer is a valid subscriber within Taguchi. You can now use the id field to conduct the next step.

Step 2: Update Subscriber With Abandoned Cart Data

Next, we will need to update the subscribers profile with the abandoned cart details (so we can automatically populate the email with the products within the abandoned cart). We do this by adding/updating a custom field with some JSON data.

Using the subscriber resource, use PUT (using the _method querystring) to update the subscribers profile with escaped JSON data (products abandoned in the customers cart):

https://<client-name>.taguchimail.com/admin/api/<organization-id>/subscriber/<subscriber-id>?auth=<username>|<password>&_accept=application/json&_content_type=application/json&_method=PUT

Replace the following variables within the URL above with appropriate data:

Variable Description
<client-name> Your Taguchi client name (e.g. edm2). If you are unsure of your client name, contact Taguchi Support.
<organization-id> A numerical organization ID. If you are unsure of your organization ID, contact Taguchi Support.
<username>
<password>
Replace these with a Taguchi user account details (with restricted permissions for API use), alternatively you can use a token to authenticate.
<subscriber-id> The subscriber ID obtained in Step 1: Lookup A Subscriber step above.

The request data should contain the JSON data within a custom field:

[
    {
        'custom_fields': [
          {
              field: 'abandoned_cart_data',
              data: '{"cart_url":"http://www.samplestore.com.au/cart","products":[{"name":"Test Item #1","image":"https://www.samplestore.com.au/images/products/ti1.jpg","url":"https://www.samplestore.com.au/test-item-1","price":"22.00","ean":"600"},{"name":"Test Item #2","image":"https://www.samplestore.com.au/images/products/ti2.jpg","url":"https://www.samplestore.com.au/test-item-2","price":"5.00","ean":"500"}]}'
          }
        ]
    }
]

This code will enable us to create or update a custom field named 'abandoned_cart_data'.

If the subscriber record has been updated successfully, the subscribers profile fields will be returned with the newly updated values:

[
   {
      import_id: null,
      cluster_id: null,
      firstname: 'Example',
      extra: null,
      lastname: 'Person',
      address2: null,
      address3: null,
      campaign_id: null,
      lists: [
         {
            import_id: null,
            option: null,
            timestamp: '2010-04-11T00:00:00',
            campaign_id: null,
            unsubscribed: null,
            list_id: 5
         },
         {
            import_id: null,
            option: null,
            timestamp: '2010-04-11T00:00:00',
            campaign_id: null,
            unsubscribed: '2010-04-12T00:00:00',
            list_id: 6
         }
      ],
      organization_id: 1,
      phone: '0400312312',
      suburb: null,
      postcode: '3000',
      address: null,
      data: null,
      id: 2,
      'custom_fields': [
          {
              field: 'examplekey',
              data: 'val'
          },
          {
              field: 'abandoned_cart_data',
              data: '{"cart_url":"http://www.samplestore.com.au/cart","products":[{"name":"Test Item #1","image":"https://www.samplestore.com.au/images/products/ti1.jpg","url":"https://www.samplestore.com.au/test-item-1","price":"22.00","ean":"600"},{"name":"Test Item #2","image":"https://www.samplestore.com.au/images/products/ti2.jpg","url":"https://www.samplestore.com.au/test-item-2","price":"5.00","ean":"500"}]}'
          }
      ]
      bounced: null,
      title: 'Mr',
      dob: '1970-04-01',
      user_class_id: 1,
      state: null,
      unsubscribed: null,
      gender: 'M',
      country: 'AU',
      ref: null,
      email: 'person@example.org',
      notifications: null
   }
]

Step 3: Set-Up Trigger Activity In Taguchi UI

In order to trigger an email, we need to set-up and deploy an email activity within the Taguchi UI. This activity will need to pull the custom field in order to display the abandoned cart data.

To do this, simply create a new email activity.

Next, we need to start populating some dynamic data (using our custom field containing JSON). There are two ways to do this.

The simplest way would be to use an existing block (e.g. article block) and display the cart items within that block using the following code:

{% 
    var data = JSON.parse(recipient.custom.abandoned_cart_data); 
%}

This script just takes the custom field using recipient.custom.abandoned_cart_data and parses it from a string to valid, workable JSON. You can then reference the JSON data included within the custom field using the data variable, like so:

<h2>Items still in your cart</h2>
{% Object.keys(data['products']).forEach(function (key) { var obj = data['products'][key]; %}
<p>
    <b>Item Name:</b> <a href="{%= obj['url'] %}">{%= obj['name'] %}</a>
</p>
<p>
    <img alt="{%= obj['name'] %}" src="{%= obj['image'] %}">
</p>
<p>
    <b>Price:</b> ${%= obj['price'] %}
</p>
{% }); %}
<hr />
<p><a href="{%= data['cart_url'] %}">Visit Your Cart </a></p>

Alternatively, the template you use can be modified to include an extra block which automatically pulls in the cart items from the subscribers custom field and displays them. The same code above can be used in the template.

If you do not manage your own templates within Taguchi, contact Taguchi Support to discuss further options in regards to template development work.

Once you are happy with the activity look and feel along with how the data will be displayed, you will need to approve and deploy. Ensure there is no target expression loaded into the activity before deploying, this activity will be triggered rather than broadcasted.

Step 4: Trigger The Activity

The activity will need to be sent to the customer/subscriber once the abandoned cart data has been populated within their subscriber profile. To do this, we can utilise the activity resource with the method TRIGGER (using the _method querystring):

https://<client-name>.taguchimail.com/admin/api/<organization-id>/activity/<activity-id>?auth=<username>|<password>&_accept=application/json&_content_type=application/json&_method=TRIGGER

Replace the following variables within the URL above with appropriate data:

Variable Description
<client-name> Your Taguchi client name (e.g. edm2). If you are unsure of your client name, contact Taguchi Support.
<organization-id> A numerical organization ID. If you are unsure of your organization ID, contact Taguchi Support.
<username>
<password>
Replace these with a Taguchi user account details (with restricted permissions for API use), alternatively you can use a token to authenticate.
<activity-id> The activity ID that will be triggered to the subscriber.

The request data should contain the following fields to trigger the activity to the subscriber:

[
    {
        "date": "2017-10-11T08:00:00", // Date and time to trigger the email (YYYY-MM-DDTHH:MM:SS)
        "conditions": [ 1234 ], // Subscriber ID(s) that will receive the triggered email
        "test": 0, // Change this to '1' if you wish to test the trigger
        "request_content": "" // Any additional content can be fed through to the request handler
    }
]

Alternatively you can set a target expression for more advanced handling:

[
    {
        "date": "2017-10-11T08:00:00", // Date and time to trigger the email
        "conditions": {"expression": "email = 'email@test.com' and not sent activity X last 7 days"}, // Target expression matching the send target
        "test": 1, // Change this flag to '1' if you wish to enable testing mode
        "request_content": "" // Any additional content can be fed through to the request handler
    }
]

The target expression included above will send to a subscriber matching the email 'email@test.com' who has not been sent activity X (replace with your activity ID) in the last 7 days. If the subscriber doen't match the expression, they will not receive the triggered email. This would be useful for frequently returning customers, preventing an abandoned cart email from being sent more than once within a week.

Upon successful completion of the request, you will see a 200 OK response. This is confirmation that the email has been successfully triggered. You can confirm this using the UI statistics for that activity.


If you encounter any issues configuring this trigger for your organization, please contact Taguchi Support.