How to personalize onsite content with Klaviyo’s group membership API
Estimated 3 minute read
|
Updated Oct 21, 2026, 12:00 AM EST
Table of contents
- You will learn
- Before you begin
- Install Klaviyo.js and load the Klaviyo object
- Check list or segment membership
- Impact on site performance
- Outcome
- Additional resources
You will learn
Learn how to use the klaviyo JavaScript object’s getGroupMembership method to check an identified visitor’s list or segment membership and use the results to personalize your website. This requires loading the klaviyo object and passing an array of list or segment IDs to check.
Advanced KDP is not included in Klaviyo’s standard marketing application, and a subscription is required to access the associated functionality. Head to our billing guide to learn how to purchase this plan.
Before you begin
This feature is only available for Advanced KDP customers, and is only available through the klaviyo object. To learn more about the klaviyo JavaScript object and what it can do, read the introduction to the Klaviyo object.
You’ll need access to your website’s code. Work with a developer if you need help with JavaScript, installing the snippets, or applying personalization to your site.
The group membership API requires Klaviyo to identify the visitor’s browser and match it to a profile. It cannot return list or segment membership for an unidentified visitor. Common ways Klaviyo identifies a visitor include:
- If someone has clicked through a Klaviyo email to your website
- If someone has subscribed through a Klaviyo form
- If your site uses custom tracking to identify someone to Klaviyo when they log in
See Klaviyo’s video on using the group membership API.
Install Klaviyo.js and load the Klaviyo object
First, install Klaviyo.js if you have not already done so. Klaviyo.js, also known as Klaviyo’s Active on Site JavaScript, supports the klaviyo object. If your ecommerce integration has installed Klaviyo.js on your site, you do not need to install it again. Otherwise, install it manually before using the klaviyo object.
To use the klaviyo object immediately on page load, we recommend manually installing the snippet below on your site (in addition to installing Klaviyo.js as mentioned above). The klaviyo object only needs to be loaded once per page.
To load the klaviyo object:
textCopy
!(function () {
if (!window.klaviyo) {
window._klOnsite = window._klOnsite || [];
try {
window.klaviyo = new Proxy(
{},
{
get: function (n, i) {
return "push" === i
? function () {
var n;
(n = window._klOnsite).push.apply(n, arguments);
}
: function () {
for (
var n = arguments.length, o = new Array(n), w = 0;
w < n;
w++
)
o[w] = arguments[w];
var t =
"function" == typeof o[o.length - 1] ? o.pop() : void 0,
e = new Promise(function (n) {
window._klOnsite.push(
[i].concat(o, [
function (i) {
t && t(i), n(i);
},
]),
);
});
return e;
};
},
},
);
} catch (n) {
(window.klaviyo = window.klaviyo || []),
(window.klaviyo.push = function () {
var n;
(n = window._klOnsite).push.apply(n, arguments);
});
}
}
})();
Check list or segment membership
To use the klaviyo JavaScript object’s getGroupMembership method for onsite personalization:
- Within your code, select the list or segment IDs you want to check membership for. You can pass up to 50 IDs in total per call.
- Identify the visitor on your site via a Klaviyo form or another identification method. If you call klaviyo.identify in your code, wait for it to complete before checking membership.
- Make a call in the following format, with an array of the list or segment IDs you want to check membership for:
textCopy
klaviyo.getGroupMembership(['listID1', 'listID2', 'listID3'])
The method returns a Promise that resolves to an array containing only the IDs from your input array that the identified visitor belongs to. Use .then() to work with the result, as shown below, or await the result inside an async function. An empty array can mean that the visitor does not belong to any of the lists or segments you provided, that the visitor could not be identified, that you passed more than 50 IDs, or that the request failed. An empty result alone does not distinguish these cases.
Use the returned list and segment IDs in your website’s code or a web personalization tool to choose relevant products, content, and more. Add the logic that changes your page after the result is available, and keep default content available when no matching IDs are returned.
Code example
This example checks membership in multiple segments. Replace the placeholder strings with your segment IDs. The code inside .then() runs after the result is available; add your site’s personalization code where indicated.
textCopy
klaviyo.getGroupMembership([
'VIPSegmentID',
'UnEngagedSegmentID',
'DogLoversSegmentID'
]).then((customerSegments) => {
if (customerSegments.includes('VIPSegmentID')) {
// Add code to display your VIP content here.
}
});
Impact on site performance
Klaviyo loads the web personalization script asynchronously. Keep your site’s default content available while the membership request completes, then apply personalization after you receive the result.
Membership responses can be cached by the browser, allowing repeated requests for the same visitor and IDs to reuse the cached response. A different request or an expired cache can require another API call, so the result is not guaranteed to reflect a membership change immediately.
Outcome
You can now use the klaviyo JavaScript object with web personalization tools and to personalize onsite content based on segment or list membership.
Additional resources
Was this article helpful?
YesNo
Use this form only for article feedback. Learn how to contact support.
Explore more from Klaviyo
Community
Connect with peers, partners, and Klaviyo experts to find inspiration, share insights, and get answers to all of your questions.
Partners
Hire a Klaviyo-certified expert to help you with a specific task, or for ongoing marketing management.
Support
Access support through your account.
Email support (free trial and paid accounts) Available 24/7
Chat/virtual assistance Availability varies by location and plan type
