Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add segment.
Signed-off-by: bgravenorst <[email protected]>
  • Loading branch information
bgravenorst committed Aug 17, 2025
commit bb6b7f915da2291cc65392e3117b2c1849550145
2 changes: 1 addition & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ const config = {
[
'@docusaurus/plugin-google-gtag',
{
trackingID: 'G-E8PSQ0E1L7',
trackingID: process.env.GA4_MEASUREMENT_ID || 'G-E8PSQ0E1L7',
anonymizeIP: true,
},
],
Expand Down
6 changes: 3 additions & 3 deletions src/components/PageFeedback/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react'
import { trackFeedbackForGA } from '../../lib/feedbackAnalytics'
import { trackFeedback } from '../../lib/feedbackAnalytics'
import styles from './styles.module.scss'

export default function PageFeedback() {
Expand All @@ -18,7 +18,7 @@ export default function PageFeedback() {
console.log(`🎯 User clicked: ${positive ? 'Yes' : 'No'} (positive: ${positive})`)
setFeedbackGiven(true)
if (positive) {
trackFeedbackForGA({
trackFeedback({
positive: true,
locale: navigator.language,
})
Expand All @@ -35,7 +35,7 @@ export default function PageFeedback() {
...(r === 'other' && otherReason.trim() && { response: otherReason.trim() }),
locale: navigator.language,
}
trackFeedbackForGA(data)
trackFeedback(data)
setSubmitted(true)
}

Expand Down
19 changes: 18 additions & 1 deletion src/lib/feedbackAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
// Enable debug mode in development
if (process.env.NODE_ENV === 'development' && typeof window.gtag === 'function') {
try {
window.gtag('config', 'debug_mode', true)
window.gtag('config', 'G-E8PSQ0E1L7', { debug_mode: true })
console.log('🔍 GA4 Debug mode enabled for development')
} catch (error) {
console.log('⚠️ Could not enable debug mode:', error)
Expand Down Expand Up @@ -119,6 +119,23 @@
console.warn('No tracking method available. Event data:', eventData)
}

/**
* Unified feedback tracking function that sends to both GA4 and Segment
*/
export const trackFeedback = (data: FeedbackData): void => {
// Track to GA4
trackFeedbackForGA(data)

// Track to Segment
import('../lib/segmentAnalytics')
.then(({ trackFeedbackForSegment }) => {
trackFeedbackForSegment(data)

Check failure on line 132 in src/lib/feedbackAnalytics.ts

View workflow job for this annotation

GitHub Actions / Lint Code Base

Argument of type 'FeedbackData' is not assignable to parameter of type '{ positive: any; reason: any; response: any; locale: any; timestamp: any; device_type: any; }'.
})
.catch(error => {
console.warn('Could not load Segment analytics:', error)
})
}

// Type augmentation for window object
declare global {
interface Window {
Expand Down
50 changes: 40 additions & 10 deletions src/lib/segmentAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,50 @@ export const trackClickForSegment = ({
...(responseStatus && { response_status: responseStatus }),
...(responseMsg && { response_msg: responseMsg }),
...(timestamp && { timestamp: timestamp }),
});
})
}
};
}

export const trackInputChangeForSegment = ({
eventName,
userExperience,
timestamp,
}) => {
export const trackInputChangeForSegment = ({ eventName, userExperience, timestamp }) => {
if (window.analytics) {
window.analytics.track("Input changed", {
window.analytics.track('Input changed', {
...(eventName && { event_name: eventName }),
...(userExperience && { user_experience: userExperience }),
...(timestamp && { timestamp: timestamp }),
});
})
}
}

export const trackFeedbackForSegment = ({
positive,
reason,
response,
locale,
timestamp,
device_type,
}) => {
if (window.analytics) {
window.analytics.track('Page Feedback Submitted', {
feedback_type: positive ? 'positive' : 'negative',
...(reason && { reason }),
...(response && { response }),
locale: locale || navigator.language,
page_path: window.location.pathname,
timestamp: timestamp || Date.now(),
device_type: device_type,
category: 'Documentation',
label: 'Page Helpfulness',
})
console.log('✅ Feedback tracked via Segment:', {
feedback_type: positive ? 'positive' : 'negative',
...(reason && { reason }),
...(response && { response }),
locale: locale || navigator.language,
page_path: window.location.pathname,
timestamp: timestamp || Date.now(),
device_type: device_type,
})
} else {
console.warn('⚠️ Segment analytics not available - feedback tracking skipped')
}
};
}
Loading