Skip to main content

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

Google Analytics 4 for Dartvel apps — gtag in the browser, the Measurement Protocol from the backend and from devices, consent-aware.

dartvel_google_analytics #

Google Analytics 4 for Dartvel applications: Google's tag in the browser, the Measurement Protocol from your backend and from devices.

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

Add it #

dartvel add google-analytics

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

dartvel:
  modules:
    googleAnalytics:
      package: dartvel_google_analytics
      backendPolicy: public
      config:
        measurementId: G-XXXXXXXXXX
      grant:
        secrets: [GA4_MEASUREMENT_ID, GA4_API_SECRET]
        egress: [www.google-analytics.com, region1.google-analytics.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 Measurement Protocol, the API secret in the server's environment (Admin > Data streams > your stream > Measurement Protocol API secrets):

GA4_API_SECRET=...

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

Who counts which page. Google's tag counts the page it loads on: it sends that page_view itself, from the address (campaign utm_* parameters included), whatever the page's own configuration says. With Ignore duplicate instances of on-page configuration on (the default for new tags, ignoreDupeGtagConfigCalls in gtag/js?id=G-...) it ignores send_page_view: false, so the module never sends a copy of that page. The module counts every route change after it, at the page's own address: the shell reports a page once its frame is drawn, which can be before the router has put the new address in the address bar. If consent arrives later, whatever loads the tag (the consent itself or an event) leaves the page on screen to the tag too.

One setting in Google Analytics itself: turn off Page changes based on browser history events under the web stream's Enhanced Measurement if your tag counts route changes on its own, or each route change is counted twice. Or set pageViews: automatic and let Google's tag count them all.

What it does without being called #

  • Page views. One page_view per page. Google's tag counts the page it loads on, campaign included (read from the landing address; see DV.Analytics.attribution); every route change after it sends page_view with page_location (the page's own address), page_path and page_title.
  • The framework's standard events. DV.Analytics.track(DVPurchase(...)) and the other DVStandardEvents (DVViewItem, DVAddToCart, DVBeginCheckout, DVSignUp, DVLogin, DVGenerateLead) are sent to gtag as the recommended event of the same name the moment they are tracked, under this module's consent category.
  • 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 with the buyer's client and session ids, so it lands in the session they were in.

Events #

Google's recommended events, with its parameter names:

DV.Modules.googleAnalytics.generateLead(leadSource: 'contact form');
DV.Modules.googleAnalytics.beginCheckout(
  value: 45000,
  currency: 'NGN',
  items: [GoogleAnalyticsItem(itemId: 'SKU-1', itemName: 'Website audit', price: 45000, quantity: 1)],
);
DV.Modules.googleAnalytics.viewItem(items: [GoogleAnalyticsItem(itemName: 'Pro plan')]);
DV.Modules.googleAnalytics.signUp(method: 'email');
DV.Modules.googleAnalytics.login(method: 'google');
DV.Modules.googleAnalytics.track(GoogleAnalyticsEvent('booked_demo', params: {'plan': 'pro'}));

An event Google Analytics would silently drop is refused when it is made: a reserved name or prefix, a name over 40 characters, more than 25 parameters.

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

Where an event goes #

Called from Google's tag (gtag) Measurement Protocol
A browser yes no
A device (Android, iOS, desktop, TV) no yes, through your backend
Your backend no yes, directly

One route only, because Google Analytics has no way to count an event sent twice as one. A device's event reaches your backend at <apiBasePath>/_dv/modules/googleAnalytics/collect, which is why the mount names a backendPolicy; the route accepts an event Google Analytics would accept, a client id and a session id, and nothing else.

On the backend an event needs the visitor's client id:

await DV.Modules.googleAnalytics.generateLead(clientId: lead.analyticsClientId);

A device has no tag to give it a client id, so each launch is a new visitor unless the application supplies one it keeps:

DV.Modules.googleAnalytics.identify(clientId: installId, userId: user?.id);

GoogleAnalyticsMeasurementProtocol(config).validate(...) asks Google's validation server what is wrong with an event; the Measurement Protocol itself answers 2xx to a malformed one.

The application decides whether consent is required, by declaring a policy under dartvel.analytics.consent.

  • With a policy, nothing is loaded or sent until the module's category is granted: not Google's tag, not a server event. The category is product 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 tag is told (gtag('consent', 'update', {analytics_storage: 'denied'})) and events stop.
  • Consent Mode v2. With a policy, the tag starts with ad_storage, ad_user_data, ad_personalization and analytics_storage all denied (gtag('consent', 'default', ...), before config), then analytics_storage granted, since the tag is loaded only once the visitor granted the category. Advertising storage stays denied.
  • The visitor's choice is kept in the browser (IndexedDB), so the banner asks once rather than on every page load.
  • 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.

Content-Security-Policy #

Nothing to edit. The module declares what Google's CSP guide lists for Google Analytics without Ads features, and the build adds it to your dartvel.security.pageCsp: script-src https://www.googletagmanager.com; img-src https://www.googletagmanager.com https://*.google-analytics.com; connect-src https://www.googletagmanager.com https://*.google-analytics.com https://*.google.com. The two lines that define gtag are 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.googleAnalytics.config, or as GoogleAnalyticsConfig passed to DV.Modules.googleAnalytics.configure(...):

Setting Default
measurementId none The web stream's G- id. Without it the module does nothing. The server also reads GA4_MEASUREMENT_ID.
consent product The consent category to wait for.
pageViews manual manual (the tag counts the first page, the module each route change), automatic (Google's tag counts them all) or off.
collectInEu false Send Measurement Protocol events to region1.google-analytics.com.
origin app://app The address a device's pages are reported under, such as https://example.com.
excludePaths [/__studio] Paths that are not page views.

Not covered #

  • Google Ads features: the declared origins are the ones for Google Analytics alone.
  • Firebase app streams: events go to a web stream's measurement id.
0
likes
0
points
364
downloads

Publisher

unverified uploader

Weekly Downloads

Google Analytics 4 for Dartvel apps — gtag in the browser, the Measurement Protocol from the backend and from devices, consent-aware.

Homepage
Repository (GitHub)
View/report issues

Topics

#dartvel #analytics #google-analytics

License

unknown (license)

Dependencies

dartvel_core

More

Packages that depend on dartvel_google_analytics