@dotcms/analytics
Version:
Official JavaScript library for Content Analytics with DotCMS.
58 lines (57 loc) • 1.9 kB
TypeScript
import { DotCMSAnalytics, DotCMSAnalyticsConfig } from '../../core/shared/models';
/**
* React hook for tracking user interactions and page views in your DotCMS application.
*
* Use this hook to add analytics tracking to your React components. It automatically
* handles user sessions, device information, and UTM campaign parameters.
*
* **Important:** Tracking is automatically disabled when editing content in DotCMS to avoid
* polluting your analytics data with editor activity.
*
* @example
* Basic usage - Track custom events
* ```tsx
* function ProductCard({ title, price }) {
* const { track } = useContentAnalytics({
* server: 'https://demo.dotcms.com',
* siteAuth: 'my-site-auth',
* debug: false
* });
*
* const handleAddToCart = () => {
* track('add-to-cart', {
* product: title,
* price: price
* });
* };
*
* return <button onClick={handleAddToCart}>Add to Cart</button>;
* }
* ```
*
* @example
* Track page views manually
* ```tsx
* function ArticlePage({ article }) {
* const { pageView } = useContentAnalytics({
* server: 'https://demo.dotcms.com',
* siteKey: 'your-site-key'
* });
*
* useEffect(() => {
* pageView({
* category: article.category,
* author: article.author
* });
* }, [article.id]);
* }
* ```
*
* @param config - Configuration object with server URL and site key
* @param config.server - The URL of your DotCMS Analytics server
* @param config.siteKey - Your unique site key for authentication
* @param config.debug - Optional. Set to true to see analytics events in the console
* @returns Object with `track()` and `pageView()` methods for analytics tracking
* @throws {Error} If the configuration is invalid (missing server or siteKey)
*/
export declare const useContentAnalytics: (config: DotCMSAnalyticsConfig) => DotCMSAnalytics;