@shopana/ga
Version:
Type-safe Google Analytics 4 (GA4) tracking library for React and Next.js with ecommerce support, event batching, and SSR compatibility
116 lines • 3.94 kB
JavaScript
import {} from '../core/AnalyticsClient';
import { buildEngagementEvent } from '../domain/engagementTracking';
import { buildLoginEvent, buildSignUpEvent } from '../domain/authTracking';
import { getMeasurementId } from './utils';
import {} from '../types/events';
import {} from '../types/common';
import { CommerceTracker } from './CommerceTracker';
import { PageTracker } from './PageTracker';
import { ContentTracker } from './ContentTracker';
import { VideoTracker } from './VideoTracker';
import { ErrorTracker } from './ErrorTracker';
export class GATracker {
constructor(client, logger) {
this.client = client;
this.logger = logger ?? console;
this.pageTracker = new PageTracker(client);
this.commerceTracker = new CommerceTracker(client, (msg) => this.logger.warn(msg));
this.contentTracker = new ContentTracker(client);
this.videoTracker = new VideoTracker(client);
this.errorTracker = new ErrorTracker(client);
}
pageView(params) {
return this.pageTracker.trackPageView(params);
}
purchase(params) {
return this.commerceTracker.purchase(params);
}
trackPurchase(params) {
this.logger.warn('[GA] trackPurchase is deprecated. Use purchase()');
return this.purchase(params);
}
addToCart(params) {
return this.commerceTracker.addToCart(params);
}
removeFromCart(params) {
return this.commerceTracker.removeFromCart(params);
}
engagement(params) {
const measurementId = getMeasurementId(params, this.client);
return this.client.track(buildEngagementEvent({ ...params, measurementId }));
}
async trackAuth(name, params) {
const measurementId = getMeasurementId(params, this.client);
const fullParams = { ...params, measurementId };
const builder = name === 'login' ? buildLoginEvent : buildSignUpEvent;
return this.client.track(builder(fullParams));
}
trackEvent(name, params, options) {
return this.client.track({
name,
measurementId: this.client.getState().measurementId,
params,
}, options);
}
viewCart(params) {
return this.commerceTracker.viewCart(params);
}
beginCheckout(params) {
return this.commerceTracker.beginCheckout(params);
}
addPaymentInfo(params) {
return this.commerceTracker.addPaymentInfo(params);
}
addShippingInfo(params) {
return this.commerceTracker.addShippingInfo(params);
}
viewItem(params) {
return this.commerceTracker.viewItem(params);
}
viewItemList(params) {
return this.commerceTracker.viewItemList(params);
}
selectItem(params) {
return this.commerceTracker.selectItem(params);
}
viewPromotion(params) {
return this.commerceTracker.viewPromotion(params);
}
selectPromotion(params) {
return this.commerceTracker.selectPromotion(params);
}
addToWishlist(params) {
return this.commerceTracker.addToWishlist(params);
}
generateLead(params) {
return this.commerceTracker.generateLead(params);
}
search(params) {
return this.contentTracker.search(params);
}
share(params) {
return this.contentTracker.share(params);
}
videoStart(params) {
return this.videoTracker.videoStart(params);
}
videoProgress(params) {
return this.videoTracker.videoProgress(params);
}
videoComplete(params) {
return this.videoTracker.videoComplete(params);
}
exception(params) {
return this.errorTracker.exception(params);
}
timingComplete(params) {
return this.errorTracker.timingComplete(params);
}
getAnalyticsClient() {
return this.client;
}
destroy() {
this.commerceTracker.destroy();
}
}
//# sourceMappingURL=GATracker.js.map