|
1 | 1 | /** |
2 | | - * Google Ads conversion tracking utilities |
| 2 | + * Google Ads conversion tracking utilities with enhanced conversions support |
| 3 | + * Implements manual enhanced conversion tracking for better attribution |
3 | 4 | */ |
4 | 5 |
|
| 6 | +import { hasConsent } from "./consent-manager" |
| 7 | + |
| 8 | +/** |
| 9 | + * SHA256 hash function for enhanced conversion data |
| 10 | + * Required for properly hashing user data before sending to Google |
| 11 | + */ |
| 12 | +async function sha256(text: string): Promise<string> { |
| 13 | + const utf8 = new TextEncoder().encode(text.toLowerCase().trim()) |
| 14 | + const hashBuffer = await crypto.subtle.digest("SHA-256", utf8) |
| 15 | + const hashArray = Array.from(new Uint8Array(hashBuffer)) |
| 16 | + return hashArray.map((byte) => byte.toString(16).padStart(2, "0")).join("") |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Interface for enhanced conversion user data |
| 21 | + */ |
| 22 | +interface EnhancedConversionData { |
| 23 | + email?: string |
| 24 | + phone?: string |
| 25 | + firstName?: string |
| 26 | + lastName?: string |
| 27 | + address?: { |
| 28 | + street?: string |
| 29 | + city?: string |
| 30 | + region?: string |
| 31 | + postalCode?: string |
| 32 | + country?: string |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Prepare and hash user data for enhanced conversions |
| 38 | + * All data is hashed using SHA256 before sending to Google |
| 39 | + */ |
| 40 | +async function prepareEnhancedData( |
| 41 | + userData?: EnhancedConversionData, |
| 42 | +): Promise<Record<string, string | Record<string, string>[]> | null> { |
| 43 | + if (!userData) return null |
| 44 | + |
| 45 | + const enhancedData: Record<string, string | Record<string, string>[]> = {} |
| 46 | + |
| 47 | + try { |
| 48 | + if (userData.email) { |
| 49 | + enhancedData.email = await sha256(userData.email) |
| 50 | + } |
| 51 | + if (userData.phone) { |
| 52 | + // Remove non-numeric characters and add country code if missing |
| 53 | + const cleanPhone = userData.phone.replace(/[^0-9]/g, "") |
| 54 | + enhancedData.phone = await sha256(cleanPhone) |
| 55 | + } |
| 56 | + if (userData.firstName) { |
| 57 | + enhancedData.first_name = await sha256(userData.firstName) |
| 58 | + } |
| 59 | + if (userData.lastName) { |
| 60 | + enhancedData.last_name = await sha256(userData.lastName) |
| 61 | + } |
| 62 | + if (userData.address) { |
| 63 | + const address: Record<string, string> = {} |
| 64 | + if (userData.address.street) { |
| 65 | + address.street = await sha256(userData.address.street) |
| 66 | + } |
| 67 | + if (userData.address.city) { |
| 68 | + address.city = await sha256(userData.address.city) |
| 69 | + } |
| 70 | + if (userData.address.region) { |
| 71 | + address.region = await sha256(userData.address.region) |
| 72 | + } |
| 73 | + if (userData.address.postalCode) { |
| 74 | + address.postal_code = await sha256(userData.address.postalCode) |
| 75 | + } |
| 76 | + if (userData.address.country) { |
| 77 | + address.country = await sha256(userData.address.country) |
| 78 | + } |
| 79 | + if (Object.keys(address).length > 0) { |
| 80 | + enhancedData.address = [address] |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return Object.keys(enhancedData).length > 0 ? enhancedData : null |
| 85 | + } catch (error) { |
| 86 | + console.error("Error preparing enhanced conversion data:", error) |
| 87 | + return null |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Track a Google Ads conversion event with enhanced conversions |
| 93 | + * Implements manual enhanced conversion tracking for improved accuracy |
| 94 | + * |
| 95 | + * @param conversionLabel - Optional conversion label (defaults to PR Reviewer trial signup) |
| 96 | + * @param value - Optional conversion value |
| 97 | + * @param userData - Optional user data for enhanced conversions |
| 98 | + */ |
| 99 | +export async function trackGoogleAdsConversion( |
| 100 | + conversionLabel = "VtOZCJe_77MbEInXkOVA", |
| 101 | + value = 10.0, |
| 102 | + userData?: EnhancedConversionData, |
| 103 | +) { |
| 104 | + // Only track if consent has been given |
| 105 | + if (!hasConsent()) { |
| 106 | + console.log("Google Ads conversion tracking skipped - no consent") |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + if (typeof window !== "undefined" && window.gtag) { |
| 111 | + try { |
| 112 | + // Prepare enhanced conversion data if provided |
| 113 | + const enhancedData = await prepareEnhancedData(userData) |
| 114 | + |
| 115 | + // Build the conversion event parameters |
| 116 | + const conversionParams: Record< |
| 117 | + string, |
| 118 | + string | number | Record<string, string | Record<string, string>[]> |
| 119 | + > = { |
| 120 | + send_to: `AW-17391954825/${conversionLabel}`, |
| 121 | + value: value, |
| 122 | + currency: "USD", |
| 123 | + } |
| 124 | + |
| 125 | + // Add enhanced conversion data if available |
| 126 | + if (enhancedData) { |
| 127 | + conversionParams.user_data = enhancedData |
| 128 | + } |
| 129 | + |
| 130 | + // Send the conversion event |
| 131 | + window.gtag("event", "conversion", conversionParams) |
| 132 | + |
| 133 | + console.log("Google Ads conversion tracked with enhanced data") |
| 134 | + } catch (error) { |
| 135 | + console.error("Error tracking Google Ads conversion:", error) |
| 136 | + // Fall back to basic conversion tracking |
| 137 | + window.gtag("event", "conversion", { |
| 138 | + send_to: `AW-17391954825/${conversionLabel}`, |
| 139 | + value: value, |
| 140 | + currency: "USD", |
| 141 | + }) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
5 | 146 | /** |
6 | | - * Track a Google Ads conversion event |
7 | | - * This should only be called after user consent has been given |
| 147 | + * Track a page view conversion (for automatic event tracking) |
| 148 | + * Used when the conversion should fire on page load rather than user action |
8 | 149 | */ |
9 | | -export function trackGoogleAdsConversion() { |
| 150 | +export function trackPageViewConversion(conversionLabel = "VtOZCJe_77MbEInXkOVA", value = 10.0) { |
| 151 | + // Only track if consent has been given |
| 152 | + if (!hasConsent()) { |
| 153 | + return |
| 154 | + } |
| 155 | + |
10 | 156 | if (typeof window !== "undefined" && window.gtag) { |
11 | | - window.gtag("event", "conversion", { |
12 | | - send_to: "AW-17391954825/VtOZCJe_77MbEInXkOVA", |
13 | | - value: 10.0, |
14 | | - currency: "USD", |
15 | | - }) |
| 157 | + // Use a slight delay to ensure gtag is fully initialized |
| 158 | + setTimeout(() => { |
| 159 | + window.gtag("event", "page_view", { |
| 160 | + send_to: `AW-17391954825/${conversionLabel}`, |
| 161 | + value: value, |
| 162 | + currency: "USD", |
| 163 | + }) |
| 164 | + }, 100) |
16 | 165 | } |
17 | 166 | } |
0 commit comments