Envive Analytics SDK
We provide a lightweight SDK for reporting specific events on the frontend to Envive.
Quick Start
Install the SDK via npm/yarn or use the CDN snippet
Get your API key from the Envive team (
ENVIVE_PUBLIC_KEY)Identify users with
identifyUser()OR passidentityOptionswith every tracking callTrack events like checkout completion and add-to-cart actions
Pre-Launch Checklist
Installation
To install @envive-ai/analytics Install as follows:
npm install @envive-ai/analyticsyarn add @envive-ai/analyticsYou can install the SDK using a small snippet of code added to your site via Google Tag Manager (GTM) to load the SDK asynchronously.
Create an HTML tag in GTM to be added to every page where you need to track the events, replacing ENVIVE_PUBLIC_KEY with your public key (provided by us).
<script type="text/javascript">
window._enviveOptions = {
apiKey: 'ENVIVE_PUBLIC_KEY',
}
!function(){"use strict";!function(){var e=["trackCheckoutCompleted","trackCheckoutBegin","trackProductViewed","trackAddToCartClicked","trackCommerceTransactionComplete","trackCustomConversion","identifyUser"];window.envive={_q:[]};for(var n=0;n<e.length;n++)(function(e){window.envive[e]=function(){this._q.push({name:e,args:arguments})}})(e[n]);var t=function(){var e=document.createElement("script");e.src="https://cdn.spiffy.ai/envive-analytics/envive-analytics-production.js",e.async=!0,e.onload=function(){var e=new Envive.EnviveAnalytics({apiKey:window._enviveOptions.apiKey});!function(e){for(;window.envive._q.length>0;){var n=window.envive._q.pop();e[n.name].apply(e,n.args)}}(e),window.envive=e},document.head.appendChild(e)};"interactive"===document.readyState||"complete"===document.readyState?t():window.addEventListener("DOMContentLoaded",t)}()}();
</script>You can install the SDK using a small snippet of code added directly to your site to load the SDK asynchronously.
Put the following on every page where you need to track the events, just before the </head> tag, replacing ENVIVE_PUBLIC_KEY with your public key (provided by us).
<script type="text/javascript">
window._enviveOptions = {
apiKey: 'ENVIVE_PUBLIC_KEY',
}
!function(){"use strict";!function(){const e=["trackCheckoutCompleted","trackCheckoutBegin","trackProductViewed","trackAddToCartClicked","trackCommerceTransactionComplete","trackCustomConversion","identifyUser"];window.envive={_q:[]};for(let n of e)window.envive[n]=function(...e){this._q.push({name:n,args:e})};var n=function(){var e=document.createElement("script");e.src="https://cdn.spiffy.ai/envive-analytics/envive-analytics-production.js",e.async=!0,e.onload=()=>{let e=new Envive.EnviveAnalytics({apiKey:window._enviveOptions.apiKey});!function(e){for(const n of window.envive._q)e[n.name](...n.args)}(e),window.envive=e},document.head.appendChild(e)};"interactive"===document.readyState||"complete"===document.readyState?n():window.addEventListener("DOMContentLoaded",n)}()}();
</script>Usage
NPM/Yarn Usage
import { EnviveAnalytics, EnviveAnalyticsOptions } from '@envive-ai/analytics'
const options: EnviveAnalyticsOptions = {
apiKey: 'ENVIVE_PUBLIC_KEY',
}
const envive = new EnviveAnalytics(options)
envive.identifyUser("some-user-id" /* merchant user id */, "shopify-user-id" /* id-type */)
envive.trackAddToCart({id: "1", quantity: 1, price: 19.99, currencyCode: "usd"});CDN Usage
<script>
window._enviveOptions = {
apiKey: 'ENVIVE_PUBLIC_KEY',
}
//...snippet
</script>
<script>
envive.identifyUser("shopify_customer_123", "shopify-user-id")
envive.trackAddToCart({id: "1", quantity: 1, price: 19.99, currencyCode: "usd"});
</script>User Identification
Choose one approach (required):
Call
identifyUser()once when the user is recognized and before calling any tracking methods
OR
Pass
identityOptionswith every tracking call
⚠️ Critical: If users are not properly identified, events cannot be attributed and analytics will not work.
User ID Guidelines
Requirements (for both methods):
Use a stable, persistent ID from your platform (never random UUIDs)
Always include
idTypeto describe where the ID came from. See idType for more information.
Correct ID sources:
✅ Shopify clientId
✅ Internal platform user ID
✅ BigCommerce SHOP_SESSION_TOKEN
✅ Magento customer_id (if configured to persist long-term)
Incorrect ID sources:
❌ Random UUIDs
❌ Session IDs
❌ Cart IDs
❌ Auto-generated values
idType
idType is a descriptive label for the user ID you send to Envive. It is highly recommended to provide an idType value to maintain unique ID namespaces.
Set idType to a kebab-case string that describes where the ID came from, such as:
shopify-client-idmagento-user-idcrm-user-id
Method 1: identifyUser() (Recommended)
Call once before any tracking calls. The id that you choose must not change for the lifetime of the user.
identifyUser(merchantUserId: string, idType: string): void
// Example
envive.identifyUser("shopify_customer_123", "shopify-user-id")See User ID Guidelines and idType for more information on how to use these parameters.
Method 2: identityOptions Parameter
If you are not using identifyUser(), include this object with every tracking call:
type IdentityOptions = {
merchantUserId: string;
idType: string;
}See User ID Guidelines and idType for more information on how to use these parameters.
Using identifyUser() Without Shopify
If your platform is not on Shopify, you must pass the stable user ID that your platform already uses.
Examples:
CRM user ID
Internal platform account ID
Authentication system user ID
Do not use session IDs, cart IDs, browser-generated IDs, or random UUIDs.
See User ID Guidelines and idType for more information on how to use these parameters.
Event Tracking Methods
Note: If not using
identifyUser(), includeidentityOptionswith every tracking call.
trackCheckoutComplete
When a user completes a purchase, it is typically triggered on the thank-you page.
Equivalent to Shopify checkout_completed event.
type CheckoutEventProperties = {
total: number;
currencyCode: string;
products: Array<{
id: string;
quantity: number;
price: number;
[x: string]: any;
}>;
[x: string]: any; // Additional optional properties
};
type IdentityOptions = {
merchantUserId: string;
idType: string;
};
// Required EventProperties
const eventProperties: CheckoutEventProperties = {
"total": 100,
"currencyCode": "usd",
"products": [
{
"id": "123",
"price": 100,
"quantity": 1
}
]
}
// Optional Property
const identity: IdentityOptions | undefined = {
merchantUserId: "some-shopify-client-id-string-here",
idType: "shopify-client-id",
}
trackCheckoutComplete(eventProperties, identity): voidtrackAddToCartClicked
When a user adds a product to their cart.
Equivalent to the Shopify product_added_to_cart event.
type AddToCartEventProperties = {
id: string;
quantity: number;
price: number;
currencyCode: string;
[x: string]: any; // Additional optional properties
};
// Required EventProperties
const eventProperties: AddToCartEventProperties = {
"id": "123",
"price": 100,
"currencyCode": "usd",
"quantity": 1
}
// Optional Property
const identity: IdentityOptions | undefined = {
merchantUserId: "some-magento-id-string-here",
idType: "magento-user-id"
}
trackAddToCartClicked(eventProperties, identity): void trackCustomConversion
Used to track a non-standard conversion defined by the merchant.
type CustomConversionEventProperties = {
revenue: number;
currencyCode: string;
[x: string]: any; // Additional optional properties
};
// Required EventProperties
const eventProperties: CustomConversionEventProperties = {
"currencyCode": "usd",
"revenue": 10
}
// Optional Property
const identity: IdentityOptions | undefined = {
merchantUserId: "some-magento-id-string-here",
idType: "magento-user-id"
}
trackCustomConversion(eventProperties, identity): voidtrackPageViewed
// Required EventProperties
const properties: Record<string, any> = {
"productId": "some-product-id"
}
// Optional Property
const identity: IdentityOptions | undefined = {
merchantUserId: "some-sales-force-commerce-user-id-string-here",
idType: "sales-force-commerce-user-id"
}
trackPageViewed(properties, identity): voidCommon Mistakes
Most frequent integration issues:
Missing
identifyUser()- Events can't be linked across sessionsRandom UUIDs - Creates new "users" every page load
Late identification - Events before
identifyUser()won't be attributedUnstable IDs - Don't use session/cart IDs or localStorage values
Platform confusion - Use your platform's actual user ID, not Shopify-specific IDs on other platforms
Server Side Usage
Note: When using the analytics SDK server-side, an enviveUserId is neither present nor required in any SDK API call methods.
However, it is critical that:
IdentifyUseris called somewhere in the user's client-side journey (on your website)All
Track__Methods are supplied with theidentityinformation
To Summarize:
IdenfiyUser or Track____ method call with identity payload included
Website
IdenfiyUser or Track with identity included must be called at least once in the user's client-side journey
Track____ methods only. No IdentifyUser calls necessary.
Server Side (eg: Lambda functions, Azure Functions, ECS Tasks etc)
Call as needed to instrument events such as "Checkout Begin", "Checkout Complete" or "Commerce Transaction Complete" and always include the identity parameter
Example Implementations
Check out the example minimal implementations of using this SDK.
Shopify Hydrogen
React
Last updated