Date of slack thread: 5/17/24
Anonymous: Hi, I’m trying to initialize statsig in a cloudworker pages function - I’ve tried two things:
import {Statsig} from "statsig-node";
await Statsig.initialize(ctx.env.STATSIG_SDK_KEY,{environment: ...
here I got the error Cannot read properties of undefined (reading 'initialize')
I next tried
import Statsig from "statsig-js";
await Statsig.initialize(ctx.env.STATSIG_SDK_KEY, {
userID: ...
}, {
environment: ...
here I got a different error: import_statsig_js2.default.initialize is not a function
Statbot: To initialize Statsig in a Cloudflare Workers environment, you should be aware that if you are unable to initialize Statsig outside of a serverless function, you will incur a server round trip to Statsig servers to initialize the SDK in every function invocation. After initialization, all checks should be locally evaluated in the function. Additionally, you need to ensure any enqueued events are flushed to Statsig prior to the function exiting by calling statsig.flush()
.
Here’s an example of how you might initialize Statsig in a Cloudflare Worker:
import Statsig from "statsig-node";
await Statsig.initialize("server-secret-key");
// Your Cloudflare Worker logic here
await Statsig.flush();
Remember to replace "server-secret-key"
with your actual server secret key from the Statsig console. Also, do not expose your server secret key in client-side applications or any external facing documents. If you accidentally exposed it, you can create a new one in the Statsig console.
If this approach does not work for you, consider reaching out to the Statsig team through their Slack community to discuss custom integration options that may better suit your architecture and requirements source.
Anonymous: Is this a known issue that cloudflare’s runtime is different enough that I cannot work with statsig? Is there a different approach I should try?
Vijaye (Statsig): Try:
import Statsig from "statsig-node"
Anonymous: That did the trick! Thanks Vijaye (Statsig)
Anonymous: Typescript was pretty sure that it wanted Curley braces there, any idea why that is?
Statbot: This thread has been marked as resolved. Press below to re-open.
Vijaye (Statsig): No idea why Typescript misled you there.