@dotcms/analytics
Version:
Official JavaScript library for Content Analytics with DotCMS.
53 lines (51 loc) • 1.59 kB
TypeScript
import { DotCMSAnalytics } from '../../dotAnalytics/shared/dot-content-analytics.model';
/**
* Custom hook that handles analytics tracking for anonymous users.
*
* @example
* ```tsx
* function Button({ title, urlTitle }) {
* const { track } = useContentAnalytics();
*
* // Track button click with custom properties
* return (
* <button onClick={() => track('btn-click', { title, urlTitle })}>
* See Details →
* </button>
* );
* }
* ```
*
* @example
* ```tsx
* // Session debugging example
* function AnalyticsDebugComponent() {
* const { getAnonymousUserId, getSessionInfo, updateSessionActivity } = useContentAnalytics();
*
* const handleManualActivity = () => {
* updateSessionActivity();
* // Manual activity updated
* };
*
* // Debug session info in development
* const debugInfo = () => {
* if (process.env.NODE_ENV === 'development') {
* console.log('Anonymous ID:', getAnonymousUserId());
* console.log('Session info:', getSessionInfo());
* }
* };
*
* return (
* <div>
* <button onClick={handleManualActivity}>Update Activity</button>
* <button onClick={debugInfo}>Debug Session</button>
* <p>User ID: {getAnonymousUserId()}</p>
* </div>
* );
* }
* ```
*
* @returns {DotCMSAnalytics} - The analytics instance with tracking capabilities for anonymous users
* @throws {Error} - Throws error if used outside of DotContentAnalyticsProvider or if analytics failed to initialize
*/
export declare const useContentAnalytics: () => DotCMSAnalytics;