straight-dog-45711
03/26/2024, 2:45 PM/landing-page-a
or /landing-page-b
.
Wondering what the best way would be to handle the assignment to the a / b group using growthbook in this case. I'm guessing maybe we could append a query param from google ads, but curious if anyone might have a solution for this type of thing off the top of their headfresh-football-47124
fresh-football-47124
straight-dog-45711
03/26/2024, 6:14 PMstraight-dog-45711
03/26/2024, 10:05 PM// Function to retrieve a cookie's value by name
function getCookie(name) {
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
return match ? decodeURIComponent(match[2]) : null;
}
// Function to configure GrowthBook with attributes including the anonymousId
function configGrowthbook(anonymousId) {
window.growthbook_config = window.growthbook_config || {};
window.growthbook_config.attributes = { anonymousId };
console.log("Configured GrowthBook with Anonymous ID:", anonymousId);
}
// Try to initialize GrowthBook with the anonymous ID from the cookie
const anonymousIdFromCookie = getCookie("ajs_anonymous_id");
if (anonymousIdFromCookie) {
configGrowthbook(anonymousIdFromCookie);
} else {
// Wait for analytics to be ready if the cookie is not found
console.log("Anonymous ID cookie not found, waiting for analytics...");
analytics.ready(function() {
const analyticsAnonymousId = analytics.user().anonymousId();
if (analyticsAnonymousId) {
configGrowthbook(analyticsAnonymousId);
} else {
console.error("Failed to retrieve Anonymous ID after analytics ready.");
}
});
}
<script async="" data-api-host="<https://cdn.growthbook.io>" data-client-key="sdk-xEb0AZIn228345Oj" src="<https://cdn.jsdelivr.net/npm/@growthbook/growthbook/dist/bundles/auto.min.js>">
</script>
straight-dog-45711
03/27/2024, 12:37 AMajs_anonymous_id
cookie / local storage value and grab it or use the analytics.user().anonymousId()
method to get it.
The using the latter method, however, won't work until Segment is initialized. Typically, you can use the analytics.ready(function() { Logic Here })
call back to make sure you're going to be able to get an anonymous id, but if the user is using an ad blocker and Segment is loading client side destinations - chances are those libraries will fail to load and thus the ready callback will never happen (this also happens with Rudderstack btw).
So, our solution was to check for the cookie and then generate our own UUID and pass it to Segment as our own Anonymous ID like so analytics.user().anonymousId(anonymousId);
Anyway, this was not intuitive and so maybe worth investigating further / mentioning in the docs or handling in the SDK so folks don't have to roll their own code to make GB work when using Segment, Rudderstack, etc