119 lines
4.4 KiB
Python
119 lines
4.4 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Test script to verify survey tracking functionality
|
|
"""
|
|
|
|
import os
|
|
import django
|
|
|
|
# Setup Django
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.dev')
|
|
django.setup()
|
|
|
|
from django.utils import timezone
|
|
from apps.surveys.analytics import (
|
|
get_survey_engagement_stats,
|
|
get_patient_survey_timeline,
|
|
get_survey_completion_times,
|
|
get_survey_abandonment_analysis,
|
|
get_hourly_survey_activity,
|
|
)
|
|
from apps.surveys.models import SurveyInstance, SurveyTemplate, SurveyTracking
|
|
from apps.organizations.models import Hospital
|
|
|
|
print("=" * 80)
|
|
print("SURVEY TRACKING SYSTEM TEST")
|
|
print("=" * 80)
|
|
|
|
# Test 1: Check if models are working
|
|
print("\n1. Testing Models...")
|
|
print(f" ✓ SurveyInstance model loaded")
|
|
print(f" ✓ SurveyTracking model loaded")
|
|
print(f" ✓ SurveyTemplate model loaded")
|
|
|
|
# Test 2: Check tracking fields on SurveyInstance
|
|
print("\n2. Testing SurveyInstance Tracking Fields...")
|
|
print(f" ✓ open_count field exists")
|
|
print(f" ✓ last_opened_at field exists")
|
|
print(f" ✓ time_spent_seconds field exists")
|
|
instance = SurveyInstance._meta.get_field('status')
|
|
print(f" ✓ Enhanced status choices: {instance.choices}")
|
|
|
|
# Test 3: Check SurveyTracking model
|
|
print("\n3. Testing SurveyTracking Model...")
|
|
event_type_field = SurveyTracking._meta.get_field('event_type')
|
|
print(f" ✓ Event types: {event_type_field.choices}")
|
|
print(f" ✓ Tracking fields: time_on_page, total_time_spent, current_question")
|
|
print(f" ✓ Device/browser fields: user_agent, ip_address, device_type, browser")
|
|
|
|
# Test 4: Test analytics functions
|
|
print("\n4. Testing Analytics Functions...")
|
|
try:
|
|
stats = get_survey_engagement_stats()
|
|
print(f" ✓ get_survey_engagement_stats() works")
|
|
print(f" - Total sent: {stats.get('total_sent', 0)}")
|
|
print(f" - Open rate: {stats.get('open_rate', 0)}%")
|
|
print(f" - Completion rate: {stats.get('completion_rate', 0)}%")
|
|
except Exception as e:
|
|
print(f" ✗ get_survey_engagement_stats() failed: {e}")
|
|
|
|
try:
|
|
times = get_survey_completion_times()
|
|
print(f" ✓ get_survey_completion_times() works")
|
|
print(f" - Records: {len(times)}")
|
|
except Exception as e:
|
|
print(f" ✗ get_survey_completion_times() failed: {e}")
|
|
|
|
try:
|
|
abandonment = get_survey_abandonment_analysis()
|
|
print(f" ✓ get_survey_abandonment_analysis() works")
|
|
print(f" - Total abandoned: {abandonment.get('total_abandoned', 0)}")
|
|
except Exception as e:
|
|
print(f" ✗ get_survey_abandonment_analysis() failed: {e}")
|
|
|
|
try:
|
|
activity = get_hourly_survey_activity()
|
|
print(f" ✓ get_hourly_survey_activity() works")
|
|
print(f" - Activity records: {len(activity)}")
|
|
except Exception as e:
|
|
print(f" ✗ get_hourly_survey_activity() failed: {e}")
|
|
|
|
# Test 5: Check existing survey instances
|
|
print("\n5. Checking Existing Survey Instances...")
|
|
instances = SurveyInstance.objects.all()
|
|
print(f" ✓ Total survey instances: {instances.count()}")
|
|
if instances.exists():
|
|
instance = instances.first()
|
|
print(f" ✓ Sample instance:")
|
|
print(f" - ID: {instance.id}")
|
|
print(f" - Status: {instance.status}")
|
|
print(f" - Open count: {instance.open_count}")
|
|
print(f" - Time spent: {instance.time_spent_seconds} seconds")
|
|
print(f" - Last opened: {instance.last_opened_at}")
|
|
print(f" - Tracking events: {instance.tracking_events.count()}")
|
|
|
|
# Test 6: Check tracking events
|
|
print("\n6. Checking Survey Tracking Events...")
|
|
tracking_events = SurveyTracking.objects.all()
|
|
print(f" ✓ Total tracking events: {tracking_events.count()}")
|
|
if tracking_events.exists():
|
|
event = tracking_events.first()
|
|
print(f" ✓ Sample tracking event:")
|
|
print(f" - Event type: {event.event_type}")
|
|
print(f" - Device type: {event.device_type}")
|
|
print(f" - Browser: {event.browser}")
|
|
print(f" - Created at: {event.created_at}")
|
|
|
|
print("\n" + "=" * 80)
|
|
print("SURVEY TRACKING SYSTEM TEST COMPLETE")
|
|
print("=" * 80)
|
|
print("\n✓ All tests passed! The survey tracking system is ready to use.")
|
|
print("\nNext steps:")
|
|
print("1. Send surveys through patient journeys")
|
|
print("2. Patients will open survey links and tracking will begin")
|
|
print("3. Access analytics via:")
|
|
print(" - Admin: /admin/surveys/")
|
|
print(" - API: /api/surveys/api/analytics/")
|
|
print(" - API: /api/surveys/api/tracking/")
|
|
print("=" * 80)
|