Basic integration
In this tutorial, let's share information with Talon.One using the main entities: customer sessions and customer profiles.
This tutorial is easier to follow if you have read the following pages:
Understanding the context
Before getting started with the implementation, let's set up some business context.
Let's imagine that we are setting up an integration between Talon.One and our clothing store. We already have an Application in the Campaign Manager, and it contains a running campaign with the following rule:
- Any valid coupon code gives a
20%
discount on the session/cart.
Choosing the SDK and the API
Communication with our API endpoints should happen only from a backend environment to avoid exposing sensitive authentication credentials to end users.
According to the integration checklist, let's select an SDK and the API.
- SDK: In our case, let's imagine our project uses Python, so let's use the Python SDK.
- API: We're working with customer sessions and profiles, so we must use the Integration API.
Creating a Talon.One API key
Create your API key:
-
In your Application, click Settings > Integration API Keys.
-
Click Create API Key.
-
In the Create API Key drawer, select Production as the key type.
-
In Key name, type a name to identify the key.
-
In Key expiration date, select a date.
-
In Third-party integration, select No.
-
Click Create API Key.
-
Click to copy the key for use.
noteYou cannot view or copy the API key after closing the drawer. If you lose it, create another API key.
This key is only for our Talon.One Application. You can now send requests to the Integration API from our integration layer:
- Python
- curl
import talon_one
from talon_one.rest import ApiException
# Create configuration with your host destination and authorization using api_key_v1
configuration = talon_one.Configuration(
host = "https://mycompany.europe-west1.talon.one", # edit the host
api_key_prefix = {
"Authorization": "ApiKey-v1"
},
api_key = {
"Authorization": "f725d5a03e091ce9ab8bf51ee6c9ca37066e42f386b08bcf77ceddfe5364560"
}
)
# Instantiate the IntegrationAPI without credentials
integration_api = talon_one.IntegrationApi(talon_one.ApiClient(configuration))
To authenticate using curl, we use the HEADER field for each request.
curl --request PUT 'https://mycompany.europe-west1.talon.one/...' \
--header 'Authorization: ApiKey-v1 f725d5a03e091ce9ab8bf51ee6c9ca37066e42f386b08bcf77ceddfe5364560' \
...
Now that we are authenticated, we can proceed with the rest of the implementation that looks as follows:
Creating a customer session
This section takes care of steps 1 and 2 of the integration landscape:
In Talon.One, a customer cart is represented by a customer session. This resource is essential, it's used to keep Talon.One updated about what the customer is doing, and to respond accordingly.
A Talon.One customer session has nothing to do with a server session. The API client decides when to close the session.
To create a session and update it, we use the Integration API's Update customer session endpoint.
A session has a state, in our case we'll create it as open
,
which is the default.
To understand session states, see Managing session states.
Back to our context, let's imagine that a shopper started creating a shopping cart. At this given time, the cart contains a couple of items and a coupon code. Let's communicate this to Talon.One to check if our coupon rule should be applied.
Sending session information
This session should contain:
- Two cart items which exist in our shop's catalog.
- A coupon (
xmas20
).
To do so, we apply the following steps:
- Create a
NewCustomerSessionV2
object to be fed to theupdateCustomerSession
endpoint. In this case, we do not provide a customer profile ID, which creates an anonymous session. For more information about customer profiles, see the section below. - Add the two cart items and the coupon to the
NewCustomerSessionV2
object. - Send the request.
- Store the response for later processing.
The following code applies the above steps:
- Python
- curl
# ...
# Instantiate the IntegrationAPI without credentials
integration_api = talon_one.IntegrationApi(talon_one.ApiClient(configuration))
# Prepare a NewCustomerSessionV2 object with the cart items and the coupon code
customer_session = talon_one.NewCustomerSessionV2()
customer_session.cart_items = [
talon_one.CartItem("Red Spring Blouse", "rdbs-1111", 1, 49, "Shirts"), # name, sku, quantity, price, category
talon_one.CartItem("Denim Trousers", "dtr-2222", 1, 74, "Trousers"),
]
customer_session.coupon_codes = ["xmas20"]
# Instantiating a new IntegrationRequest object
integration_request = talon_one.IntegrationRequest(customer_session)
CURL -X PUT https://mycompany.europe-west1.talon.one/v2/customer_sessions/my_session_id -d
'{
"customerSession": {
"state": "open",
"cartItems": [
{
"name": "Red Spring Blouse",
"sku": "rdbs-1111",
"quantity": 1,
"price": 49,
"category": "Shirts",
},
{
"name": "Denim Trousers",
"sku": "dtr-2222",
"quantity": 1,
"price": 74,
"category": "Trousers",
}
],
"couponCodes":["xmas20"]
}
}'
Running this sample executes the request and we now have the response in the
integration_request
variable.
See also:
Performance tips
Do more with one call: The
Update customer session
and Update customer profile
endpoints offer a responseContent
property that you can use to save API calls.
For example, you can use this property to retrieve the customer
profile information without having to use another endpoint.
Reduce response time: When you query the Update customer session
or
Update customer profile
endpoint but
do not want the request to modify your Application, set the runRuleEngine
and dry
parameters to true
to send a dry request and
get faster response times.
You will still see the returned effects but there will be no change to your Application.
Use a single event for a single customer action: When implementing customer actions, consider that each event represents a single occurrence of a customer action related to a customer session or customer profile. To avoid receiving too many 409 responses, ensure that the customer action does not trigger multiple events in parallel.
Managing parallel requests
For data integrity purposes, Talon.One allows three parallel Integration API requests per customer profile or session integration ID. Parallel requests are queued and processed sequentially. The queue is limited to two requests. Extra requests will receive a 409 Too many requests are updating this profile/session at the same time response.
We recommend only sending sequential requests.
This applies to the following endpoints:
For example, consider the following parallel requests:
- One
Update customer profile
request on a givencustomerId
. This request is being processed. - Two
Update customer session
requests involving the samecustomerId
as the request above. These two requests are queued. - Extra requests, for example, a request to
Track event
, related to the samecustomerId
will receive a 409 response.
Processing effects
This section takes care of step 5 of the integration diagram:
In the previous section, we sent an update to Talon.One. For every session or profile update that we send, Talon.One runs all applicable rules and returns the effects in the API response.
The way we process these effects depends on the SDK. We can register effect handlers or loop through the effects array in the response. With the Python SDK, we loop over the effects.
In our case, we have only one rule that applies a 20% discount if the coupon code is valid so we can expect the following effects:
All effects are available in API effects.
To process the effects, let's loop through the effects
array from the response. To
keep the code sample small, let's only look for the setDiscount
and rejectCoupon
effects.
# ... auth
# API request
integration_request = talon_one.IntegrationRequest(customer_session)
try:
api_response = integration_api.update_customer_session_v2(
"my_session_id", integration_request
)
# Parse the effects
for effect in api_response.effects:
if effect.effect_type == "setDiscount":
# Initiating right props instance according to the effect type
setDiscountProps = integration_api.api_client.deserialize_model(
effect.props, talon_one.SetDiscountEffectProps
)
# Access the specific effect properties
name = (setDiscountProps.name,)
value = setDiscountProps.value
print(
"setDiscount triggered:\n"
f" Effect name: {name}\n"
f" Discount value: {value}"
)
# Apply the discount to the cart...
elif effect.effect_type == "rejectCoupon":
rejectCouponEffectProps = integration_api.api_client.deserialize_model(
effect.props, talon_one.RejectCouponEffectProps
)
print(f"Coupon was rejected: {rejectCouponEffectProps.rejection_reason}")
# Display banner to the user...
except ApiException as e:
print("Exception when calling IntegrationApi->update_customer_session_v2: %s\n" % e)
The next step is about processing the effect in our store, so this depends on your setup and industry. Typically, you would recalculate the cart value to display it to your customer.
We have covered the main steps to manage the whole interaction with Talon.One. The following section are use cases that are useful to further customize the data you share with Talon.One.
Adding extra information to a customer session
You might have to pass data that is not supported by the SDK. For example, you can readily create cart items, but what if we want to pass shipping-related information and run different promotion rules based on the address? We can do this with custom attributes.
In our case, let's imagine that we want to pass the city of the delivery address. Let's define a custom attribute in the Campaign Manager:
- Click Account > Tools > Attributes > Create Attribute.
- In Entity, select Customer session. This means our customer session will store the attributes.
- In Type, select String.
- In API name, type
shippingCity
. This is the name to reference in the API call. - Fill in all the other fields as needed to enable the attribute in your Application.
We can now set this custom attribute in API calls:
- Python
- curl
integration_request = talon_one.IntegrationRequest(
{
"attributes": {
"shippingCity": "Seattle",
}
},
)
api_response = integration_api.update_customer_session_v2("my_session_id", integration_request)
CURL -X PUT https://mycompany.europe-west1.talon.one/v2/customer_sessions/my_session_id -d
'{
"customerSession": {
"attributes": {
"shippingCity": "Seattle",
}
}
}'
Managing customer profiles
Aside from customer sessions, we can manage customer profiles. They store the attributes of your customers. For example, a profile stores the identifier, address, device, location or shopping history data of a customer.
A customer profile is represented inside Talon.One by the CustomerProfile
entity.
A customer profile is the owner of a customer session and may have many sessions over
its lifetime. Profiles are created and updated using the
updateCustomerProfile endpoint,
which works like the updateCustomerSession
endpoint explained above.
The identifier of a customer profile is stored in the integration id
property.
Creating a customer profile
Let's create a customer profile with id customer-1234
and store a name and some
shipping information in it:
- We can use any of the built-in attributes to define our customer. You can see the available built-in attributes on the Attributes page in the Campaign Manager.
- We can create custom attributes on the customer entity to store extra information.
Let's use the Name
, ShippingCity
and ShippingRegion
built-in attributes
and a custom attribute named MyAttribute
in this example. We assume
that MyAttribute
has been created and assigned to the customer entity.
See how to create attributes.
- Python
- curl
# Define a profile id.
customer_profile_id = 'customer-1234'
# Create the payload of the request.
# We assume that these attributes have been created and
# associated to the Customer Profile entity in the Campaign Manager.
body = talon_one.NewCustomerProfile({
'Name': "Anthony Ray",
'ShippingCity': "Seattle",
'ShippingRegion': "Washington",
'MyAttribute': "my value"
})
# Execute the request
api_response = integration_api.update_customer_profile_v2(customer_profile_id, body)
CURL -X PUT https://mycompany.europe-west1.talon.one/v2/customer_profiles/customer-1234 -d
'{
"attributes": {
"Name": "Anthony Ray",
"ShippingCity": "Seattle",
"ShippingRegion": "Washington",
"MyAttribute": "my value"
}
}'
We can now use this customer profile with our sessions.
Connecting a customer profile to a customer session
In the examples used for the customer session tutorial above, we used anonymous sessions. These sessions are not connected to any customer profile, which is not recommended.
To connect a session to a profile, let's set the profile_id
property of the session to
the value of the customer profile id:
- Python
- curl
# Create the customer profile.
customer_profile_id = "customer-1234"
body = talon_one.NewCustomerProfile(
{"Name": "Anthony Ray", "ShippingCity": "Seattle", "ShippingRegion": "Washington"}
)
api_response = integration_api.update_customer_profile_v2(customer_profile_id, body)
# Create customer session payload
customer_session = talon_one.NewCustomerSessionV2()
# Add the profile id
# This is how we bind the session to the profile
customer_session.profile_id = customer_profile_id
# Create the request
integration_request = talon_one.IntegrationRequest(customer_session)
# Execute the request
api_response = integration_api.update_customer_session_v2(
"my_session_id", integration_request
)
CURL -X PUT https://mycompany.europe-west1.talon.one/v2/customer_sessions/my_session_id -d
'{
"customerSession": {
"profileId": "customer-1234",
// cartItems ...
// other session data if needed
}
}'
Closing the session
We've seen how to create a session, bind it to a customer profile, and add custom attributes to it. At the end of the order workflow, in our case the payment step, we have to close the session. Doing so increments campaign budget limit counters, redeems potential coupons, etc.
To close the session, we update the state
property to closed
:
- Python
- curl
# Reuse our previous customer_session object
# and update the state
customer_session.state = "closed"
# Create the request
integration_request = talon_one.IntegrationRequest(customer_session)
# Execute the request
api_response = integration_api.update_customer_session_v2(
"my_session_id", integration_request
)
CURL -X PUT https://mycompany.europe-west1.talon.one/v2/customer_sessions/my_session_id -d
'{
"customerSession": {
"state": "closed"
}
}'
For more information about how to manage sessions, see Managing session states.
Troubleshooting issues
You can display the Integration API logs in the Campaign Manager to diagnose issues.