A JavaScript template for integrating Braze campaigns with Cloudinary that enables marketers to dynamically select campaign assets based on Braze liquid tags and Cloudinary metadata.
This template allows you to automatically deliver contextually relevant assets to users based on audience segment, locale, or other attributes.
Key Features:
- Use tags in Cloudinary for all assets belonging to a campaign(s)
- Use SMD in Cloudinary to indicate asset attributes (e.g., locale, audience)
- Use targeting variables with liquid tags from Braze to drive selection logic
- Use a Cloudinary URL with variables representing the targeting attributes in the connected content editor in Braze
- Use fn_select and JavaScript filter to resolve the best fit image (JavaScript filter is uploaded into Cloudinary)
The URL structure to be used in Braze campaigns:
https://<Base_URL>/<resource_type>/list/$locale_!en!/$audience_!Internal!/fn_select:js:v<js_epoch_seconds>:template.js/v<url_epoch_seconds>/<campaign_tag>.json
- Base_URL: The first part of your Cloudinary media delivery URLs
- resource_type:
videofor videos,imagefor images - list: Fixed string required by the syntax
- $locale_!en!: Sets the
localescript parameter. Value 'en' comes from Braze liquid tags - $audience_!Internal!: Sets the
audiencescript parameter - fn_select:js:v<epoch_seconds>:template.js: Calls the custom script
- If the script is in a subfolder, use colon (:) as separator (e.g.,
scripts:template.js) - Use current
<js_epoch_seconds>timestamp for faster propagation when modifying the script
- If the script is in a subfolder, use colon (:) as separator (e.g.,
- <campaign_tag>: Cloudinary tag assigned to all campaign assets
- .json: Fixed extension required by the syntax
This template.js filters and selects assets from a Cloudinary campaign based on metadata parameters like locale and audience. It's designed to be deployed as a Cloudinary custom function.
For detailed information about Cloudinary custom functions, see the official documentation.
The script accepts two main parameters:
- locale: Language/locale identifier (e.g., 'en', 'fr', 'de')
- audience: Audience segment identifier (e.g., 'Internal', 'Premium')
- Parses the URL transformation string to extract parameters
- Iterates through available campaign resources
- Checks each resource's metadata for matching
localeandaudiencevalues - Returns the first resource that matches both criteria
- Falls back to a default image if no match is found
- getTxParts(tx): Parses the transformation string into key-value pairs
- getParam(txParts, arg): Extracts a specific parameter value from parsed transformation parts
- getMetadataDict(metadata): Converts metadata array into a dictionary for easier lookup
- haveMetadata(md, arg, value): Checks if metadata contains a specific key-value pair
- fallbackImage(publicId, tx): Returns a fallback image when no matches are found
The main function:
- Retrieves the document content (campaign resources)
- Extracts transformation parameters from the URL
- Gets the
localeandaudienceparameters - Searches for a matching resource based on metadata
- Returns the matched resource or fallback image
To adapt this template for your use case:
- Skip the "Helper functions" section - these are boilerplate utilities
- Edit the "Main" section with your specific:
- Parameter names
- Metadata field names
- Matching logic
- Fallback behavior
If no resource matches the specified locale and audience parameters, the script returns:
- Default image:
samples/two-ladies - Preserves the transformation chain from the original URL
The template script expects Cloudinary metadata in the following format. Please follow the documentation for adding metadata.
[
{
"public_id": "asset_name",
"metadata": [
{
"external_id": "locale",
"value": "en"
},
{
"external_id": "audience",
"value": "Internal"
}
]
}
]- Upload the script to your Cloudinary account
- Note the script's public_id and location
- Construct URLs using the format above
- Use the URLs in your Braze campaign templates
- Update the
<js_epoch_seconds>timestamp when you modify the script
To target English language content for Internal audience:
https://res.cloudinary.com/demo/image/list/$locale_!en!/$audience_!Internal!/fn_select:js:v1234567890:template.js/v1234567890/summer_campaign.json