Solutions
...
Integrating MomentPerks
JavaScript SDK Guide
14 min
overview integrating momentscience into your online store, website, mobile app, or other user experiences is quick and straightforward this guide walks you through how to add the javascript sdk to your website to start delivering personalized offers what you'll learn how to access and configure your sdk snippet how to add the snippet to your website or checkout page how to pass user specific payload values for targeting and personalization requirements before you begin, make sure you have your sdk id getting your sdk id docid\ nilh20nfdf9md1fctspu3 the "sdk id" is also referred to as the "account id" in the javascript snippet shown in the dashboard these terms are interchangeable, they refer to the same value integration steps to help you get started, the following interactive demo walks through the process of getting the javascript sdk code snippt from the dashboard step 1 access your integration code to retrieve your javascript sdk integration code go to the momentscience dashboard from the left sidebar, click integrations and select moments you'll be redirected to the momentperks integration page click the install theme tab, or go directly to this link your javascript sdk code will appear you can copy it directly or download it for later use step 2 add the sdk to your website paste the integration code before the closing \</head> tag of your site ensure your accountid value in your script matches your sdk id from the momentscience dashboard javascript code example \<script type="module"> (function (window) { window\ adpxconfig = { accountid 'your sdk id', // replace with your account's sdk id autoshow true }; window\ adpxuser = { // optional define user specific values }; })(window\ self !== window\ top ? window\ top window); (async function (window) { const target = window\ document head || window\ document body; const script = document createelement('script'); script type = 'text/javascript'; script src = 'https //cdn pubtailer com/launcher min js'; script crossorigin = 'anonymous'; script async = true; script id = 'adpx launcher'; target appendchild(script); await new promise((resolve) => { window\ adpx ? resolve() script addeventlistener('load', resolve); }); window\ adpx init(window\ adpxconfig); })(window\ self !== window\ top ? window\ top window); \</script> step 3 configure sdk initialization the momentscience js sdk uses window\ adpxconfig to configure offer behavior at runtime you must set this configuration before the sdk script is loaded on the page the table below outlines all supported attributes for the adpx init() configuration parameter type required description default accountid string yes your unique momentscience account identifier this is also referred to as your sdk id the sdk uses this value to associate offer delivery, analytics, and creative rendering with your account you can getting your sdk id docid\ nilh20nfdf9md1fctspu3 β autoload boolean no controls whether the sdk automatically fetches offers on initialization if true , the sdk loads offers automatically once initialized if false , you must manually trigger offer loading by calling window\ adpx refresh() this is useful if you want to delay offer fetching until after a specific user action or event true autoshow boolean no controls whether the offers are automatically shown after offers are loaded if true , offers will appear automatically once fetched if false , you must explicitly call window\ adpx show() to display offers note if autoload is set to false , autoshow has no effect and will be ignored true prefetch boolean no when true , the sdk fetches offers on the initial load and stores them in localstorage on subsequent visits within one hour, it reuses the cached offers and session id, enabling a consistent experience fetches and stores offers in localstorage during the first page load reuses the same offers and session id on the second page load, if within one hour maintains selected state across reloads when user selected perks (usp) is enabled prevents an additional network request on the second load by using cached data caches are valid for up to two page views or one hourβwhichever occurs first after expiration or third load, fetches new offers and generates a new session id false dev boolean no enables testing mode when true , the sdk fetches offers in a developer safe testing mode no engagement or impression data will be recorded geo targeting is disabled all possible offers are returned regardless of targeting use this mode when validating payloads or debugging false settings object no used to override default momentscience dashboard settings at runtime common use cases include forcing a specific placement or theme adjusting unit behavior per page {} styles object no used to apply custom styling to the moments unit at runtime supported style areas unit layout and spacing header appearance (background, text) content font styles and sizes primary cta (positive button) styling secondary cta (negative button) styling body font and colors footer content and style {} window\ adpxconfig = { accountid 'your sdk id', autoload true, autoshow true, dev true, settings {}, styles {} }; step 4 pass in user data (optional) you can optionally provide user specific payload values using the window\ adpxuser object this allows momentscience to personalize offer content, improve targeting, and enhance reporting user data must be defined before the sdk initialization ( window\ adpx init() ), and can be set from either the client side or injected server side at page render for detailed information about passing payload values using the js sdk, refer to passing payload values docid\ ttzzq9hb2joe g3mposj8 why pass user data? use case benefit offer personalization tailor offer messaging to create a more relevant and personal experience for users enhanced targeting deliver offers based on specific user attributes like geography, membership status, or custom segments customized reporting attribute offer interactions and claims to specific user identifiers or segments for deeper analytics loyaltyboost support enable reward based experiences for users who engage with partner offers using provided ids example payload data you can include any custom key value pairs, but the following are commonly used key example description placement 'post transaction' context in which the offer appears (e g , 'checkout' , 'thank you' ) pub user id 'user abc 123' unique identifier for the user from your system firstname 'john' userβs first name used in personalized messages zipcode '74839' helps geo target specific offers country 'us' country code (iso 3166 1 alpha 2) adpx fp 'df983hgjdl9023klasjd' adpx fingerprint or tracking token custom fields 'plan' 'gold' add any extra targeting or analytics attributes as needed example payload data window\ adpxuser = { placement 'post transaction', pub user id `uniquepubuserid`, firstname 'john', zipcode '74839', country 'us', adpx fp 'df983hgjdl9023klasjd', loyalty program id '23445665393', }; step 5 verify your integration to confirm your integration is working visit a page where the sdk is embedded you should see offers displayed if you're using adpxuser , open your browserβs devtools and confirm those values are present before adpx init() is called if you encounter any issues during integration, contact us at help\@momentscience com