require 'net/http'
require 'uri'
require 'cgi'
require 'json'
url = URI.parse('{{ @apiUrl }}') # sensor URL
headers = {
'Content-Type' => 'application/x-www-form-urlencoded',
'Api-Key' => '{{ @apiKey }}' # Tracking ID
}
# Replace each key value with actual info
data = {
########### Required fields ###########
# Unique value that allows identification of a user. Ex: alice54 (string)
'userName': '',
# User email (string)
'emailAddress': '',
# User IP address (string)
'ipAddress': '',
# URL path of visited page (string)
'url': '',
# User-agent of user request (string)
'userAgent': '',
# Event UTC timestamp ('Y-m-d H:i:s.v' string)
'eventTime': '',
########### Optional fields ###########
# User first name (string)
'firstName': '',
# User last name (string)
'lastName': '',
# User full name (string)
'fullName': '',
# Title of visited page (string)
'pageTitle': '',
# User phone number (string)
'phoneNumber': '',
# Referer of visited page (string)
'httpReferer': '',
# Status code for page visit (string)
'httpCode': '',
# User browser language (string)
'browserLanguage': '',
# Type of user action from event types list (string)
# For ex. 'page_view', 'field_edit', etc.
'eventType': '',
# Type of HTTP request from list (string)
'httpMethod': '',
# User created UTC timestamp ('Y-m-d H:i:s' string)
'userCreated': '',
# Payload, must have `page_search` or `account_email_change` event type (array)
'payload': [],
# Field history, must have `field_edit` event type (array)
'fieldHistory': [
{
'field_name': '', # Name of the field
'field_id': '', # ID of the field
'new_value': '', # Field new value
'old_value': '', # Field old value
'parent_id': '' # ID of the field parent
}
]
}
if data.key?('payload') && !data['payload'].nil?
data['payload'] = JSON.generate(data['payload'])
end
if data.key?('fieldHistory') && !data['fieldHistory'].nil?
data['fieldHistory'] = JSON.generate(data['fieldHistory'])
end
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true if url.scheme == 'https'
request = Net::HTTP::Post.new(url, headers)
request.body = URI.encode_www_form(data)
response = http.request(request)