Wix CRM events are fired in your site's backend when certain events related to site contacts occur in your site's backend. 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 contact related events.
Note: Backend events don't work when previewing your site.
To add a CRM 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:
For example, an event handler that handles contact updates looks like this:
Triggered when a contact is created.
function wixCrm_onContactCreated(event: ContactCreated): void;
Information about the contact that was created and metadata for the event.
// Place this code in the events.js file
// of your site's Backend section.
// Add the file if it doesn't exist.
export function wixCrm_onContactCreated(event) {
const eventId = event.metadata.id;
const entityId = event.entity._id;
const lastName = event.entity.info.name.last;
}
/* Full event object:
* {
* "metadata": {
* "id": "7bdca23b-a89c-48a1-be76-6dcf46fc4edb",
* "entityId": "beeb8dc1-93d6-49f3-9389-18a9997f554f",
* "eventTime": "2024-01-10T10:30:27.479909037Z",
* "triggeredByAnonymizeRequest": false
* },
* "entity": {
* "_id": "beeb8dc1-93d6-49f3-9389-18a9997f554f",
* "revision": 1,
* "source": {
* "sourceType": "ADMIN"
* },
* "_createdDate": "2024-01-10T10:30:27.447Z",
* "_updatedDate": "2024-01-10T10:30:27.447Z",
* "primaryInfo": {
* "email": "fforte@example.com",
* "phone": "646-446-4789"
* },
* "lastActivity": {
* "activityDate": "2024-01-10T10:30:27.446Z",
* "activityType": "CONTACT_CREATED"
* },
* "info": {
* "name": {
* "first": "Forrest",
* "last": "Forte"
* },
* "emails": [
* {
* "tag": "UNTAGGED",
* "email": "fforte@example.com",
* "primary": true,
* "_id": "c27570d4-fc36-4391-aeb4-216b65ebb5e9"
* },
* {
* "tag": "UNTAGGED",
* "email": "forrest.forte@example.com",
* "primary": false,
* "_id": "5377214c-d6da-4212-aa8b-2735f0ad71e7"
* }
* ],
* "phones": [
* {
* "tag": "MOBILE",
* "countryCode": "US",
* "phone": "646-446-4789",
* "e164Phone": "+16464464789",
* "formattedPhone": "+1 646-446-4789",
* "primary": true,
* "_id": "412ccdb6-b5de-441f-ad9f-a32637357c98"
* },
* {
* "tag": "WORK",
* "countryCode": "US",
* "phone": "718-250-2680",
* "e164Phone": "+17182502680",
* "formattedPhone": "+1 718-250-2680",
* "primary": false,
* "_id": "90148d5d-4940-4761-9a30-6c677c92fa3e"
* }
* ],
* "addresses": [
* {
* "_id": "19d2ab15-0c8e-4f2c-894a-eddaba1088cf",
* "tag": "BILLING",
* "address": {
* "formatted": "82 Howard Ave\nNew York, New York 11598\nUnited States",
* "subdivision": "US-NY",
* "city": "New York",
* "country": "US",
* "postalCode": "11598",
* "addressLine1": "82 Howard Ave"
* }
* }
* ],
* "labelKeys": [
* "contacts.customers"
* ],
* "extendedFields": {
* "custom.age": 43,
* "invoices.vatId": "",
* "contacts.displayByFirstName": "Forrest Forte",
* "contacts.displayByLastName": "Forte Forrest"
* }
* }
* }
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Triggered when a contact is deleted.
If a contact is deleted as part of a merge, the originatedFrom
property is sent as merge
.
Otherwise, originatedFrom
isn't returned.
function wixCrm_onContactDeleted(event: ContactDeleted): void;
Metadata for the event.
// Place this code in the events.js file
// of your site's Backend section.
// Add the file if it doesn't exist.
export function wixCrm_onContactDeleted(event) {
const contactId = event.metadata.entityId;
console.log("Contact deleted", event);
}
/* Full event object:
* {
* "metadata": {
* "id": "e252a78d-bd95-4aa2-b65f-91730da186fa",
* "entityId": "custom.customer-group-3",
* "eventTime": "2024-01-11T12:47:31.862945419Z",
* "triggeredByAnonymizeRequest": false
* }
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Triggered when one or more source contacts are merged into a target contact.
function wixCrm_onContactMerged(event: ContactMergedEvent): void;
Information about the source and target contacts, and metadata for the event.
// Place this code in the events.js file
// of your site's Backend section.
// Add the file if it doesn't exist.
export function wixCrm_onContactMerged(event) {
const eventId = event.metadata.id;
const sourceContactIds = event.data.sourceContactIds;
const firstSourceContactId = event.data.sourceContactIds[0];
const targetContactId = event.data.targetContact._id;
const targetContactName = `${event.data.targetContact.info.name.first} ${event.data.targetContact.info.name.last}`;
console.log("Target contact updated by merge", targetContactId);
console.log("Source contacts deleted by merge", sourceContactIds);
}
/* Full event object:
* {
* "metadata": {
* "id": "c6f09a37-8c03-469e-bfa8-9a40939cac66",
* "entityId": "87227cf7-4ed5-47c3-8261-795d16b6dc9f",
* "eventTime": "2024-01-14T10:26:38.848Z",
* "triggeredByAnonymizeRequest": false
* },
* "data": {
* "sourceContactIds": [
* "2ca312b3-e850-465a-9991-c59c9c140919"
* ],
* "targetContactId": "527676cc-2f20-4318-a76f-07854e02be2c",
* "targetContact": {
* "revision": 3,
* "source": {
* "sourceType": "ADMIN"
* },
* "lastActivity": {
* "activityDate": "2024-01-14T10:26:38.848Z",
* "activityType": "CONTACT_MERGED"
* },
* "primaryInfo": {
* "email": "j.jackson@example.com",
* "phone": "646-458-4589"
* },
* "info": {
* "name": {
* "first": "Jeremy",
* "last": "Jackson"
* },
* "emails": {
* "items": [
* {
* "tag": "UNTAGGED",
* "email": "j.jackson@example.com",
* "primary": true,
* "_id": "dec280b7-a8dd-45bc-a7de-0da9166caf63"
* }
* ]
* },
* "phones": {
* "items": [
* {
* "tag": "MOBILE",
* "countryCode": "US",
* "phone": "646-458-4589",
* "e164Phone": "+16464584589",
* "primary": true,
* "_id": "c40ef68a-2cc9-4ab0-adb4-97be64c57878"
* },
* {
* "tag": "MOBILE",
* "countryCode": "IL",
* "phone": "55-334-3434",
* "e164Phone": "+972553343434",
* "primary": false,
* "_id": "caa48199-d40d-4c2e-9eec-ae9bdf8e4f35"
* }
* ]
* },
* "addresses": {
* "items": [
* {
* "tag": "HOME",
* "address": {
* "formatted": "Israel",
* "country":"IL"
* },
* "_id": "6a2fd038-fa7a-486d-9230-e48bcea824bc"
* }
* ]
* },
* "labelKeys": {
* "items": [
* "custom.anonymous"
* ]
* },
* "extendedFields": {
* "items": {
* "contacts.displayByLastName": "Jackson Jeremy",
* "emailSubscriptions.effectiveEmail": "j.jackson@example.com",
* "custom.age": 37,
* "contacts.displayByFirstName": "Jeremy Jackson"
* }
* }
* },
* "_id": "527676cc-2f20-4318-a76f-07854e02be2c",
* "_createdDate": "2024-01-14T10:22:43.952Z",
* "_updatedDate": "2024-01-14T10:26:38.848Z"
* }
* }
* }
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.