@momentumcms/plugins-analytics 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Page View Collector
3
+ *
4
+ * Express middleware that tracks SSR page view timing and context.
5
+ * Mounted at the application root to intercept page renders.
6
+ */
7
+ import type { Request, Response, NextFunction } from 'express';
8
+ import type { AnalyticsEvent } from '../analytics-event.types';
9
+ import type { PageViewTrackingOptions } from '../analytics-config.types';
10
+ /**
11
+ * Callback type for page view event emission.
12
+ */
13
+ export type PageViewEmitter = (event: AnalyticsEvent) => void;
14
+ /**
15
+ * Check whether a user-agent string belongs to a known bot.
16
+ */
17
+ export declare function isBot(ua: string | undefined): boolean;
18
+ /**
19
+ * Check whether a request path should be excluded from page view tracking.
20
+ */
21
+ export declare function shouldExcludePath(path: string, excludeExtensions: ReadonlySet<string>, excludePaths: readonly string[]): boolean;
22
+ /**
23
+ * Creates Express middleware that tracks SSR page view metrics.
24
+ *
25
+ * @param emitter - Callback to emit analytics events
26
+ * @param options - Page view tracking options
27
+ * @returns Express middleware function
28
+ */
29
+ export declare function createPageViewCollectorMiddleware(emitter: PageViewEmitter, options?: PageViewTrackingOptions): (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,21 @@
1
+ import type { EnvironmentProviders } from '@angular/core';
2
+
3
+ /** Configuration for the client-side page view tracker. */
4
+ export interface PageViewTrackingConfig {
5
+ /** Map of collection slug -> URL pattern (e.g. `{ articles: '/articles/:slug' }`) */
6
+ contentRoutes?: Record<string, string>;
7
+ /** Ingest endpoint URL. @default '/api/analytics/collect' */
8
+ endpoint?: string;
9
+ /** Additional path prefixes to exclude from tracking. */
10
+ excludePrefixes?: readonly string[];
11
+ }
12
+
13
+ /**
14
+ * Provide client-side page view tracking for an Angular application.
15
+ *
16
+ * Listens to Angular Router NavigationEnd events and sends page_view analytics
17
+ * events to the ingest endpoint for SPA navigations.
18
+ */
19
+ export declare function providePageViewTracking(
20
+ config: PageViewTrackingConfig,
21
+ ): EnvironmentProviders;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Page View Tracker — Pure Logic
3
+ *
4
+ * Testable functions used by the Angular PageViewTrackerService.
5
+ * No Angular dependencies — safe for Node.js and Vitest environments.
6
+ */
7
+ import { type CompiledContentRoute } from './utils/content-route-matcher';
8
+ /** Default path prefixes excluded from client-side page view tracking. */
9
+ export declare const DEFAULT_EXCLUDE_PREFIXES: readonly string[];
10
+ /**
11
+ * Determine whether a navigation should be tracked.
12
+ *
13
+ * @param url - The navigated URL (may include query string / fragment)
14
+ * @param isFirstNavigation - True for the initial route (already tracked by SSR)
15
+ * @param excludePrefixes - Path prefixes to skip
16
+ */
17
+ export declare function shouldTrackNavigation(url: string, isFirstNavigation: boolean, excludePrefixes: readonly string[]): boolean;
18
+ /**
19
+ * Build a page_view client event payload from a URL.
20
+ *
21
+ * @param url - The navigated URL (may include query / fragment)
22
+ * @param compiledRoutes - Pre-compiled content routes for collection matching
23
+ */
24
+ export declare function buildPageViewEvent(url: string, compiledRoutes: readonly CompiledContentRoute[] | undefined): {
25
+ name: string;
26
+ category: string;
27
+ properties: Record<string, unknown>;
28
+ };
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Content Route Matcher
3
+ *
4
+ * Compiles Express-style :param patterns into matchers for
5
+ * attributing page views to CMS collections.
6
+ */
7
+ /** A compiled content route matcher. */
8
+ export interface CompiledContentRoute {
9
+ /** Collection slug this route maps to */
10
+ collection: string;
11
+ /** Original pattern string */
12
+ pattern: string;
13
+ /** Compiled regex */
14
+ regex: RegExp;
15
+ /** Ordered parameter names extracted from the pattern */
16
+ paramNames: readonly string[];
17
+ /** Number of static (non-param) segments — used for priority sorting */
18
+ staticSegments: number;
19
+ }
20
+ /** Result of matching a URL against content routes. */
21
+ export interface ContentRouteMatch {
22
+ /** Collection slug */
23
+ collection: string;
24
+ /** Extracted route parameters */
25
+ params: Record<string, string>;
26
+ }
27
+ /**
28
+ * Compile a single route pattern into a regex matcher.
29
+ *
30
+ * Supports Express-style `:paramName` segments.
31
+ * e.g. `/articles/:slug` compiles to `/^\/articles\/([^/]+)\/?$/`
32
+ */
33
+ export declare function compileContentRoute(collection: string, pattern: string): CompiledContentRoute;
34
+ /**
35
+ * Compile all content routes, sorted by specificity (most static segments first).
36
+ */
37
+ export declare function compileContentRoutes(routes: Record<string, string>): CompiledContentRoute[];
38
+ /**
39
+ * Match a URL path against compiled content routes.
40
+ * Returns the first (highest-priority) match, or undefined.
41
+ */
42
+ export declare function matchContentRoute(path: string, routes: readonly CompiledContentRoute[]): ContentRouteMatch | undefined;