keystone-design-bootstrap 1.0.99 → 1.0.100

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keystone-design-bootstrap",
3
- "version": "1.0.99",
3
+ "version": "1.0.100",
4
4
  "description": "Keystone Design Bootstrap - Sections, Elements, and Theme System for customer websites",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -2,7 +2,7 @@
2
2
 
3
3
  import posthog from 'posthog-js';
4
4
  import { PostHogProvider as PHProvider } from 'posthog-js/react';
5
- import { useEffect, Suspense } from 'react';
5
+ import { useEffect, Suspense, useRef } from 'react';
6
6
  import { usePathname, useSearchParams } from 'next/navigation';
7
7
  import { error as logError, initializePostHogLogging } from './logging';
8
8
 
@@ -74,12 +74,28 @@ function resolvePageInfo(pathname: string): PageInfo {
74
74
  function PostHogPageviewTracker() {
75
75
  const pathname = usePathname();
76
76
  const searchParams = useSearchParams();
77
+ const latestUrlRef = useRef<string | null>(null);
77
78
 
78
79
  useEffect(() => {
79
80
  if (!pathname) return;
80
81
 
81
82
  const search = searchParams?.toString();
82
83
  const url = window.location.origin + pathname + (search ? `?${search}` : '');
84
+ const previousUrl = latestUrlRef.current;
85
+ if (previousUrl && previousUrl !== url) {
86
+ const previousPath = (() => {
87
+ try {
88
+ return new URL(previousUrl).pathname;
89
+ } catch {
90
+ return pathname;
91
+ }
92
+ })();
93
+ posthog.capture('$pageleave', {
94
+ $current_url: previousUrl,
95
+ page_path: previousPath,
96
+ });
97
+ }
98
+ latestUrlRef.current = url;
83
99
  const { page_name, page_slug } = resolvePageInfo(pathname);
84
100
 
85
101
  posthog.capture('$pageview', {
@@ -90,6 +106,29 @@ function PostHogPageviewTracker() {
90
106
  });
91
107
  }, [pathname, searchParams]);
92
108
 
109
+ useEffect(() => {
110
+ const onPageHide = () => {
111
+ const currentUrl = latestUrlRef.current;
112
+ if (!currentUrl) return;
113
+ const currentPath = (() => {
114
+ try {
115
+ return new URL(currentUrl).pathname;
116
+ } catch {
117
+ return window.location.pathname;
118
+ }
119
+ })();
120
+ posthog.capture('$pageleave', {
121
+ $current_url: currentUrl,
122
+ page_path: currentPath,
123
+ });
124
+ };
125
+
126
+ window.addEventListener('pagehide', onPageHide);
127
+ return () => {
128
+ window.removeEventListener('pagehide', onPageHide);
129
+ };
130
+ }, []);
131
+
93
132
  return null;
94
133
  }
95
134