rabbit-analytics
v1.2.7
Published
A custom analytics service for Shopify and WooCommerce platforms.
Maintainers
Readme
Analytics Service Library
This library provides an abstract AnalyticsService class and two concrete implementations: ShopifyAnalyticsService and WooCommerceAnalyticsService. It includes a factory (AnalyticsFactory) to instantiate the appropriate analytics service based on the platform.
Installation
Install the package using npm:
npm install rabbit-analyticsUsage
Importing the Library
First, import the necessary classes from the package:
const AnalyticsFactory = require('rabbit-analytics');Creating an Analytics Service
Use the AnalyticsFactory to create an instance of the analytics service for a specific platform:
const analyticsService = AnalyticsFactory.create('Shopify');Supported platforms:
ShopifyWooCommerce
Methods
Each analytics service implements the following methods:
track(event, properties)
Tracks a specific event with optional properties.
const analyticsService = AnalyticsFactory.create('Shopify');
analyticsService.track('Product Viewed', {
productId: '12345',
productName: 'Cool T-Shirt',
category: 'Apparel',
});Parameters:
event(string): The name of the event to track.properties(object): An optional object containing additional event properties.
Example:
// Tracking a purchase event
analyticsService.track('Purchase Made', {
orderId: '98765',
amount: 49.99,
currency: 'USD',
});identify(userId, traits)
Identifies a user with a unique ID and optional traits.
const analyticsService = AnalyticsFactory.create('WooCommerce');
analyticsService.identify('user-123', {
email: '[email protected]',
name: 'John Doe',
subscriptionLevel: 'Premium',
});Parameters:
userId(string): The unique ID of the user.traits(object): An optional object containing user traits.
Example:
// Identifying a user with additional traits
analyticsService.identify('user-456', {
email: '[email protected]',
name: 'Jane Doe',
registeredAt: '2024-01-01',
});page(pageName)
Tracks page views with optional properties.
const analyticsService = AnalyticsFactory.create('Shopify');
analyticsService.page('Homepage');Parameters:
pageName(string): The name of the page being viewed. If not provided, the current document title will be used.
Example:
// Tracking a product page view
analyticsService.page('Product Detail Page');Implementation Details
Abstract AnalyticsService Class
The AnalyticsService class is an abstract base class with the following methods:
track(event, properties): Should be implemented by subclasses to track events.identify(userId, traits): Should be implemented by subclasses to identify users.page(pageName): Should be implemented by subclasses to track page views.
Concrete Implementations
ShopifyAnalyticsService
Implements the AnalyticsService methods for Shopify.
track(event, properties): Logs events with Shopify-specific properties.identify(userId, traits): Logs user identification with Shopify-specific traits.page(pageName): Logs page visits with Shopify-specific properties.
WooCommerceAnalyticsService
Implements the AnalyticsService methods for WooCommerce.
track(event, properties): Logs events with WooCommerce-specific properties.identify(userId, traits): Logs user identification with WooCommerce-specific traits.page(pageName): Logs page visits with WooCommerce-specific properties.
Factory Class
AnalyticsFactory
create(platform): Creates an instance of the analytics service for the specified platform. Supported platforms areShopifyandWooCommerce.
Example:
const shopifyService = AnalyticsFactory.create('Shopify');
const wooCommerceService = AnalyticsFactory.create('WooCommerce');License
This project is licensed under the MIT License - see the LICENSE file for details.
This Markdown content provides a complete and detailed overview of how to use the analytics service library. You can copy this content into a `README.md` file in your project directory.