Wix events are fired in your site's backend when certain events occur in your site's store. You can write event handlers that react to these events. Event handler functions receive data that corresponds to the event that has occurred. Use event handlers to create custom responses to marketing events.
To add a marketing event handler, add an events.js file to the Backend section of your site if one does not already exist. All event handler functions for your site are defined in this file.
Event handler functions are defined using the following pattern:
export function <wixAppName>_<eventName>(event) { }
For example, an event handler that handles coupon creation in a Wix site looks like this:
export function wixMarketing_onCouponCreated(event) {}
Note: Backend events don't work when previewing your site.
An event that is triggered when a coupon is used.
The onCouponApplied()
event handler runs when an order with an applied coupon is completed, or when an order is manually marked as paid.
Note: Backend events don't work when previewing your site.
function onCouponApplied(event: CouponAppliedEvent): void;
Information about an applied coupon.
// Place this code in the events.js file
// of your site's Backend section.
export function wixMarketing_onCouponApplied(event) {
const appliedCouponId = event.data.coupon._id;
const appliedCouponName = event.data.coupon.name;
const couponAppliedTime = event.metadata.eventTime;
const appliedCouponOrderId = event.data.wixAppOrderId;
}
/* Full event object
*
* {
* "metadata": {
* "id": "99a3d562-1650-40e6-97f5-3918e6ec94aa",
* "entityId": "03a35393-7835-44b0-af53-6019a484b48b",
* "eventTime": "2021-07-17T11:56:03.852350Z",
* "triggeredByAnonymizeRequest": false
* },
* "data": {
* "coupon": {
* "_id": "03a35393-7835-44b0-af53-6019a484b48b",
* "name": "Summer Sale 10% off",
* "code": "SummerSale",
* "startTime": "Wed Jun 30 2021 21:00:00 GMT+0000 (Coordinated Universal Time)",
* "expirationTime": "Tue Aug 31 2021 20:59:59 GMT+0000 (Coordinated Universal Time)",
* "limitPerCustomer": 1,
* "active": true,
* "scope": {
* "namespace": "stores",
* "group": {
* "name": "product",
* "entityId": "5376f9ec-b92e-efa9-e4a1-f4f480aa0d3a"
* }
* },
* "_dateCreated": "Sun Jan 17 2021 11:40:56 GMT+0000 (Coordinated Universal Time)",
* "numberOfUsages": 1,
* "expired": false,
* "displayData": {
* "name": "Indonesian Blend",
* "formattedPrice": "$35.00",
* "mediaItem": "https://static.wixstatic.com/media/nsplsh_316b6449475f3235386255~mv2_d_2977_3951_s_4_2.jpg/v1/fit/w_2977,h_3951,q_90/file.jpg"
* },
* "limitedToOneItem": false,
* "type": "PercentOff",
* "percentOffRate": 10
* },
* "wixAppId": "1380b703-ce81-ff05-f115-39571d94dfcd",
* "wixAppOrderId": "6758b1bc-539f-430c-a0ea-889130ef3351"
* }
* }
*
*/
An event that is triggered when a coupon is created.
The onCouponCreated()
event handler runs when a new coupon is created.
Note: Backend events don't work when previewing your site.
function onCouponCreated(event: CouponEvent): void;
Information about a newly created coupon.
// Place this code in the events.js file
// of your site's Backend section.
export function wixMarketing_onCouponCreated(event) {
const newCouponId = event.entity._id;
const newCouponType = event.entity.type;
const couponValidFrom = event.entity.startTime;
}
/* Full event object
*
* {
* "metadata": {
* "id": "c4ea2c0a-0952-48ad-9b15-39e660448663",
* "entityId": "03a35393-7835-44b0-af53-6019a484b48b",
* "eventTime": "2021-01-17T11:40:56.795047Z",
* "triggeredByAnonymizeRequest": false
* },
* "entity": {
* "_id": "03a35393-7835-44b0-af53-6019a484b48b",
* "name": "Summer Sale 10% off",
* "code": "SummerSale",
* "startTime": "Wed Jun 30 2021 21:00:00 GMT+0000 (Coordinated Universal Time)",
* "expirationTime": "Tue Aug 31 2021 20:59:59 GMT+0000 (Coordinated Universal Time)",
* "limitPerCustomer": 1,
* "active": true,
* "scope": {
* "namespace": "stores"
* },
* "_dateCreated": "Sun Jan 17 2021 11:40:56 GMT+0000 (Coordinated Universal Time)",
* "expired": false,
* "displayData": null,
* "limitedToOneItem": true,
* "type": "PercentOff",
* "percentOffRate": 10
* }
* }
*
*/
An event that is triggered when a coupon is deleted.
The onCouponDeleted()
event handler runs when a coupon is deleted.
Note: Backend events don't work when previewing your site.
function onCouponDeleted(event: CouponDeletedEvent): void;
Information about a deleted coupon.
// Place this code in the events.js file
// of your site's Backend section.
export function wixMarketing_onCouponDeleted(event) {
const deletedCouponId = event.metadata.entityId;
}
/* Full event object
*
* {
* "metadata": {
* "id": "0c279cd0-3db2-4e5c-afb5-9b1346bf3ccc",
* "entityId": "03a35393-7835-44b0-af53-6019a484b48b",
* "eventTime": "2021-01-17T12:01:46.597758Z",
* "triggeredByAnonymizeRequest": false
* }
* }
*
*/