Sitecore
Interfacing xConnect Using Python
February 27, 2018 Josip Ivković

Intro
 
In our previous post we demonstrated how to use the official xConnect .NET library to interface with Experience Database. But, what if your device does not support .NET 4.6? Worry not, Coria has got you covered. We developed a library named xConnectPy.

 

xConnectPy
 
xConnectPy is designed to be as similar to the official library as possible. Function names, overloads and parameters are nearly identical. Since we really wanted to mirror overloading, the library currently only works on Python 3.5 to support an "overloading" library. Although very functional, bear in mind that xConnectPy is in its very early stages and some of the official library features may not yet be implemented.

 

Setup
 
First, make sure you are running Python 3.5.*. Then you should be able to install the library:

pip3 install xconnectpy

 
Next, we need a configuration file. Create config.xml inside your project folder and adjust the sample:

<settings>
<add key="serviceRoot" value="https://myservice.com/odata/" />
<add key="certificates" value="certificates/xconnect_certificates.pem" />
<add key="logFile" value="logs/log.txt" />
<add key="SSLTunnel" value="true" />
<add key="encodePayloads" value="true" />
<add key="enableLogging" value="true" />
</settings>

We also need an SSL certificate. Use OpenSSL to convert your certificate to .pem extension and add it to your project folder.

 

 

Creating Contact
 
Start by importing the library:

from xconnectpy import *
 

And instantiating it the client:

client = XConnectClient('config/config.xml')
 

Let's add Corry Corrian to xDB as we did in previous examples. First we need ContactIdentifier and Contact objects:

contactIdentifier = ContactIdentifier(Source="Coria", Identifier="Test", IdentifierType=ContactIdentifierType.Known)
contact = Contact(Identifiers=[contactIdentifier])
 

Add the contact to client and submit it:

client.add_contact(contact)
client.submit()

 

 

Adding Facet

 
Let's fetch the contact we created using ContactIdentifier:

contactReference = IdentifiedContactReference("Coria", "Test")
contact = client.get(contactReference, ExpandOptions())
 

Create personal information facet:

personalInfoFacet = PersonalInformation(FirstName="Corry", LastName="Corian")
 

Bind it to contact and submit:

client.set_facet(contact, PersonalInformation.DefaultFacetKey, personalInfoFacet)
client.submit()

 

 
Adding Interaction

 
We can also create a store purchase interaction.
 
Fetch the contact:

contactReference = IdentifiedContactReference("Coria", "Test")
contact = client.get(contactReference, ExpandOptions())
 

Set the Store Channel GUID and create Interaction:

enterStoreChannelId = "3fc61bb8-0d9f-48c7-9bbd-d739dcbbe032"
userAgent = "xConnectPy"
interaction = Interaction(contact, InteractionInitiator.Contact, enterStoreChannelId, userAgent)
 

Set the Purchase Outcome GUID and create Outcome:

productPurchaseOutcomeId = "9016e456-95cb-42e9-ad58-997d6d77ae83"
outcome = Outcome(productPurchaseOutcomeId, datetime.utcnow().isoformat(), "USD", 42.95)
 

Add outcome to Interaction Events list:

interaction.Events.append(outcome)
 

Add interaction to client and submit:

client.add_interaction(interaction)
client.submit()
 

 

Conclusion

 

Ability to use xConnect API in Python opens various possibilities. We hope that xConnectPy will help you make powerful Sitecore integration layers.

If you use xConnectPy to drive your next ML or IOT project let me know! Contact me to chat or collaborate at jivkovic@coria.com 
We hope that you find this blog post series interesting and useful. If you have any questions feel free to contact us!

Josip Ivković
Machine Learning, Data Science, & Development