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)
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:
Name | Type | Description |
---|---|---|
billingPerson | object{Person} | Customer details including address and contact details (name and phone). |
String | Customer's email. | |
id | Number | Customer ID. |
membership | object{CustomerGroup} | Customer group details. Available only if a customer belongs to a customer group. |
ownerId | number | Ecwid Store ID. |
registered | UNIX Timestamp | Customer's registration date. |
shippingAddresses | array{ShippingAddress} | Customer’s address book. |
Person
Person
Name | Type | Description |
---|---|---|
name | string | Customer's name. |
companyName | string, optional | Company name of a customer, if available. |
street | string, optional | Shipping/billing address. Street. |
city | string, optional | Shipping/billing address. City. |
countryName | string, optional | Shipping/billing address. Country name. |
countryCode | string, optional | Shipping/billing address. Country code in ISO 3166-2. |
postalCode | string, optional | Shipping/billing address. ZIP code. |
stateOrProvinceCode | string, optional | Shipping/billing address. State code ISO 3166-2. |
phone | string, optional | Customer's phone number, if available. |
CustomerGroup
CustomerGroup
Name | Type | Description |
---|---|---|
id | number | Customer group ID. |
name | string | Customer group name. |
ownerid | number | Ecwid store ID |
ShippingAddress
ShippingAddress
Name | Type | Description |
---|---|---|
id | integer | Saved shipping address ID. |
person | object{Person} | Customer's shipping address details. |
Ecwid.getTrackingConsent();
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:
Name | Type | Description |
---|---|---|
userResponse | string | Customer'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. |
askConsent | boolean | true if store requests customer consent, false otherwise. |
Ecwid.setTrackingConsent();
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();
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
Name | Type | Description |
---|---|---|
countryCode | string | Country code (null if not found) |
stateCode | string | State code (null if not found) |
source | string | The source of the received country code and state code. Possible values: 'SHIPPING_ADDRESS’, ‘BILLING_ADDRESS’, 'IP_ADDRESS’. |
Ecwid.Customer.signout(success,error)
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:
Name | Type | Description |
---|---|---|
success | boolean | Is true if a customer was signed out, false otherwise. |
error | string | Error message. Has value only if success is false |