refined-rose-14039
03/20/2025, 2:04 PMlet visitor_id = localStorage.getItem("visitor_id");
console.log(this.context);
if (!visitor_id) {
visitor_id = Math.random().toString(36).substring(2, 15);
console.log('visitor_id didnt exist', visitor_id);
// or any other method to generate a random ID
localStorage.setItem("visitor_id", visitor_id);
}
let pageType = this.context.page_type;
async function initializeGrowthBook() {
try {
// Manually fetch features
const response = await fetch(`<https://cdn.growthbook.io/api/features/sdk-z2eQdvHSoRomlSFH>`);
const { features } = await response.json();
// Initialize GrowthBook with the features
const gb = new GrowthBook({
enableDevMode: true,
features,
apiHost: "<https://cdn.growthbook.io>",
clientKey: "sdk-z2eQdvHSoRomlSFH",
subscribeToChanges: true,
attributes: {
id: visitor_id, // Use the anonymous ID for user identification
},
trackingCallback: (experiment, result) => {
console.log("Experiment Viewed", {
experimentId: experiment.key,
variationId: result.key,
});
},
});
let experiment_set = localStorage.getItem("experiment_key");
if (!experiment_set) {
let experimentKey = gb.isOn("groove-mini-cart");
localStorage.setItem("experiment_key", experimentKey);
if(pageType === 'product') {
window.location.reload();
}
}
if (gb.isOn("groove-mini-cart")) {
console.log("Feature enabled!");
$body.addClass('in-experiment');
$dropdown.addClass('cart-dropdown-gb');
cartPreviewGb(secureBaseUrl, cartId);
} else {
console.log("Feature disabled");
cartPreview(secureBaseUrl, cartId);
}
return gb; // Return the instance if needed elsewhere
} catch (error) {
console.error("Error initializing GrowthBook:", error);
}
}
initializeGrowthBook().then(gb => {
});
strong-mouse-55694
03/20/2025, 3:34 PMtrackingCallback
. This function is what sends the experiment exposure data to your db, but right now it's just logging to the console.
console.log("Experiment Viewed", {
experimentId: experiment.key,
variationId: result.key,
});
We need to ensure that the data is getting to the right place. Which event tracker/db are your using (e.g., like GA4/BigQuery)?refined-rose-14039
03/21/2025, 2:50 PMstrong-mouse-55694
03/21/2025, 2:59 PMthirdPartyTrackingPlugin
to automatically track your experiment exposure data: https://docs.growthbook.io/lib/js#third-party-trackingrefined-rose-14039
03/25/2025, 1:06 PM// Initialize GrowthBook with the features
const pluginOptions = {
// By default, it will attempt to send to all 3 of these
trackers: ["segment", "gtag", "gtm"],
// Additional custom tracking callback
additionalCallback: (experiment, result) => {
console.log("Experiment Callback", {
experimentId: experiment.key,
variationId: result.key,
})
}
}
const gb = new GrowthBook({
enableDevMode: true,
features,
apiHost: "<https://cdn.growthbook.io>",
clientKey: "sdk-z2eQdvHSoRomlSFH",
subscribeToChanges: true,
plugins: [
thirdPartyTrackingPlugin(pluginOptions),
],
attributes: {
id: visitor_id, // Use the anonymous ID for user identification
},
trackingCallback: function(experiment, result) {
}
});
strong-mouse-55694
03/25/2025, 2:06 PMrefined-rose-14039
03/25/2025, 6:35 PMrefined-rose-14039
03/25/2025, 6:36 PMstrong-mouse-55694
03/25/2025, 7:53 PMconst pluginOptions = {
// By default, it will attempt to send to all 3 of these
trackers: ["gtag"],
}
const gb = new GrowthBook({
enableDevMode: true,
clientKey: "sdk-z2eQdvHSoRomlSFH",
plugins: [
thirdPartyTrackingPlugin(pluginOptions),
],
attributes: {
id: visitor_id, // Use the anonymous ID for user identification
},
});
refined-rose-14039
03/26/2025, 2:36 PMrefined-rose-14039
03/26/2025, 2:36 PMbitter-salesclerk-96251
03/26/2025, 2:43 PMasync function initializeGrowthBook() {
try {
// Manually fetch features
const response = await fetch(`<https://cdn.growthbook.io/api/features/sdk-z2eQdvHSoRomlSFH>`);
const { features } = await response.json();
// Initialize GrowthBook with the features
const pluginOptions = {
trackers: ["gtag"],
}
const gb = new GrowthBook({
enableDevMode: true,
features,
apiHost: "<https://cdn.growthbook.io>",
clientKey: "sdk-z2eQdvHSoRomlSFH",
plugins: [
thirdPartyTrackingPlugin(pluginOptions),
],
attributes: {
id: visitor_id, // Use the anonymous ID for user identification
}
});
bitter-salesclerk-96251
03/26/2025, 2:47 PMstrong-mouse-55694
03/26/2025, 2:58 PM<script>
// Wait for the GrowthBook SDK to load before running
window.growthbook_queue = window.growthbook_queue || [];
window.growthbook_queue.push((gb) => {
// Function that uses feature flags to make changes to the page
const applyFeatureFlags = () => {
if(gb.isOn("dark-mode")) {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
}
// Call your function initially plus whenever new data is received
applyFeatureFlags();
document.addEventListener("growthbookdata", applyFeatureFlags)
});
</script>
bitter-salesclerk-96251
03/26/2025, 3:51 PMbitter-salesclerk-96251
03/26/2025, 3:52 PMbitter-salesclerk-96251
03/26/2025, 3:57 PMimport { GrowthBook } from "@growthbook/growthbook";
import { thirdPartyTrackingPlugin } from "@growthbook/growthbook/plugins";
...
let visitor_id = localStorage.getItem("visitor_id");
if (!visitor_id) {
visitor_id = Math.random().toString(36).substring(2, 15);
// or any other method to generate a random ID
localStorage.setItem("visitor_id", visitor_id);
...
let pageType = this.context.page_type;
async function initializeGrowthBook() {
try {
// Manually fetch features
const response = await fetch(`<https://cdn.growthbook.io/api/features/sdk-z2eQdvHSoRomlSFH>`);
const { features } = await response.json();
// Initialize GrowthBook with the features
const pluginOptions = {
trackers: ["gtag"],
}
const gb = new GrowthBook({
enableDevMode: true,
features,
apiHost: "<https://cdn.growthbook.io>",
clientKey: "sdk-z2eQdvHSoRomlSFH",
plugins: [
thirdPartyTrackingPlugin(pluginOptions),
],
attributes: {
id: visitor_id, // Use the anonymous ID for user identification
}
});
let experiment_set = localStorage.getItem("experiment_key");
if (!experiment_set) {
let experimentKey = gb.isOn("groove-mini-cart");
localStorage.setItem("experiment_key", experimentKey);
if(pageType === 'product') {
window.location.reload();
}
}
if (gb.isOn("groove-mini-cart")) {
console.log("Feature enabled!");
$body.addClass('in-experiment');
$dropdown.addClass('cart-dropdown-gb');
cartPreviewGb(secureBaseUrl, cartId);
} else {
console.log("Feature disabled");
cartPreview(secureBaseUrl, cartId);
}
return gb; // Return the instance if needed elsewhere
} catch (error) {
console.error("Error initializing GrowthBook:", error);
}
}
// Call the async function
initializeGrowthBook().then(gb => {
});
bitter-salesclerk-96251
03/26/2025, 4:00 PMbitter-salesclerk-96251
03/26/2025, 4:00 PMstrong-mouse-55694
03/26/2025, 4:44 PMbitter-salesclerk-96251
03/31/2025, 6:28 PMbitter-salesclerk-96251
03/31/2025, 6:31 PMbitter-salesclerk-96251
03/31/2025, 6:31 PMstrong-mouse-55694
04/01/2025, 8:51 AMbitter-salesclerk-96251
04/01/2025, 1:49 PMbitter-salesclerk-96251
04/01/2025, 1:50 PMbitter-salesclerk-96251
04/01/2025, 1:51 PMbitter-salesclerk-96251
04/01/2025, 1:51 PMstrong-mouse-55694
04/02/2025, 11:57 AM