Skip to main content

dartvel_meta_pixel 3.47.14 copy "dartvel_meta_pixel: ^3.47.14" to clipboard
dartvel_meta_pixel: ^3.47.14 copied to clipboard

Meta (Facebook) Pixel for Dartvel apps — page views and events in the browser, the Conversions API from the backend, de-duplicated, consent-aware.

dartvel_meta_pixel #

Meta (Facebook) Pixel for Dartvel applications: page views and events in the browser, the same events through your backend to the Conversions API, counted once.

A first-party Dartvel module. It lives in modules/ of the Dartvel repository and is released with the framework.

Add it #

dartvel add meta-pixel

That writes the dependency and the mount. Give it your pixel id:

dartvel:
  modules:
    metaPixel:
      package: dartvel_meta_pixel
      backendPolicy: public
      config:
        pixelId: "1234567890"
      grant:
        secrets: [META_PIXEL_ID, META_CONVERSIONS_API_TOKEN]
        egress: [graph.facebook.com]

The grant: is what the module's code reaches past it, and dartvel add writes it. The build reads the same list from the code and refuses a mount whose grant differs (DV-MODULE-001, DV-MODULE-003). No pin is needed: the module is released with the framework, and resolved from the same checkout or release as dartvel_core it carries the framework's identity.

and, for the Conversions API, the access token in the server's environment (.env in development, your host's secrets in production):

META_CONVERSIONS_API_TOKEN=...

The token is never a setting. A mount's config: is compiled into the client, so accessToken: there is refused by name.

The pixel id can live on the server too. Leave pixelId empty and set META_PIXEL_ID beside the token: the backend uses it, and a browser or device asks the backend which pixel to load (once per page load, and only after the visitor consents), so setting the two secrets turns the pixel on with no code change or rebuild.

META_PIXEL_ID=1234567890
META_CONVERSIONS_API_TOKEN=...

What it does without being called #

  • Page views. The first page and every route change send PageView. The page shell reports them; no page calls anything. Each goes out once from the browser and once to the Conversions API with one event id, so Meta counts it once: the pixel's own automatic PageView on every history.pushState (which has no event id, so Meta could not de-duplicate it) is switched off with fbq.disablePushState, and fbq.allowDuplicatePageViews lets the module's views of later routes through, which fbevents.js would otherwise drop after the first of a page load. The landing page's view carries the utm_* parameters it arrived with as event data.
  • The framework's standard events. DV.Analytics.track(...) with a DVStandardEvent is sent as Meta's standard event the moment it is tracked, browser and Conversions API with one event id: DVViewItem is ViewContent, DVAddToCart AddToCart, DVBeginCheckout InitiateCheckout (with num_items), DVSignUp CompleteRegistration, DVGenerateLead Lead (its lead source as content_name) and DVPurchase Purchase with the event id purchase.<transaction id>, the one a store's server-side purchase uses. Items become content_ids and contents. DVLogin has no Meta event and sends nothing.
  • The click id survives the page. The fbclid a visit landed with is kept on the device (DV.Analytics.attribution), so an event on a later page, or a pixel loaded only after the visitor accepted the banner on the second page, still carries it: the module writes the _fbc cookie in Meta's format (fb.1.<time>.<fbclid>, 90 days) when the pixel loads and there is none, and sends it as fbc to the Conversions API.
  • Purchases, when a store module such as dartvel_woocommerce is mounted: the store publishes a purchase when the payment is confirmed, and this module reports Purchase from the backend, and from the buyer's browser when it learns the order is paid, both with the event id purchase.<order id>.

Events #

DV.Modules.metaPixel.viewContent(contentName: 'Pricing');
DV.Modules.metaPixel.lead(contentName: 'Callback form');
DV.Modules.metaPixel.contact();
DV.Modules.metaPixel.initiateCheckout(value: 45000, currency: 'NGN', numItems: 1);
DV.Modules.metaPixel.purchase(value: 45000, currency: 'NGN', eventId: 'order-981');
DV.Modules.metaPixel.trackCustom('BookedDemo', data: {'plan': 'pro'});
DV.Modules.metaPixel.track(MetaPixelEvent('Schedule'));

A tap that leaves the application, such as a WhatsApp button:

.onTap(() {
  DV.Modules.metaPixel.outboundClick(whatsApp);   // a Contact
  openLink(whatsApp);   // however the application opens a link
})

A WhatsApp link, a phone number or an email address is a Contact. Pass event: MetaPixelEvent.lead() to report it as a Lead instead. Any other link is a custom OutboundClick with the destination, without its query.

Every call is safe anywhere: with no pixel id, before consent, with the pixel's script blocked or the network down, it does nothing and does not throw.

Where an event goes #

Called from Browser pixel (fbq) Conversions API
A browser yes yes, through your backend, same event_id
A device (Android, iOS, desktop, TV) no yes, through your backend
Your backend no yes, directly

Meta counts a browser event and a server event once when their name and id match, so an in-app browser that blocks the pixel still reports through the server. A client's event reaches your backend at <apiBasePath>/_dv/modules/metaPixel/collect, which is why the mount names a backendPolicy. That route accepts an event name, a web address, up to 4 KB of parameters and the _fbp/_fbc cookies, and nothing else; the backend adds the caller's address and user agent from the request.

On the backend there is no page, so say who and where:

await DV.Modules.metaPixel.track(
  MetaPixelEvent.lead(),
  user: MetaUserData(email: lead.email, phone: lead.phone),
  sourceUrl: Uri.parse('https://example.com/contact'),
);

MetaUserData normalises and SHA-256 hashes the email, phone, names, city, country and postcode as Meta requires, and sends the address, user agent, fbp and fbc unhashed, as Meta requires.

The application decides whether consent is required, by declaring a policy:

dartvel:
  analytics:
    consent:
      version: "2026-11-01"
      categories:
        essential: { required: true }
        marketing: { default: denied, tracking: true }
  • With a policy, nothing is loaded or sent until the module's category is granted: not the pixel's script, not a server event. The category is marketing unless config.consent names another. A category the policy does not declare is never granted, and the module says so once in the log. When consent is withdrawn the pixel is told (fbq('consent', 'revoke')) and events stop.
  • With no policy, the application has not asked for consent to be required and the module sends from the first page. This is the default.

A Global Privacy Control signal is honoured for a tracking: true category by the framework, and by the backend route for a client that sent the event anyway.

Content-Security-Policy #

Nothing to edit. The module declares the origins the pixel needs and the build adds them to your dartvel.security.pageCsp: script-src https://connect.facebook.net, and connect-src and img-src https://www.facebook.com. The pixel's base code is served from your own origin as a file, so no 'unsafe-inline' is needed. With no page policy declared, none is sent and nothing is added.

Settings #

Under dartvel.modules.metaPixel.config, or as MetaPixelConfig passed to DV.Modules.metaPixel.configure(...):

Setting Default
pixelId none The pixel's id. Without it the module does nothing. Leave it empty (or out) to use the server's META_PIXEL_ID: the backend reads it, and a browser or device asks the backend for it once, after consent, so the pixel turns on by setting the secret, with no rebuild.
consent marketing The consent category to wait for.
serverEvents true Send a client's events through the backend to the Conversions API.
apiVersion v27.0 The Graph API version.
testEventCode none Events Manager's test code; unset it in production.
excludePaths [/__studio] Paths that are not page views.

Not covered #

  • App install attribution and SKAdNetwork: a device's events are sent with action_source: other, since Meta's app source needs app data this module does not collect. Use Meta's SDK for those.
  • A visitor with JavaScript off sends no page view: there is no <noscript> image.
0
likes
0
points
419
downloads

Publisher

unverified uploader

Weekly Downloads

Meta (Facebook) Pixel for Dartvel apps — page views and events in the browser, the Conversions API from the backend, de-duplicated, consent-aware.

Homepage
Repository (GitHub)
View/report issues

Topics

#dartvel #analytics #facebook

License

unknown (license)

Dependencies

crypto, dartvel_core

More

Packages that depend on dartvel_meta_pixel