Connecting PostgreSQL

Some companies use several platforms to collect and manage customer data. Thus, sometimes you need to add to messages certain content based on customer data tracked or stored at third-party platforms, such as an external CRM, mobile application, database, etc. For example, you want to add to an email personalized product recommendations based on offline sales or orders made via calls and need access to this data.

To access data from third-party sources and use it for content substitution, use a preprocessor or connect to external data sources, such as Google Sheets, BigQuery or PostgreSQL.

Connection to PostgreSQL would be the most optimal option if you need to

  • add extra fields for segmentation by contact fields;
  • configure seamless synchronization of content component;
  • get extra data on contacts in order to build precise segments based on specific conditions.

1. Set up a connector.

  1. Go to your personal profile → Settings → Connectors → and select Connect PostgreSQL.
Select Connect PostgreSQL in Connectors
  1. Fill in all fields:
  • Name. Required.
  • Host. Can be the domain name of the remote server or its IP address.
  • Port. Entered automatically by default.
  • Database. Name of the database on a remote server.
  • Username. User's database login (read only user role is enough). Required.
  • Password. Required.
Fill in all fields

You may click Validate connection to test whether authorization data is entered correctly.

📘

Note

When connecting to PostgreSQL, we recommend using a secure SSL connection for secure data transfer.

If you are not using an SSL connection, the system will warn you:

Connection is not SSL-secured

In case of a successful connection, the corresponding service notification will appear in the top right corner. If the connection failed, check the entered data and contact your system administrator if needed.

  1. In Set unique contact field, select a table column that contains a unique contact key and our system's unique contact key. They should match.

For example, you choose Email. After connection, the column of the PostgreSQL table with email addresses will be mapped to the corresponding field in the system.

Table with contact data

Aside from email addresses, uniqueness of contacts can be determined by a phone number, external ID or additional contact field.

📘

Important

The created connector can be used to connect to several data sources. All of them will have the same unique contact key so it’s recommended to use one key in all tables. To use a different key, then create another connector to the same database and set a different unique contact field.

  1. Click Save.

2. Configure a data source.

Your external database may contain several tables, and you need to select which one you want to connect to.

  1. Go to your personal profile → Settings → Data sources, click New data source and select External data source.
Select External data source
  1. Select the created connector.
Select the created connector
  1. Select dataset and table and enter a source name. Click Save.
Select dataset and table

4. Now, import contacts to our service and fill the external source with data.

Now you can build segments based on the imported contact fields.

Build a segment using the imported data

📘

Important

Connection to the external database doesn’t presuppose contact import. Segmentation is only available for contacts that exist both in your account and in the external database. Synchronize and update contacts before creating campaigns.

3. Add data to a message.

To insert data, you need to reference it using a Velocity parameter $!data.get(‘source_name’). As an example, we will be using the created source with the name promo_codes.

Bulk campaign to a segment

For example, you plan a campaign to a contact kozak@example.com.

Example of table

A set of data will be extracted from the table and inserted into an object:

{
  "data": {
    "promo_codes": [
      {
        "id": "2",
        "email": "kozak@example.com",
        "name": "Dina Kozak",
        "birthday": "2020-09-02T00:00:00Z",
        "promo_code": "AAAA-BBBB-DDDD"
      }
    ]
  }
}

Fields of the array promo_codes can be referenced in two ways:

  • With parameters (if you know the number of parameters in the array).
- $!data.get('promo_codes').get(0).get('name')

  $!data.get('promo_codes').get(0).get('promo_code')
  • With a loop (most common method).
- # foreach($pc in $!data.get('promo_codes'))

  $!pc.get('name')

  $!pc.get('promo_code')

  # end

Triggered campaign

First, you need to create a dynamic segment with certain conditions. For example, your segment will include only contacts who have a birthday today.

Segment conditions

When the workflow is triggered for contacts that match this condition, the system generates the event. The name of the event is formed from the static part (regularEventType) and the segment ID the campaign is sent to. It can be, for example, regularEventType-170531841.

The event contains contact data, contact ID in our platform (ContactId), email address (EmailAddress), and data from an external table.

The data from each table field is converted into a key and is placed in an array with a numerical name that corresponds to the ID of the data source.

Data source key

In turn, this array is serialized and placed as a string in the field jsonParam. The body of the event may look as follows:

{
  "params": [
    {
      "name": "ContactId",
      "value": "623927159"
    },
    {
      "name": "jsonParam",
      "value": "{\"1043\":[{\"id\":,\"email\":\"kozak@example.com\",\"name\":\"Dina Kozak\",\"birthday\":\"2020-09-02T00:00:00Z\",\"promo_code\":\"AAAA-BBBB-DDDD\"}]}"
    },
    {
      "name": "EmailAddress",
      "value": "kozak@example.com"
    }
  ]
}

To deserialize a string into a set of objects, in the workflow > block Email > JSON specify the data source from the event - ${jsonParam}.

JSON parameter

Having set up the workflow, configure triggers:

Configure trigger

The same approach as for a bulk campaign is used to extract data from the event, with little difference in the name of the array. The following object will be in the email:

{
  "data": {
    "1043": [
      {
        "id": "2",
        "email": "kozak@example.com",
        "name": "Dina Kozak",
        "birthday": "2020-09-02T00:00:00Z",
        "promo_code": "AAAA-BBBB-DDDD"
      }
    ]
  }
}

The fields of the array 1043 can be referenced in two ways:

  • With parameters (if you know the number of parameters in the array).
- $!data.get('1043').get(0).get('name')

  $!data.get('1043').get(0).get('promo_code')

  • With a loop (most common method).
- # foreach($pc in $!data.get('1043'))

  $!pc.get('name')

  $!pc.get('promo_code')

  # end

Connect PostgreSQL as an external data source to fulfill a wide range of marketing tasks, for example, substitute promo codes in triggered campaigns or build complex segments based on data collected on different platforms.

Step 4. Set up Data Export

To regularly update information about the audience, orders and campaign results in PostgreSQL, set up data export from the Yespo to tables. For example, you can export responses to an NPS survey, purchase history, date of the last click in a message, etc.

Available datasets for export:

  • broadcasts;
  • contactActivities;
  • contacts;
  • devices;
  • events
  • messages;
  • orderItems;
  • orders
  • revenue.

List of Data Export Parameters

Broadcasts

ParameterTypeDescription
createdDatetimestampDay and time of message creation (format: '2021-10-08 11:11:02')
groupIdintSegment IDs participating in the broadcast
idintBroadcast ID
mediaTypevarchar (50)Media type (SMS, Email, WebPush, Viber, MobilePush, AppInbox, Widget)
messageIdintMessage ID
namevarchar (1000)Broadcast name
startedDatetimestampDay and time of broadcast sending (format: '2021-10-08 11:11:02')
statusvarchar (50)Broadcast statuses:
• IDLE - completed;
• RUNNING - started;
• PAUSED - paused (if the mailing was not stopped manually by you, contact support for more details);
• SCHEDULED - planned;
• UNCONFIRMED - in queue for moderation;
• CONSIDERATION - under moderation;
• BLOCKED - blocked by moderator.
updatedDatetimestampDay and time of broadcast updating (format: '2021-10-08 11:11:02')

ContactActivities

ParameterTypeDescription
activity (activityStatus) *varchar (100)Activity status:
• DELIVERED – the message was delivered.
• UNDELIVERED – the message was undelivered (statusDescription contains the reason).
• RECEIVED – the message was opened.
• UNSUBSCRIBED – a contact unsubscribed from the broadcasting list.
• CLICKED – a contact clicked links in the message.
• SPAM – a contact reported spam.
• SUBSCRIPTION_CHANGED – a contact changed the subscription category.
• PUSH_SUBSCRIBED — a contact subscribed to push notifications.
broadcastIdintBroadcast ID
campaignTypevarchar (50)Campaign type:
• IM — triggered message,
• Group — bulk campaign.
clickEventLink varchar (1000)Contains the link clicked by a contact (when the status is CLICKED)
contactIdbigintContact ID in Yespo (Internal)
errorCode (statusDescription) *varchar (1000)Delivery error SMTP and description
eventKeyvarchar (100)Event key
eventTypeKeyvarchar (100)Event type key
externalCustomerIdvarchar (100)Contact ID in your system (External)
mediaTypevarchar (50)Media type (SMS, Email, WebPush, Viber, MobilePush, AppInbox, Widget, In-App, Telegram)
messageInstanceIdintService field
messageLanguageCodevarchar (50)Message language code
messageNamevarchar (100)Message name in Yespo account
messageIdintMessage ID
messageTagsvarchar (200)Message tags
messageURLvarchar (1000)Contains the link to the email web-version
senderNamevarchar (200)Viber sender name
started (activityDateTime) *timestampDay and time of message sending (format: '2021-10-08 11:11:02')
utmCampaign varchar (400)Campaign UTM
workflowIdstringWorkflow ID
workflowInstanceIdvarchar (200)The identifier of a particular workflow launch. Use it to group mailings within the launch of a workflow.
workflowBlockIdvarchar (200)Workflow block ID

*The field will be removed as overdated; use the field indicated in brackets

Contacts

ParameterTypeDescription
contactIdbigintContact ID in eSputnik (Internal)
contactSourcevarchar (50)Contact source:
• SITE_AUTOMATED - binding an email to a push subscriber (webPush collection script),
• I_MESSAGE - sending a single message,
• CAMPAIGN - workflow (block Create contact or Add to segment),
• IMPORT - file import or Add contacts method,
• MANUAL - manually created,
• SUBSCRIPTION - subscription form (Subscribe a contact API method),
• Add contact API method,
• ORDER - order on the site (Add orders API method).
createdDatetimestampContact creation date and time (format: '2021-10-08 11:11:02')
email varchar (50)Contact email
emailDomainvarchar (100)Email domain
emailStatusvarchar (50)Email status
externalCustomerId varchar (100)Contact ID in your system (External)
firstName varchar (50)Contact first name
languageCode varchar (20)Contact language code
lastClickedDate timestampDay and time of the last click (format: '2021-10-08 11:11:02')
lastNamevarchar (50)Contact last name
lastReceivedDate timestampDay and time of the last delivery (format: '2021-10-08 11:11:02')
lastSentDate timestampDay and time of the last sending (format: '2021-10-08 11:11:02')
lastViewedDatetimestampDay and time of the last opening (format: '2021-10-08 11:11:02')
smsvarchar (50)Phone number
totalClickedintTotal clicks amount
totalReceivedintTotal received messages amount
totalSentintTotal sent messages amount
totalViewedintTotal viewed messages amount

Devices

ParameterTypeDescription
appVersionvarchar (50)App version
applicationIdintApp ID
categoryvarchar (50)Category
contactIdbigintContact ID
deviceIdvarchar (250)Device ID
deviceModelvarchar (100)Device model
externalCustomerIdvarchar (100)External customer ID
languageCodevarchar (20)Language code
osTypevarchar (50)OS type
osVersionvarchar (50)OS version
pushTokenvarchar (max)Push token
timeZonevarchar (100)Time zone

Events

ParameterTypeDescription
contactIdintContact ID
eventIdintEvent ID
eventTypeKeystringEvent type
keyValuestringEvent key
occuredtimestampDate and time of occurrence (format: '2021-10-08 11:11:02')
paramsJsonjson50 first event parameters and values ​​(name/value); up to 300 characters of values ​​are transferred.

Messages

ParameterTypeDescription
annoyanceLevelintMessage annoyance level
languagevarchar (50)Language of the main version of the message
mediaTypevarchar (50)Media type (SMS, Email, WebPush, Viber, MobilePush, AppInbox, Widget, In-App)
messageIdintMessage ID
namevarchar (200)Message name
replyTovarchar (200)Reply-to address
sendervarchar (200)Sender
subjectvarchar (1000)Subject
tagsvarchar (200)Tags
translationsvarchar (200)Language versions of the message
updateDatetimestampDay and time of message updating (format: '2021-10-08 11:11:02')

OrderItems

ParameterTypeDescription
cost floatnumericProduct price
descriptionvarchar (300)Product description
externalProductIdvarchar (100)External product ID
imageUrlvarchar (200)Product image link
namevarchar (100)Product name
orderDatetimestampOrder creation date (format: '2021-10-08 11:11:02')
orderIdintOrder ID in Yespo (Internal)
quantityintNumber of products
urlvarchar (200)Product URL

Orders

ParameterTypeDescription
contactIdbigintContact ID in Yespo (Internal)
deliveryAddress varchar (100)Delivery address
deliveryMethodvarchar (50)Delivery method
discountnumericDiscount
emailvarchar (50)Email
externalOrderIdvarchar (100)External order ID
firstName varchar (50)Contact first name
lastNamevarchar (50)Contact last name
orderCreatedDatetimestampDate of order creation in Yespo (format: '2021-10-08 11:11:02')
orderDate timestampOrder creation date (format: '2021-10-08 11:11:02')
orderId intOrder ID in Yespo (Internal)
paymentMethodvarchar (50)Payment method
Phonevarchar (50)Phone number
Statusvarchar (50)Order status:
• INITIALIZED,
• IN PROGRESS,
• DELIVERED,
• CANCELED.
totalCostnumericTotal order cost

Revenue

ParameterTypeDescription
activity (activityStatus)*varchar (100)Activity status:
• DELIVERED – the message was delivered.
• UNDELIVERED – the message was undelivered (statusDescription contains the reason).
• RECEIVED – the message was opened.
• UNSUBSCRIBED – a contact unsubscribed from the broadcasting list.
• CLICKED – a contact clicked links in the message.
• SPAM – a contact reported spam.
• SUBSCRIPTION_CHANGED – a contact changed the subscription category.
• PUSH_SUBSCRIBED — a contact subscribed to push notifications.
campaignType varchar (50)Campaign type:
• IM — triggered message,
• Group — bulk campaign.
clickEventLink varchar (1000)Contains the link clicked by a contact (when the status is CLICKED)
contactIdbigintContact ID in Yespo (Internal)
currency varchar (3)Currency
externalCustomerId varchar (100)Contact ID in your system (External)
externalOrderId varchar (200)External order ID
mediaType varchar (50)Media type (Sms, Email, WebPush, Viber, MobilePush, AppInbox, Widget, In-App)
messageInstanceIdintService field
messageName varchar (100)Message name in Yespo account
messageTags varchar (200)Message tags
messageUrl varchar (1000)Contains the link to the email web-version
orderDate timestampOrder creation date (format: '2021-10-08 11:11:02')
senderName varchar (200)Viber sender name
started (activityDateTime)*timestampDay and time of message sending (format: '2021-10-08 11:11:02')
totalCostnumericTotal order cost
utmCampaign varchar (400)Campaign UTM

*The field will be removed as overdated; use the field indicated in brackets

📘

Note

Existing entries in the Revenue table (for orders placed after 03.08.2024) can be updated along with the upload of new orders. Updating may be necessary, for example, if the total cost of the order has changed.

This information will be transferred to PostgreSQL and will update the data in the tables.

📘

Important

You don’t need to pre-create PostgreSQL tables. They will be generated automatically at the first export, and since that all the incoming data will be updated. All the table names will correspond to the data sets (contact activities, contacts, order items, orders, revenue)

Setting up PostgreSQL connector for data export

1. Go to Settings → Data export and click New data export. Select one of the created PostgreSQL connectors.

Select connector

2. Select upload interval and check the boxes for the data sets you want to upload. The data set type by default is public, you can select another type if you have specified it in your PostgreSQL settings.  
Click Save.

Edit data

The connection will be displayed in the Data export section, here you can edit its settings.