File size: 1,481 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"use client";

import { attributionMetadata, getAccountAttribution } from "@/lib/attribution";
import { track } from "@/lib/datafast";

export type ClipExportSurface = "task" | "editor" | "editor_queue";

/**
 * Records a clip export. Every export sends `clip_exported`; the user's first
 * export also sends `first_clip_exported` with the server-stored attribution,
 * so activation can be credited to the campaign even across devices. Only the
 * exporting account's own attribution (confirmed, or claimed and pending) is attached.
 */
export async function trackClipExport({ userId, ...details }: { userId: string; surface: ClipExportSurface; format: string }) {
  const accountMetadata = attributionMetadata(getAccountAttribution(userId));
  track("clip_exported", { ...details, ...accountMetadata });

  try {
    const response = await fetch("/api/attribution/clip-export", { method: "POST", keepalive: true });
    if (!response.ok) return;
    const result = await response.json();
    if (!result?.first_export) return;
    // An export can land before sign-up attribution syncs; fall back to this account's claimed visit.
    const serverMetadata = attributionMetadata(result.attribution);
    track("first_clip_exported", {
      ...details,
      ...(Object.keys(serverMetadata).length > 0 ? serverMetadata : accountMetadata),
      hours_since_signup: result.hours_since_signup,
    });
  } catch {
    // Tracking must never interfere with the download itself.
  }
}