Event factory
GA4 provides a list of recommended events and automatically-collected events. And for these events Google also provides a list of recommended event parameters.
EventFactory
provides the methods to create all recommended and automatically-collected events, and for each event it also adds standard event parameters. So you don't need to define event parameters manually.
#
How to use event factoryTo use EventFactory
you need to init the object with config and then call the method to create events. EventFactory provides a special method for each event. Using this pattern createEventName
so for example to create add_to_cart
event you could use creatAddToCart
method, for page_view
event - createPageView
method and so on. For example:
const config = { dataset: "analytics_XXXXXX", incrementalTableName: "events_XXXXXX",};let ef = new ga4.EventFactory(config);let addToCart = ef.createAddToCart();let pageView = ef.createPageView();addToCart.publish();pageView.publish();
note
Don't forget to publish events after their creation.
These helper methods (like createEventName
) actually do these steps:
- create a new Event object
- call
setEventName
with the event name - add all recommended event parameters for the particular event using
addEventParameters
method
#
Event factory methodsYou could find a list of supported events in the file includes/recommended_events.js
or in the table below.
Method Name | Event Name | Event Parameters |
---|---|---|
createAddPaymentInfo | add_payment_info | coupon (string), currency (string), payment_type (string), value (coalesce_float) |
createAddShippingInfo | add_shipping_info | coupon (string), currency (string), shipping_tier (string), value (coalesce_float) |
createAddToCart | add_to_cart | currency (string), value (coalesce_float) |
createAddToWishlist | add_to_wishlist | currency (string), value (coalesce_float) |
createBeginCheckout | begin_checkout | coupon (string), currency (string), value (coalesce_float) |
createEarnVirtualCurrency | earn_virtual_currency | virtual_currency_name (string), value (coalesce_float) |
createGenerateLead | generate_lead | value (coalesce_float), currency (string) |
createJoinGroup | join_group | group_id (string) |
createLevelEnd | level_end | level_name (string), success (string) |
createLevelStart | level_start | level_name (string) |
createLevelUp | level_up | character (string), level (int) |
createLogin | login | method (string) |
createPostScore | post_score | level (string), character (string), score (int) |
createPurchase | purchase | coupon (string), currency (string), transaction_id (string), shipping (coalesce_float), tax (coalesce_float), value (coalesce_float) |
createRefund | refund | coupon (string), currency (string), transaction_id (string), shipping (coalesce_float), tax (coalesce_float), value (coalesce_float) |
createRemoveFromCart | remove_from_cart | currency (string), value (coalesce_float) |
createSearch | search | search_term (string) |
createSelectContent | select_content | content_type (string), item_id (string) |
createSelectItem | select_item | item_list_name (string), item_list_id (string) |
createSelectPromotion | select_promotion | promotion_id (string), promotion_name (string), creative_name (string), creative_slot (string) |
createShare | share | content_type (string), item_id (string), method (string) |
createSignUp | sign_up | method (string) |
createSpendVirtualCurrency | spend_virtual_currency | item_name (string), virtual_currency_name (string), value (coalesce_float) |
createTutorialBegin | tutorial_begin | |
createTutorialComplete | tutorial_complete | |
createUnlockAchievement | unlock_achievement | achievement_id (string) |
createViewCart | view_cart | currency (string), value (coalesce_float) |
createViewItem | view_item | currency (string), value (coalesce_float) |
createViewItemList | view_item_list | item_list_name (string), item_list_id (string) |
createViewPromotion | view_promotion | promotion_id (string), promotion_name (string), creative_name (string), creative_slot (string) |
createClick | click | link_classes (string), link_domain (string), link_id (string), link_url (string), outbound (string) |
createFileDownload | file_download | file_extension (string), file_name (string), link_classes (string), link_domain (string), link_id (string), link_text (string), link_url (string) |
createFormStart | form_start | form_id (string), form_name (string), form_destination (string) |
createFormSubmit | form_submit | form_id (string), form_name (string), form_destination (string), form_submit_text (string) |
createScroll | scroll | engagement_time_msec (int) |
createVideoComplete | video_complete | video_current_time (int), video_duration (int), video_percent (int), video_provider (string), video_title (string), video_url (string), visible (string) |
createVideoProgress | video_progress | video_current_time (int), video_duration (int), video_percent (int), video_provider (string), video_title (string), video_url (string), visible (string) |
createVideoStart | video_start | video_current_time (int), video_duration (int), video_percent (int), video_provider (string), video_title (string), video_url (string), visible (string) |
createViewSearchResults | view_search_results | search_term (string) |
createPageView | page_view | page_title (string), page_referrer (string) |
#
Create and reuse custom events between projectsAlso EventFactory provides createEvent
method to create custom events. You could use this method by providing config with these properties:
eventName
- event nameeventParameters
- list of event parametersuserProperties
- list of user propertiesqueryParameters
- list of query parameters
For example:
const sessionStart = ef.createEvent({ eventName: "session_start", columns: [ { name: "privacy_info.analytics_storage", columnName: "analytics_storage" }, ], eventParams: [{ name: "page_referrer", type: "string" }],});sessionStart.publish();
Read more about createEvent method
EventFactory also provides createEvents method to create a few custom events at once based on the list of event configs. So if you have standard setup between projects you could save the event configs in the separate file in includes
folder and reuse it to create all needed events. For example, you could save config like this:
const config = [ { eventName: "session_start", columns: [ { name: "privacy_info.analytics_storage", columnName: "analytics_storage", }, ], eventParams: [{ name: "page_referrer", type: "string" }], }, { eventName: "profit_event", columns: [ { name: "privacy_info.analytics_storage", columnName: "analytics_storage", }, ], eventParams: [{ name: "profit_description", type: "string" }], },];module.exports = { config };
And reuse it like this:
// Import packageconst ga4 = require("dataform-ga4-sessions");// Create event factory objectlet ef = new ga4.EventFactory(config);// Create all eventslet events = ef.createEvents(project_events.configs);// Publish eventsevents.forEach((event) => event.publish());
#
Change target schema for all eventsTo set target schema for all events you could use targetSchema
property. For example:
// Define EventFactoryconst ef = new ga4.EventFactory(eventConfig);// Set target schema for all events created by factoryef.targetSchema = "my_schema";let events = ef.createEcommerceEvents();events.forEach((event) => { // But you could overwrite target schema for particular event if (event.target.tableName === "purchase") { event.target.schema = " my_schema_2"; } event.publish();});