Skip to main content

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 factory#

To 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 methods#

You could find a list of supported events in the file includes/recommended_events.js or in the table below.

Method NameEvent NameEvent Parameters
createAddPaymentInfoadd_payment_infocoupon (string), currency (string), payment_type (string), value (coalesce_float)
createAddShippingInfoadd_shipping_infocoupon (string), currency (string), shipping_tier (string), value (coalesce_float)
createAddToCartadd_to_cartcurrency (string), value (coalesce_float)
createAddToWishlistadd_to_wishlistcurrency (string), value (coalesce_float)
createBeginCheckoutbegin_checkoutcoupon (string), currency (string), value (coalesce_float)
createEarnVirtualCurrencyearn_virtual_currencyvirtual_currency_name (string), value (coalesce_float)
createGenerateLeadgenerate_leadvalue (coalesce_float), currency (string)
createJoinGroupjoin_groupgroup_id (string)
createLevelEndlevel_endlevel_name (string), success (string)
createLevelStartlevel_startlevel_name (string)
createLevelUplevel_upcharacter (string), level (int)
createLoginloginmethod (string)
createPostScorepost_scorelevel (string), character (string), score (int)
createPurchasepurchasecoupon (string), currency (string), transaction_id (string), shipping (coalesce_float), tax (coalesce_float), value (coalesce_float)
createRefundrefundcoupon (string), currency (string), transaction_id (string), shipping (coalesce_float), tax (coalesce_float), value (coalesce_float)
createRemoveFromCartremove_from_cartcurrency (string), value (coalesce_float)
createSearchsearchsearch_term (string)
createSelectContentselect_contentcontent_type (string), item_id (string)
createSelectItemselect_itemitem_list_name (string), item_list_id (string)
createSelectPromotionselect_promotionpromotion_id (string), promotion_name (string), creative_name (string), creative_slot (string)
createSharesharecontent_type (string), item_id (string), method (string)
createSignUpsign_upmethod (string)
createSpendVirtualCurrencyspend_virtual_currencyitem_name (string), virtual_currency_name (string), value (coalesce_float)
createTutorialBegintutorial_begin
createTutorialCompletetutorial_complete
createUnlockAchievementunlock_achievementachievement_id (string)
createViewCartview_cartcurrency (string), value (coalesce_float)
createViewItemview_itemcurrency (string), value (coalesce_float)
createViewItemListview_item_listitem_list_name (string), item_list_id (string)
createViewPromotionview_promotionpromotion_id (string), promotion_name (string), creative_name (string), creative_slot (string)
createClickclicklink_classes (string), link_domain (string), link_id (string), link_url (string), outbound (string)
createFileDownloadfile_downloadfile_extension (string), file_name (string), link_classes (string), link_domain (string), link_id (string), link_text (string), link_url (string)
createFormStartform_startform_id (string), form_name (string), form_destination (string)
createFormSubmitform_submitform_id (string), form_name (string), form_destination (string), form_submit_text (string)
createScrollscrollengagement_time_msec (int)
createVideoCompletevideo_completevideo_current_time (int), video_duration (int), video_percent (int), video_provider (string), video_title (string), video_url (string), visible (string)
createVideoProgressvideo_progressvideo_current_time (int), video_duration (int), video_percent (int), video_provider (string), video_title (string), video_url (string), visible (string)
createVideoStartvideo_startvideo_current_time (int), video_duration (int), video_percent (int), video_provider (string), video_title (string), video_url (string), visible (string)
createViewSearchResultsview_search_resultssearch_term (string)
createPageViewpage_viewpage_title (string), page_referrer (string)

Create and reuse custom events between projects#

Also EventFactory provides createEvent method to create custom events. You could use this method by providing config with these properties:

  • eventName - event name
  • eventParameters - list of event parameters
  • userProperties - list of user properties
  • queryParameters - 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:

includes/project_events.js
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:

definitions/events.js
// 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 events#

To 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();});