Serverside javascript helpers
This article describes the server-side destination APIs.
decodeURI
decodeURIDecodes any encoded characters in the provided URI. Returns a string that represents the decoded URI. Returns undefined when provided with invalid input.
Syntax
decodeUri(encoded_uri);Example
const decodeUri = require('decodeUri');
const decodedUrl = decodeUri(data.encodedUrl);
if (decodedUrl) {
// ...
}decodeUriComponent
decodeUriComponentDecodes any encoded characters in the provided URI component. Returns a string that represents the decoded URI component. Returns undefined when given invalid input.
Syntax
decodeUriComponent(encoded_uri_component);Example
encoded_uri_component
string
A URI component that has been encoded by encodeUriComponent() or by other means.
encodeUri
encodeUriReturns an encoded Uniform Resource Identifier (URI) by escaping special characters. Returns a string that represents the provided string encoded as a URI.
Syntax
Example
uri
string
A complete URI.
encodeUriComponent
encodeUriComponentReturns an encoded Uniform Resource Identifier (URI) by escaping special characters. Returns a string that represents the provided string encoded as a URI.
Syntax
Example
str
string
A component of a URI.
fromBase64
fromBase64Decodes a base64-encoded string. Returns undefined if the input is invalid.
Syntax
Example
base64EncodedString
string
Base64 encoded string.
generateRandom
generateRandomReturns a random number (integer) within the given range.
Syntax
Example
min
number
Minimum potential value of the returned integer (inclusive).
max
number
Maximum potential value of the returned integer (inclusive).
getAllEventData
getAllEventDataReturns a copy of the event data.
Syntax
Usage Example
Data example:
If you send this web event:
Then the getAllEventData() function will return this object:
getCookieValues
getCookieValuesReturns an array containing the values of all cookies with the given name.
Syntax
Example
Parameters
name
string
Name of the cookie.
noDecode
boolean
If true, the cookie values will not be decoded before being returned. Defaults to false.
getEventData
getEventDataReturns a copy of the value at the given path in the event data. Returns undefined if there is no event data or if there is no value at the given path.
Syntax
Example
Parameters
keyPath
any
The path of the key, where path components are separated by dots. The path components can be keys in an object or indices in an array. If keyPath is not a string, it is coerced into a string.
getRemoteAddress
getRemoteAddressReturns a string representation of the IP address where the request originated, e.g. 62.123.65.780 for IPv4 or 2001:0db8:85a3:0:0:8a2e:0370:1234 for IPv6
Syntax
getTimestamp
getTimestampDeprecated. Prefer getTimestampMillis.
Returns a number that represents the current time in milliseconds since Unix epoch, as returned by Date.now().
Syntax
getTimestampMillis
getTimestampMillisReturns a number that represents the current time in milliseconds since Unix epoch, as returned by Date.now().
Syntax
getType
getTypeReturns a string describing the given value's type.
string
'string'
number
'number'
boolean
'boolean'
null
'null'
undefined
'undefined'
Array
'array'
Object
'object'
Function
'function'
Syntax
Example
Parameters
value
any
Input value.
logToConsole
logToConsoleLogs its argument(s) to the console.
These logs are visible within Destination Builder's console.
Example
Syntax
Parameters
The function takes one or more arguments, each of which is converted to a string, if necessary, and logged to the console.
makeInteger
makeIntegerConverts the given value to a number (integer).
Syntax
Parameters
value
any type
The value to convert.
makeNumber
makeNumberConverts the given value to a number.
Syntax
Parameters
value
any type
The value to convert.
makeString
makeStringReturns the given value as a string.
Syntax
Parameters
value
any type
The value to convert.
parseUrl
parseUrlReturns an object that contains all of a given URL's component parts, similar to the URL object.
This API will return undefined for any malformed URL. For properly formatted URLs, fields not present in the URL string will have a value of an empty string, or in the case of searchParams, an empty object.
The returned object will have the following fields:
Syntax
Example
Parameters
url
string
The full url that will be parsed.
sha256
sha256Calculates the SHA-256 digest of the input and invokes a callback with the digest encoded in base64, unless the options object specifies a different output encoding.
This API signature and behavior matches the sha256 API for web containers; however, Custom Templates in server containers should use the sha256Sync API for simpler code.
Syntax
Example
Parameters
input
string
The string to hash.
onSuccess
function
Called with the resulting digest, encoded in base64, unless the options object specifies a different output encoding.
options
object
Optional options object to specify the output encoding. If specified, the object should contain the key outputEncoding with value as one of base64 or hex.
sha256Sync
sha256SyncCalculates and returns the SHA-256 digest of the input, encoded in base64, unless the options object specifies a different output encoding.
Syntax
Example
Parameters
input
string
The string to hash.
options
object
Optional options object to specify the output encoding. If specified, the object should contain the key outputEncoding with value as one of base64 or hex.
toBase64
toBase64Encodes a string as base64.
Syntax
Example
Parameters
input
string
String to encode.
JSON
JSONReturns an object that provides JSON functions.
The parse() function parses a JSON string to construct the value or object described by the string. If the value cannot be parsed (malformed JSON), the function will return undefined. If the input value is not a string, the input will be coerced to a string.
The stringify() function converts the input into a JSON string. If the value cannot be parsed (ex. the object has a cycle), the method will return undefined.
Syntax
Example
Math
MathAn object providing Math functions.
Syntax
Parameters
Math function parameters are converted to numbers.
sendHttpGet
sendHttpGetMakes an HTTP GET request to the specified URL, and invokes a callback with the response once the request completes or times out.
Syntax
Example
Parameters
url
string
The request URL.
callback
function
An optional callback to invoke upon request completion, error, or timeout.
It is invoked with the response status code, the response headers, and the response body (or undefined if there was no response body).
If the request failed (e.g. invalid URL, no route to host, SSL negotiation failure, etc.), the callback will be invoked with a response status code of zero, no headers, and an undefined body.
If the 'timeout' option was set and the request timed out, the callback will be invoked with a response status code of -1, no headers, and an undefined body.
options
object
Optional request options. The supported options are headers, timeout. Advanced options can be added in extraOptions
Options
headers: Additional request headers represented as an object.
timeout: The timeout, in milliseconds, before the request is aborted.
extraOptions: Advanced options (ex: {strictSSL:true})
sendHttpRequest
sendHttpRequestMakes an HTTP request to the specified URL, and invokes a callback with the response once the request completes or times out.
Syntax
Example
Parameters
url
string
The request URL.
callback
function
An optional callback to invoke upon request completion, error, or timeout. It is invoked with the response status code, the response headers, and the response body (or undefined if there was no response body). If the request failed (e.g. invalid URL, no route to host, SSL negotiation failure, etc.), the callback will be invoked with a response status code of zero, no headers, and an undefined body. If the 'timeout' option was set and the request timed out, the callback will be invoked with a response status code of -1, no headers, and an undefined body.
options
object
Optional request options. The supported options are: headers, method, and timeout. Unknown option keys are ignored. Advanced options can be added in extraOptions.
body
string
Optional request body.
Options
headers: Additional request headers.
method: The request method, defaults to 'GET'.
timeout: The timeout, in milliseconds, before the request is aborted.
extraOptions: Advanced options (ex: {strictSSL:true})
md5Sync
md5SyncCalculates and returns the md5 digest of the input.
Syntax
Example
templateDataStorage
templateDataStorageThe templateDataStorage helper allows temporary storage and retrieval of data, such as API tokens, during script execution. It is particularly useful for caching reusable data to reduce redundant API calls. Data stored in templateDataStorage persists on the server running the template. Since templates execute on multiple servers, and each server may have multiple instances, stored data is not guaranteed to be accessible for all subsequent template execution.
Syntax
Example: Managing API Tokens
setItemCopy(key, value)
Stores a value under the specified key. Overwrites the value if the key already exists.
getItemCopy(key)
Retrieves the value associated with the specified key. Returns undefined if the key does not exist.
removeItemCopy(key)
Deletes the value associated with the specified key.
key
string
The unique identifier for the data to be stored/retrieved.
value
any
The data to be stored (for setItemCopy).
Last updated
Was this helpful?