Embedding conversion
How to integrate conversion campaigns with your application or site
Step 1 — Embed the snippet
To get started with conversion campaigns you'll need to add the embed snippet to your application or website.
You can find the embed snippet in the Campaigns screen or by clicking the gear icon in the main header and then Conversion.
Copy the snippet and insert it just before the closing </head> tag in your
application or website.
Step 2 — Initialize conversion
When initializing, the script needs to know the ID of the subscription from your connected subscription platform to identify each subscriber as they visit your website or use your application.
Once you've added the embed snippet, call the ProsperStack.convert.init()
method with the ID of the subscription from your platform to complete the
initialization.
For example:
document.addEventListener("DOMContentLoaded", () => {
window.ProsperStack.convert.init({
subscription: {
platformId: "sub_H6X0WYrAzq2iGk",
},
});
});
We recommend signing your requests. Signing proves the subscription belongs to the logged-in user.
Once the init() method is called, ProsperStack will initialize your conversion
campaigns and begin displaying offers, unless the campaign is configured to
trigger manually. For manually triggered campaigns, see
Step 3.
Specifying a campaign
You can specify the conversion campaign to initialize with the campaignId
property.
To find the campaign ID:
- Open Campaigns in the Conversion section.
- Click the campaign name to open its editor.
- Copy the value shown next to Campaign ID.
Use the copied value when initializing Conversion. For example:
document.addEventListener("DOMContentLoaded", () => {
window.ProsperStack.convert.init({
campaignId: "cmpn_HsCgNwZxBe3fR9qVXEaCbB7H",
subscription: {
platformId: "sub_H6X0WYrAzq2iGk",
},
});
});
If no campaignId is specified, your default campaign will be used.
Sending custom properties
Custom properties allow you to attach custom data to your subscriber and subscription objects for more powerful targeting and segmentation.
You'll need to create custom properties in ProsperStack before storing values in them. Learn more about managing custom properties.
Specify custom properties to attach to either a subscriber or subscription
with the properties object. Use an object with the unique identifier of the
property as the key, for example:
{
number_of_contacts: 5800,
is_professional: true,
last_contacted: "2021-05-04",
preferred_name: "Johnny"
}
Here's an example of initialization with custom properties:
window.ProsperStack.convert.init({
subscription: {
platformId: "sub_H6X0WYrAzq2iGk",
properties: {
is_professional: true,
num_contacts: 5800,
},
},
subscriber: {
properties: {
preferred_name: "Johnny",
},
},
});
Learn more about formatting values for custom properties.
Handling events
The init() method provides the following callbacks to monitor offer events:
-
onOfferPresented?: () => voidCalled when an offer is presented.
-
onOfferClosed?: (result: CampaignResult) => voidCalled when an offer is closed. The
resultobject contains astatusproperty with one of the following values:accepted— The subscriber accepted the offer.dismissed— The subscriber dismissed the offer. The offer can be shown again after a delay.declined— The subscriber declined the offer. The offer will not be shown to the subscriber again.expired— The offer expired.
-
onError?: (error: Error) => voidCalled when the campaign can't start or its offer can't load, for example when a signature doesn't match or has expired. No offer is shown, and the error is also logged to the console.
Here's an example of handling offer events:
document.addEventListener("DOMContentLoaded", () => {
window.ProsperStack.convert.init({
campaignId: "cmpn_HsCgNwZxBe3fR9qVXEaCbB7H",
subscription: {
platformId: "sub_H6X0WYrAzq2iGk",
},
onOfferPresented: () => {
console.log("Offer presented");
},
onOfferClosed: (result) => {
console.log("Offer closed with status", result.status);
},
});
});
Step 3 — Triggering manual campaigns
This step is only required if the campaign is configured to trigger manually.
If the initialized campaign is configured to trigger manually, you can trigger
the campaign by calling the ProsperStack.convert.trigger() method.
Signing requests
In order to verify the authenticity of requests to start a campaign session, we recommend signing your requests with your account secret.
Signing proves that the subscription belongs to the logged-in user. Without it, anyone who knows a subscription ID could open a campaign session for that subscription.
Signing works the same way as signing requests for the cancellation flow. It uses the same HMAC-SHA256 signature and the same account secret. You can find your account secret in the Account section of your ProsperStack settings. Click the gear icon in the main header to access settings.
There are two ways to sign a request:
Option 1 — A signed payload
Build the payload on your backend server, sign it, and render both the payload and the signature into the page.
The payload is a JSON string with the following properties:
-
type: "campaign"- (required) - Must be the string"campaign". -
clientId: string- (required) - Your unique client ID. You can find your client ID in your ProsperStack settings page. -
campaignId?: string- (optional) - The ID of the campaign to initialize. If omitted, your default campaign is used. -
subscription: object- (required)platformId: string- (required) - The ID of the subscription from your connected subscription platform.properties?: object- (optional) - Subscription custom properties.
-
subscriber?: object- (optional)properties?: object- (optional) - Subscriber custom properties.
-
timestamp: number- (required) - The time you created the payload, in Unix seconds.
The signature is a hex HMAC-SHA256 digest of the exact payload string, computed with your account secret. Here's an example in Node.js:
import crypto from "crypto";
const payload = JSON.stringify({
type: "campaign",
clientId: "acct_IrbFC8mvFtDAugC8k7uNlRn6",
campaignId: "cmpn_HsCgNwZxBe3fR9qVXEaCbB7H",
subscription: {
platformId: "sub_H6X0WYrAzq2iGk",
},
timestamp: Math.floor(Date.now() / 1000),
});
const secret = "my account secret";
const signature = crypto
.createHmac("sha256", secret)
.update(payload)
.digest("hex");
Create the signature on your backend server, so that the secret is not exposed to the client. Make sure not to do this in the browser!
Then pass the payload and signature to init(), along with any event
handlers:
document.addEventListener("DOMContentLoaded", () => {
window.ProsperStack.convert.init({
payload:
'{"type":"campaign","clientId":"acct_IrbFC8mvFtDAugC8k7uNlRn6","campaignId":"cmpn_HsCgNwZxBe3fR9qVXEaCbB7H","subscription":{"platformId":"sub_H6X0WYrAzq2iGk"},"timestamp":1767225600}',
signature:
"c9efbfb8121c892b5de8c4e7c0c9182db7f3d87aa4ee463191e06fe36f4b00be",
onOfferPresented: () => {
console.log("Offer presented");
},
});
});
Pass the payload string exactly as you signed it. Don't parse and re-serialize it.
Each signed payload is for one user. Never cache a payload and signature
pair and serve it to other users.
Don't pass subscription, subscriber or campaignId next to payload. Put
them inside the payload instead. Passing both is an error.
Option 2 — The sign callback
Instead of rendering a signed payload, you can pass a sign callback to
init() along with campaignId, subscription and subscriber as usual.
The script builds the payload for you, including type and timestamp, and
calls sign with the JSON-encoded payload string. Your callback sends the
string to your backend, which signs it and returns the hex HMAC-SHA256 signature
digest.
sign?: (payload: string) => string | Promise<string>
Here's an example of calling a backend method to sign the payload:
document.addEventListener("DOMContentLoaded", () => {
window.ProsperStack.convert.init({
campaignId: "cmpn_HsCgNwZxBe3fR9qVXEaCbB7H",
subscription: {
platformId: "sub_H6X0WYrAzq2iGk",
},
sign: async (payload) => {
const response = await fetch("https://app.example.com/sign", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ payload }),
});
const { signature } = await response.json();
return signature;
},
});
});
Your signing endpoint must do two things:
- Check the subscription. Parse the payload and confirm that
subscription.platformIdbelongs to the logged-in user before signing. If your endpoint signs whatever it receives, anyone can get a signature for any subscription, and signing no longer protects you. - Sign the string exactly as received. Compute the HMAC over the payload string you were sent. Don't parse and re-serialize it first.
How long a signature is valid
A signed payload is accepted for 1 hour from its timestamp. Timestamps up to 5
minutes in the future are also accepted, to allow for clock skew between your
server and ours.
If a page stays open longer than that, such as a single-page application, use
the sign callback. It signs at the time
init() is called, so the timestamp is always fresh.
Signed sessions stay signed
Once a subscriber's campaign session has been opened with a signed request, unsigned requests for that subscriber don't show an offer. Sign every page that runs the campaign.
Requiring signed requests
To require every campaign request on your account to be signed, go to Settings
→ Conversion and check Require signed campaign requests under
Security. Once it's on, an unsigned request doesn't open a campaign session
and your onError callback receives the error. A session that was opened
without a signature can't accept an offer either.
Content Security Policy (CSP)
If your application uses a Content Security Policy, you'll need to add the following directives to your policy:
connect-src—https://api.prosperstack.comandhttps://graphql.prosperstack.comframe-src—https://app.prosperstack.com
The script calls https://api.prosperstack.com to start the campaign session
and https://graphql.prosperstack.com to load the offer. The offer itself
displays in a frame from https://app.prosperstack.com.
And if loading the library via a <script> tag from the CDN:
script-src-https://cdn.prosperstack.com