Friday, November 12, 2021

Python — Extract Data from Salesforce Marketing Cloud (ExactTarget)

As we know that data is truly the new diamond in the rough and accelerated digital technologies have initiated new business models and revenue streams.  Salesforce Marketing Cloud Email Studio helps us to get the best ROI from email marketing which  lets us to create targeted one-to-one email communications at scale, with end-to-end campaign development and sophisticated content delivery capabilities. 

Python — Extract Data from Salesforce Marketing Cloud (ExactTarget)
Salesforce Marketing Cloud (SFMC) is Salesforce’s SaaS platform which is highly capable to is a provider of digital marketing automation and analytics software and services. SFMC is a marketing platform that has capabilities capabilities across the marketing domain to support many facets of marketing including multi-channel campaign execution, dynamic customer journeys, pre- and post-campaign analytics including audience building and segmentation, social media engagement and advertising, and a data management platform. This includes the following: 
1. Customer engagement in real-time
2. Email and marketing automation
3. Social media engagement, listening and advertising
4. Mobile (SMS) messaging and push notifications
5. Customer marketing analytics
Salesforce Marketing Cloud Email Studio has a range of functions, from standard email services to sophisticated cross-channel capabilities. Salesforce aims to deliver enterprise email marketing functionality to every marketer, making it easy for businesses to scale up their capabilities, whether that means adding extra customer channels to the strategy or sending out millions of emails in one go.
Salesforce Marketing Cloud Email Studio allows marketers to streamline content production using Salesforce’s content management system, Content Builder. Using the system’s tagging and sharing function, it’s possible to create targeted one-to-one emails at scale. Simple drag and drop tools make it easy to create eye catching content to help drive customer engagement, without any coding knowledge needed.

The Marketing Cloud platform SDKs provide a cross-functional framework around the SOAP and REST APIs which allows developers to integrate APIs using native language code libraries by using SOAP and REST APIs to provide agnostic protocol interfaces and automated token management.
In this article, we will elaborate the steps to set up the Python framework for importing your data to your Salesforce Marketing Cloud platform using their REST API endpoints.
First of all, you must have an account on Salesforce Marketing Cloud platform and you will need to set up access credentials for your API calls by following the below steps —
  1. login at Salesforce Marketing Cloud platform as given below -

Creating the credentials on Salesforce Marketing Cloud

2. After successful login, go to setup (under your account in top right corner of the home screen) as given below —

3. After click on the Setup, go to Platform Tools and click on Installed Packages in side Apps as given below —

4. Now, you have to click on the New button as given below -

5. Now, just provide the requested information and click on save button as given below -

6. After creating the package, click on add component and choose API integration as given below —

7. In this section, you have to choose Integration Type as server and fill your account subdomain to enter the URI https://SUBDOMAIN.auth.marketingcloudapis.com/, after click on the save button you will get the following screen -

Now, you have all the credentials such as Client Id, Client Secret, Base, REST base and SOAP base URI as showing in the above screen.

Creating your Python Code — Python is one of the fastest growing programming languages.
1. Whether it’s data manipulation with Pandas,
2. Creating visualizations with Seaborn, or
3. Deep learning with TensorFlow,
Python seems to have a tool for everything.

Installation — The Fuel SDK for Python provides easy access to Salesforce Marketing Cloud’s Fuel API Family services, including a collection of REST APIs and a SOAP API. The Fuel SDK for python can be easily installed from the Python Package Index using the pip command. Pip is a tool for installing and managing Python packages.

pip install Salesforce-FuelSDK

These APIs provide access to Salesforce Marketing Cloud functionality via common collection types such as array/hash.

# Add a require statement to reference the Fuel SDK's functionality: import FuelSDK 
import ET_Client

To enable OAuth2 authentication, set useOAuth2Authentication: True in the config.python file or pass it in the params argument to the ET_Client constructor.

#define local variables 
clientid= '<Your Client Id>'
clientsecret='<Your Client Secret>'
subdomain = '<Your Domain Name>'
auth_base_url = f'https://{subdomain}.auth.marketingcloudapis.com/' rest_url = f'https://{subdomain}.rest.marketingcloudapis.com/'
soap_url=f'https://{subdomain}.soap.marketingcloudapis.com/'
#Passing config as a parameter to ET_Client constructor:
myClient = FuelSDK.ET_Client(True, False,
{'clientid': clientid,
'clientsecret':clientsecret,
'useOAuth2Authentication': 'True',
'authenticationurl': auth_base_url,
'applicationType': 'server',
'baseapiurl': rest_url,
'soapendpoint': soap_url})
# Next, create an instance of the ET_Client class:
myClient = FuelSDK.ET_Client()

# Create an instance of the object type we want to work with:
list = FuelSDK.ET_List()

# Associate the ET_Client- object using the auth_stub property:
list.auth_stub = myClient

# Utilize one of the ET_List methods:
response = list.get()

# Print out the results for viewing
print 'Post Status: ' + str(response.status)
print 'Code: ' + str(response.code)
print 'Message: ' + str(response.message)
print 'Result Count: ' + str(len(response.results))
print 'Results: ' + str(response.results)

Read Data into Pandas Dataframe — Pandas is a Python package that provides fast, flexible, and expressive data structures designed to make working with structured (tabular, multidimensional, potentially heterogeneous) and time series data both easy and intuitive. It aims to be the fundamental high-level building block for doing practical, real world data analysis in Python.

#import pandas library package
import pandas as pd
#define dataframe to read response into dataframe
df=pd.DataFrame(response.results)
#load data into csv data file
df.to_csv('salesforce_marketing_cloud_sample.csv', index=False)

The ET_Client class takes care of many of the required steps when accessing Salesforce Marketing Cloud’s API, including retrieving appropriate access tokens, handling token state for managing refresh, and determining the appropriate endpoints for API requests.

To get more info on FuelSDK-Python, please click here

To learn more, please follow us -
To Learn more, please visit our YouTube channel at —
To Learn more, please visit our Instagram account at -
To Learn more, please visit our twitter account at -

Popular Posts