The Datahappy JavaScript SDK, also known as the Pixel, provides a simple API for tracking events and managing user identity and consent from your website’s frontend.
The datahappy.init() method is asynchronous. It kicks off the process of fetching your configuration and getting the SDK ready. All subsequent API calls (like page or track) are safely queued and will be processed in order once initialisation is complete.You can wait for the SDK to be fully loaded using await datahappy.init() or by using the global datahappy.ready.then() handler. This is only necessary if you need to access SDK properties, like the datahappy.Event constants, immediately after initialisation.

Methods

init()

Initialises the SDK. This should be the first method called to ensure the SDK is configured correctly. The installation snippet calls this for you.
datahappy.init(projectId, [options])
projectId
string
required
Your unique project identifier, provided during onboarding.
options
Object
An object containing initial identity or configuration overrides.

page()

Tracks a page view. The installation snippet calls this once on initial page load. For Single-Page Applications (SPAs), you must call this method again after each route change.
datahappy.page([params])
params
Object
An object containing a properties key, which can be used to send custom properties with the page view and to override any automatically captured page data (e.g. page_title, page_url).

track()

Tracks a custom event.
datahappy.track(event)
event
Object
required
An object containing the event details.

updateIdentity()

Updates the current visitor’s identity profile.
datahappy.updateIdentity(identity)
identity
Object
required
An object containing the identity fields to update.

getIdentity()

Returns the current identity stored in the SDK (anonymousId, userId, traits).
const identity = datahappy.getIdentity()

updateConsent()

Updates the visitor’s consent preferences. If you are using a custom consent solution, you must call this method on every page load to ensure the SDK is initialised with the correct consent state.
datahappy.updateConsent(consent)
An object where keys are consent categories (e.g. advertising, analytics) and values are booleans.

getConsent()

Returns the current consent state stored in the SDK.
const consent = datahappy.getConsent()

reset()

Resets the current user’s identity. This is an asynchronous operation that clears the userId and traits and generates a new anonymousId. It is useful for “log out” functionality.
await datahappy.reset()

Data Reference: The Datahappy Schema

This is a reference for the standard events, properties and traits that Datahappy recognises natively. Using this schema where possible helps us automatically map your data to downstream integrations.

Standard Events

We provide a set of standard event names via the datahappy.Event object. It’s recommended to use these where applicable.
Object KeyEvent Name (string)Description
LEAD_FORM_COMPLETEDlead_form_completedA user submitted a form to express interest.
SIGNUP_COMPLETEDsignup_completedA user created an account.
USER_LOGGED_INuser_logged_inA user logged in to their account.
SEARCH_PERFORMEDsearch_performedA user performed a search.
ITEM_LIST_VIEWEDitem_list_viewedA user viewed a list of items (e.g. a product category page).
ITEM_VIEWEDitem_viewedA user viewed a specific item.
ITEM_ADDEDitem_addedA user added an item to their cart.
CHECKOUT_STARTEDcheckout_startedA user started the checkout process.
PAYMENT_INFO_ADDEDpayment_info_addedA user submitted their payment information.
PURCHASE_COMPLETEDpurchase_completedA user completed a purchase.
TRIAL_STARTEDtrial_startedA user started a free trial.
SUBSCRIPTION_STARTEDsubscription_startedA user started a paid subscription.
SUBSCRIPTION_CANCELLEDsubscription_cancelledA user cancelled their subscription.

Event Properties

NameTypeDescription
valueFloatA monetary value associated with an event e.g. revenue
currencyStringThe currency of the transaction e.g. “USD”
transaction_idStringA unique ID for the transaction.
subscription_idStringA unique ID for the subscription. Only set here if you’re not passing an items array.
couponStringOptional. Discount code that was used.
shippingFloatOptional. Cost of shipping.
taxFloatOptional. Value added tax.
delivery_categoryStringOptional. e.g. “home_delivery”.
itemsArrayA list of items related to the event. See below for item properties.

Item Properties

NameTypeDescription
item_idStringUnique identifier for the item being purchased.
item_nameStringHuman-readable name of the item.
is_subscriptionBooleanIndicates whether the item is a subscription or not.
subscription_idStringUnique identifier for the subscription.
affiliationStringStore or business affiliation from where the item originates.
couponStringCode for any discount coupon applied.
discountFloatMonetary discount applied to the item’s price.
indexIntegerPosition index of the item, often used in a list or cart.
item_brandStringBrand of the item.
item_categoryStringCategory to which the item belongs.
item_list_idStringIdentifier for the list in which the item appears.
item_list_nameStringHuman-readable name of the list in which the item appears.
item_variantStringSpecifies the variant of the item (e.g. colour, size).
location_idStringIdentifier for the store location from where the item was purchased.
priceFloatPrice of a single unit of the item.
quantityIntegerNumber of units of the item being purchased.

User Traits

NameTypeDescription
firstNameStringFirst name of a user.
lastNameStringLast name of a user.
nameStringFull name of a user. If you only pass a first and last name, Datahappy automatically fills in the full name for you.
emailStringEmail address of a user.
phoneStringPhone number of a user.
titleStringTitle of a user, usually related to their position at a specific company. Example: “VP of Engineering”.
companyNameStringCompany the user represents.
companyObjectCompany the user represents, optionally containing: name (String), id (String or Number), industry (String), employeeCount (Number) and plan (String).
addressObjectStreet address of a user optionally containing: street, city, region, state, postalCode, country & countryCode (ISO 3166-1 alpha-2 standard).
genderStringGender of a user.
birthdayDateUser’s birthday.
websiteStringWebsite of a user.
messageStringA message from the user e.g. for lead capture.
descriptionStringDescription of the user.
createdAtStringDate the user’s account was first created. datahappy recommends using ISO-8101 date strings. If not set datahappy will set to the date the event is triggered.