Worker Analytics Beta not working
There is a. beta feature that allows you to write custom analytics from Cloudflare Workers/pages
I have enabled the analytics feature on the Pages overview.
I click on setup and follow the instructions.
I have created an entry in my toml
[[analytics_engine_datasets]]
binding = "ANALYTICS"
I have added the binding to my env so my code can call it like this
env.ANALYTICS.writeDataPoint({
blobs: ['signup', 'beta', 'failed'],
indexes: [email, firstName, lastName],
});
but I get an error when running the code locally as the local env.ANALYTICS resource being bound to doesn't seem to exist:
TypeError: Cannot read properties of undefined (reading 'writeDataPoint')
If I push it to preview it doesn't create the resource.
I have manually gone into the function settings and set the binding on
Analytics Engine bindings at the bottom of the page, still nothing.
Any ideas?
4 Replies
Pages doesn't support the wrangler.toml, it shouldn't do anything in local dev and wouldn't do anything when deployed, the bindings you have to all set in your Functions settings of your Pages project.
Did you set it for both Production & Preview in your Pages Project?
The way you access it also varies on Pages, if you're using normal Functions, it'd be on context.env.<binding-name>. There's some docs for Pages with Analytics Engine here: https://developers.cloudflare.com/pages/platform/functions/bindings/#analytics-engine
Bindings · Cloudflare Pages docs
A binding enables your Pages Functions to interact with resources on the Cloudflare developer platform. Use bindings to integrate your Pages Functions …
yeah I set it manually there ... still didn't work
oh wait ... it started working
woo
I'm having this exact issue. Sadly I don't quite understand @Chaika's explanation. I'm just using the wrangler.toml, where do I need to setup the analytics engine?