|
3 | 3 | import React, { useState, useEffect } from "react"; |
4 | 4 | import { motion, AnimatePresence } from "framer-motion"; |
5 | 5 | import { Sparkles, X, Gift, Zap, Rocket, AlertCircle } from "lucide-react"; |
6 | | -import { initMixpanelOnce } from "@/lib/analytics"; |
| 6 | +import { initMixpanelOnce, mixpanel } from "@/lib/analytics"; |
7 | 7 |
|
8 | 8 | // @ts-ignore |
9 | 9 | declare global { |
@@ -52,59 +52,54 @@ export function DynamicCTAButton() { |
52 | 52 | useEffect(() => { |
53 | 53 | initMixpanelOnce(); |
54 | 54 |
|
55 | | - // Fetch feature flag configuration |
56 | | - if (window.mixpanel?.flags) { |
57 | | - window.mixpanel.flags |
58 | | - .get_variant_value('we_buy_custom_cta', null) |
59 | | - .then((value: any) => { |
60 | | - console.log('[MIXPANEL]: Got CTA config:', value); |
| 55 | + // Fetch feature flag configuration. |
| 56 | + // The analytics proxy holds this call until the Mixpanel lib loads, |
| 57 | + // so we no longer need a "is Mixpanel there yet" guard. The .catch |
| 58 | + // below still applies the fallback if the read fails. |
| 59 | + mixpanel.flags |
| 60 | + .get_variant_value('we_buy_custom_cta', null) |
| 61 | + .then((value: any) => { |
| 62 | + console.log('[MIXPANEL]: Got CTA config:', value); |
61 | 63 |
|
62 | | - if (value && typeof value === 'object' && 'cta' in value) { |
63 | | - setConfig(value as CTAConfig); |
64 | | - // Try to determine variant key for tracking |
65 | | - Object.entries(defaultConfigs).forEach(([key, cfg]) => { |
66 | | - if (cfg.cta === value.cta) { |
67 | | - setVariantKey(key); |
68 | | - } |
69 | | - }); |
| 64 | + if (value && typeof value === 'object' && 'cta' in value) { |
| 65 | + setConfig(value as CTAConfig); |
| 66 | + // Try to determine variant key for tracking |
| 67 | + Object.entries(defaultConfigs).forEach(([key, cfg]) => { |
| 68 | + if (cfg.cta === value.cta) { |
| 69 | + setVariantKey(key); |
| 70 | + } |
| 71 | + }); |
70 | 72 |
|
71 | | - window.mixpanel.track('Dynamic CTA Loaded', { |
72 | | - variant: variantKey || 'custom', |
73 | | - cta_text: value.cta, |
74 | | - color: value.color |
75 | | - }); |
76 | | - } else { |
77 | | - // Fallback to a random default for demo purposes |
78 | | - const keys = Object.keys(defaultConfigs); |
79 | | - const randomKey = keys[Math.floor(Math.random() * keys.length)]; |
80 | | - setConfig(defaultConfigs[randomKey]); |
81 | | - setVariantKey(randomKey); |
| 73 | + window.mixpanel.track('Dynamic CTA Loaded', { |
| 74 | + variant: variantKey || 'custom', |
| 75 | + cta_text: value.cta, |
| 76 | + color: value.color |
| 77 | + }); |
| 78 | + } else { |
| 79 | + // Fallback to a random default for demo purposes |
| 80 | + const keys = Object.keys(defaultConfigs); |
| 81 | + const randomKey = keys[Math.floor(Math.random() * keys.length)]; |
| 82 | + setConfig(defaultConfigs[randomKey]); |
| 83 | + setVariantKey(randomKey); |
82 | 84 |
|
83 | | - console.log('[MIXPANEL]: Using fallback CTA config:', randomKey); |
84 | | - window.mixpanel.track('Dynamic CTA Loaded', { |
85 | | - variant: randomKey, |
86 | | - cta_text: defaultConfigs[randomKey].cta, |
87 | | - color: defaultConfigs[randomKey].color, |
88 | | - is_fallback: true |
89 | | - }); |
90 | | - } |
91 | | - setIsLoading(false); |
92 | | - }) |
93 | | - .catch((error: any) => { |
94 | | - console.error('[MIXPANEL]: Error fetching CTA config:', error); |
95 | | - // Use fallback on error |
96 | | - const fallbackKey = 'snag'; |
97 | | - setConfig(defaultConfigs[fallbackKey]); |
98 | | - setVariantKey(fallbackKey); |
99 | | - setIsLoading(false); |
100 | | - }); |
101 | | - } else { |
102 | | - // No Mixpanel, use fallback |
103 | | - const fallbackKey = 'snag'; |
104 | | - setConfig(defaultConfigs[fallbackKey]); |
105 | | - setVariantKey(fallbackKey); |
106 | | - setIsLoading(false); |
107 | | - } |
| 85 | + console.log('[MIXPANEL]: Using fallback CTA config:', randomKey); |
| 86 | + window.mixpanel.track('Dynamic CTA Loaded', { |
| 87 | + variant: randomKey, |
| 88 | + cta_text: defaultConfigs[randomKey].cta, |
| 89 | + color: defaultConfigs[randomKey].color, |
| 90 | + is_fallback: true |
| 91 | + }); |
| 92 | + } |
| 93 | + setIsLoading(false); |
| 94 | + }) |
| 95 | + .catch((error: any) => { |
| 96 | + console.error('[MIXPANEL]: Error fetching CTA config:', error); |
| 97 | + // Use fallback on error |
| 98 | + const fallbackKey = 'snag'; |
| 99 | + setConfig(defaultConfigs[fallbackKey]); |
| 100 | + setVariantKey(fallbackKey); |
| 101 | + setIsLoading(false); |
| 102 | + }); |
108 | 103 | }, []); |
109 | 104 |
|
110 | 105 | const handleClick = () => { |
|
0 commit comments