hundreds-student-36571
08/29/2025, 4:25 PMexport const gbInstance = new GrowthBook({
apiHost: import.meta.env.VUE_APP_GROWTHBOOK_API_HOST,
clientKey: import.meta.env.VUE_APP_GROWTHBOOK_CLIENT_KEY,
enableDevMode: !isProduction(),
plugins: [autoAttributesPlugin()],
trackingCallback: (experiment, result) => {
$analytics.track({
event_type: 'Experiment Viewed',
event_properties: {
experimentId: experiment.key,
variationId: result.key
}
});
console.log(`key-${experiment.key} result-${result.key}`);
}
});
const initializeGrowthBook = async () => {
try {
if ($analytics) {
gbInstance.updateAttributes({
id: $analytics.amplitude.getUserId(),
deviceId: $analytics.amplitude.getDeviceId()
});
}
await gbInstance.init({ streaming: true });
gbFlags.initialize(gbInstance);
return gbInstance;
} catch (e) {
return null;
}
};
I’m implementing it like this, and in the plugins I specify autoAttributesPlugin(). That generates attributes for targeting (one of them is id).
When setting up an experiment in the admin panel, you can assign it based on anonymous_id or user_id. How is this id connected with user_id or anonymous_id? Or do I need to explicitly set user_id and anonymous_id when initializing?
If so, how should this be connected with Amplitude, since it has both user_id and device_id? (edited)strong-mouse-55694
08/29/2025, 7:03 PMhundreds-student-36571
08/29/2025, 8:41 PMuser_id and anonymous_id are based on (how they’re generated) and how to link them with Amplitude, because when configuring metrics here I can only choose these two attributes. Please explain it step by step, if possible, so I can have a better understanding—I’d be very grateful.hundreds-student-36571
08/29/2025, 8:52 PMconst initializeGrowthBook = async () => {
try {
if ($analytics) {
gbInstance.updateAttributes({
user_id: $analytics?.amplitude.getUserId() || null,
anonymous_id: $analytics?.amplitude.getDeviceId() || null
});
}
await gbInstance.init({ streaming: true });
gbFlags.initialize(gbInstance);
return gbInstance;
} catch (e) {
return null;
}
}
first i will have user_id null for anonymous users. After login my user_id i will set equal to user id from amplitude
gbFlags.updateAttributes({
user_id: this.$analytics.amplitude.getUserId()
});
after logout i will do this
window.experiment.clear();
window.amplitude.amplitude.setUserId(null);
gbFlags.updateAttributes({
user_id: window.amplitude.amplitude.getUserId()
});
user_id here equal nullhundreds-student-36571
08/29/2025, 9:00 PMstrong-mouse-55694
08/29/2025, 9:33 PMhundreds-student-36571
08/30/2025, 5:05 AMconst featureCache = reactive(
Object.fromEntries(
Object.keys(GROWTHBOOK_FEATURES).map((feature) => [feature, null])
) as Record<FeatureKey, boolean | string | null>
);
let gbInstance: GrowthBook | null = null;
export const gbFlags = {
initialize(gb: GrowthBook) {
gbInstance = gb;
gbInstance.setRenderer(() => {
Object.keys(featureCache).forEach((key) => {
featureCache[key as FeatureKey] =
gbInstance?.evalFeature(GROWTHBOOK_FEATURES[key as FeatureKey])
?.value ?? null;
});
});
},
async updateAttributes(attributes: Record<string, string>) {
gbInstance?.updateAttributes(attributes);
},
I have services function like thisstrong-mouse-55694
09/02/2025, 1:00 PM