Documentation
Feedback
Guides
Storefront Development

Storefront Development
FastStore
FastStore SDK
Analytics
The Analytics module manages events on a website. The module is composed of two main functions:
  • sendAnalyticsEvent: Fires events in the browser. Events fired using this function are shared only with the website's origin. The event is wrapped and sent to the Window object via standard postMessage calls.
  • useAnalyticsEvent: Intercepts fired events and usually communicates with an analytics provider.
{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAMklEQVR4nAEnANj/AAMdMwACIYSFlf///wAFHTUADyiBf5L76vQA4NTe39rfvdjVmuLRKvAT7JwtMQ0AAAAASUVORK5CYII=","img":{"src":"https://vtexhelp.vtexassets.com/assets/docs/src/analyticsmodule___37ad6fdb1e969835206b6d4b91461529.png","width":1487,"height":1109,"type":"png"}}
Events sent via the Analytics module are not automatically sent to any analytics provider (e.g., Google Analytics). To connect with a provider, configure the useAnalyticsEvent hook.
The Analytics module supports sending and receiving different events, allowing you to implement custom event types and override default ones.
Additionally, the Analytics module offers built-in ecommerce event types based on the Google Analytics 4 (GA4) data model.

List of native event types

The Analytics module comes with native types based on ecommerce events. All event types are available for use and extension. Here is a list of events natively supported by the Analytics module:
TypeParameters
add_payment_infocurrency, value, coupon, payment_type, and items
add_shipping_infocurrency, value, coupon, shipping_tier, and items
add_to_cartcurrency, value, and items
add_to_wishlistcurrency, value, and items
begin_checkoutcurrency, value, coupon, and items
loginmethod
page_viewpage_title, page_location, and send_page_view
purchasecurrency, transaction_id, value, affiliation, coupon, shipping, tax, and items
refundcurrency, transaction_id, value, affiliation, coupon, shipping, tax, and items
remove_from_cartcurrency, value, and items
searchsearch_term
select_itemitem_list_id, item_list_name, and items
select_promotionitem_list_id, item_list_name, and items
sharemethod, content_type, and item_id
signupmethod, content_type, and item_id
view_cartcurrency, value, and items
view_itemcurrency, value, and items
view_item_listitem_list_id, item_list_name, and items
view_promotionitems
Each event exports at least two types: one for the event parameters and one for the event type itself. For example, the add_to_cart event has two exported types: AddToCartParams<T extends Item = Item> and AddToCartEvent<T extends Item = Item>.
Some types are common to all events, such as the Item type. These types are particularly useful when overriding Item properties or a whole Item itself.

Google Analytics 4

Google Analytics is the industry-leading analytics solution that most ecommerce websites use, and the Analytics module was designed with this in mind. All the helper functions and hooks in the module use the Google Analytics 4 (GA4) data model by default. This allows you to use code hinting tools to receive suggestions of GA4 events and their recommended properties while coding.
To send the events to Google Analytics, use the useAnalyticsEvent hook. The hook must listen to events from sendAnalyticsEvents and add them to the dataLayer (recommended for Google Tag Manager) or call the gtag function directly (for gtag script implementations).

Code hinting

By leveraging the type definitions in the Analytics module, you can use IntelliSense suggestions for code hinting.
{"base64":"  ","img":{"width":1999,"height":1085,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":367153,"url":"https://vtexhelp.vtexassets.com/assets/docs/src/intellisense___44c8c3c8f2687f88e787ce38f867901c.png"}}
Contributors
1
Photo of the contributor
+ 1 contributors
Was this helpful?
Yes
No
Suggest Edits (GitHub)
Contributors
1
Photo of the contributor
+ 1 contributors
On this page