Methods for customer management

JS API methods are used for client-side management of customers currently logged in to the store. Get customer details like email, group ID, and shipping address, set their tracking consent, and create custom sign out flows.

Enable JS API on the storefront

The methods listed below require JS API to be loaded on the page. Read more on how to enable JS API.

Ecwid.Customer.get(customer)

This method is used to receive all data on the customer currently logged in to the store.

Request example:

Ecwid.Customer.get(function(customer) { 
  console.log(customer.email);
});

// prints
// [email protected]

Fields available in the returned customer object:

NameTypeDescription
billingPersonobject{Person}Customer details including address and contact details (name and phone).
emailStringCustomer's email.
idNumberCustomer ID.
membershipobject{CustomerGroup}Customer group details. Available only if a customer belongs to a customer group.
ownerIdnumberEcwid Store ID.
registeredUNIX TimestampCustomer's registration date.
shippingAddressesarray{ShippingAddress}Customer’s address book.

Person

NameTypeDescription
namestringCustomer's name.
companyNamestring, optionalCompany name of a customer, if available.
streetstring, optionalShipping/billing address. Street.
citystring, optionalShipping/billing address. City.
countryNamestring, optionalShipping/billing address. Country name.
countryCodestring, optionalShipping/billing address. Country code in ISO 3166-2.
postalCodestring, optionalShipping/billing address. ZIP code.
stateOrProvinceCodestring, optionalShipping/billing address. State code ISO 3166-2.
phonestring, optionalCustomer's phone number, if available.

CustomerGroup

NameTypeDescription
idnumberCustomer group ID.
namestringCustomer group name.
owneridnumberEcwid store ID

ShippingAddress

NameTypeDescription
idintegerSaved shipping address ID.
personobject{Person}Customer's shipping address details.

Ecwid.getTrackingConsent();

This method provides information about customer's consent to be tracked on store pages. Use it to define what cookies your application can collect.

Get customer's cookies consent:

Ecwid.getTrackingConsent();

// {  userResponse: "ACCEPTED", askConsent: true }

Fields available in the returned object:

NameTypeDescription
userResponsestringCustomer's cookie consent. Available values:

- "UNDEFINED": customer hasn't specified their consent yet.
- "ACCEPTED": customer agreed to collect all types of cookies.
- "DECLINED": customer agreed to collect only essential cookies.
-"ANALYTICS_ONLY" - customer agreed to collect essential and analytics cookies only.
-"PERSONALIZATION_ONLY" - customer agreed to collect essential and personalization cookies.
askConsentbooleantrue if store requests customer consent, false otherwise.

Ecwid.setTrackingConsent();

Set customer's cookie consent for the website. Use this method if your website asks for customers' cookie consent before Ecwid.

Available values:

  • "UNDEFINED": customer hasn't specified their consent yet.
  • "ACCEPTED": customer agreed to collect all types of cookies.
  • "DECLINED": customer agreed to collect only essential cookies.
  • "ANALYTICS_ONLY" - customer agreed to collect essential and analytics cookies only.
  • "PERSONALIZATION_ONLY" - customer agreed to collect essential and personalization cookies.

Request example:

Ecwid.setTrackingConsent("ACCEPT");
Ecwid.getTrackingConsent();
//{userResponse: "ACCEPTED", askConsent: <as per settings>}

Ecwid.setTrackingConsent("DECLINE");
Ecwid.getTrackingConsent();
//{userResponse: "DECLINED", askConsent: <as per settings>}

Ecwid.setTrackingConsent("UNRECOGNIZED_VALUE");
Ecwid.getTrackingConsent();
//{userResponse: "UNDEFINED", askConsent: <as per settings>}

Ecwid.setTrackingConsent("ANALYTICS_ONLY");
Ecwid.getTrackingConsent();
//{userResponse: "ANALYTICS_ONLY", askConsent: <as per settings>}

Ecwid.setTrackingConsent("PERSONALIZATION_ONLY");
Ecwid.getTrackingConsent();
//{userResponse: "PERSONALIZATION_ONLY", askConsent: <as per settings>}

Track changes in consent value

Ecwid.OnConsentChanged tracks changes in consent value:

Ecwid.OnConsentChanged.add(function(consent) {
    console.log("consent changed to " + consent);
});
Ecwid.setTrackingConsent("DECLINE");
//consent changed to DECLINED

Define consent value before loading Ecwid store

If you use a custom website, set customers' cookie consent with the following config added before the Ecwid integration code:

ec.config = ec.config || {};
ec.config.tracking = ec.config.tracking || {};
ec.config.tracking.ask_consent = true;
ec.config.tracking.user_response = "DECLINED";

Ecwid.getVisitorLocation();

This method gets the customer's location based on their shipping or billing address, or IP.

Request example:

Ecwid.getVisitorLocation();

// {countryCode: 'US', stateCode: 'NE', source: 'SHIPPING_ADDRESS'}

Available fields

NameTypeDescription
countryCodestringCountry code (null if not found)
stateCodestringState code (null if not found)
sourcestringThe source of the received country code and state code. Possible values: 'SHIPPING_ADDRESS’, ‘BILLING_ADDRESS’, 'IP_ADDRESS’.

Ecwid.Customer.signout(success,error)

This method signs out customer from their account.

Request example:

Ecwid.Customer.signOut(function(success,error) { 
  if (success == true) {
      console.log("Customer signed out");
  } else {
      console.log("Signout failed. Error message: " + error);
  }
});

// prints
// Customer signed out

Fields available in the returned object:

NameTypeDescription
successbooleanIs true if a customer was signed out, false otherwise.
errorstringError message. Has value only if success is false