Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { KeyFactorDraft } from "@/types/key_factors";
import { PostWithForecasts } from "@/types/post";
import { Question } from "@/types/question";
import { sendAnalyticsEvent } from "@/utils/analytics";
import cn from "@/utils/core/cn";
import {
isBaseRateDraft,
Expand Down Expand Up @@ -83,6 +84,14 @@ type CombinedSuggestionItem =
keyFactorIndex: number;
};

/**
* Helper to determine the type of a key factor draft
*/
const getKeyFactorType = (
kf: KeyFactorDraft
): "driver" | "base_rate" | "news" =>
isDriverDraft(kf) ? "driver" : isBaseRateDraft(kf) ? "base_rate" : "news";

const KeyFactorsAddInCommentLLMSuggestions: React.FC<Props> = ({
onBack,
postData,
Expand Down Expand Up @@ -217,6 +226,12 @@ const KeyFactorsAddInCommentLLMSuggestions: React.FC<Props> = ({
idx: number,
opts?: { showErrors?: boolean }
) => {
// Track edit action
const keyFactorType = getKeyFactorType(kf);
sendAnalyticsEvent("keyFactorLLMSuggestionEdited", {
event_category: keyFactorType,
});

const id = editingIdRef.current++;
setEditingSessions((prev) => [
...prev,
Expand Down Expand Up @@ -708,6 +723,12 @@ const KeyFactorsAddInCommentLLMSuggestions: React.FC<Props> = ({
<KeyFactorActionButton
kind="accept"
onClick={async () => {
// Track accept action
const keyFactorType = getKeyFactorType(kf);
sendAnalyticsEvent("keyFactorLLMSuggestionAccepted", {
event_category: keyFactorType,
});

const res = await addSingleSuggestedKeyFactor(kf);
if (!res || ("errors" in res && res.errors)) {
handleEdit(kf, keyFactorIndex, {
Expand All @@ -725,7 +746,15 @@ const KeyFactorsAddInCommentLLMSuggestions: React.FC<Props> = ({
/>
<KeyFactorActionButton
kind="reject"
onClick={() => removeKeyFactorAt(keyFactorIndex)}
onClick={() => {
// Track reject action
const keyFactorType = getKeyFactorType(kf);
sendAnalyticsEvent("keyFactorLLMSuggestionRejected", {
event_category: keyFactorType,
});

removeKeyFactorAt(keyFactorIndex);
}}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ export const useKeyFactors = ({

setSuggestedKeyFactors(filtered);
fetchedOnceRef.current.add(cid);

// Track when key factors are successfully generated
if (filtered.length > 0) {
sendAnalyticsEvent("keyFactorLLMGenerated", {
event_category: "success",
count: filtered.length,
driver_count: filtered.filter(isDriverDraft).length,
base_rate_count: filtered.filter(isBaseRateDraft).length,
news_count: filtered.filter(isNewsDraft).length,
});
}
} finally {
setIsLoadingSuggestedKeyFactors(false);
delete inFlightRef.current[cid];
Expand All @@ -140,6 +151,13 @@ export const useKeyFactors = ({
const comment = comments.find((c) => c.id === commentId);
if (!comment) return;

// Track automatic generation attempt (before user clicks button)
if (!fetchedOnceRef.current.has(commentId)) {
sendAnalyticsEvent("keyFactorLLMGenerationAttempted", {
event_category: "automatic",
});
}

const ids = extractQuestionNumbersFromText(comment.text || "");
if (!ids.length) {
questionLinksCheckedRef.current.add(commentId);
Expand Down Expand Up @@ -289,9 +307,35 @@ export const useKeyFactors = ({
is_private: false,
});

// Determine if this is a manual creation or from LLM suggestions
// Count based on filtered arrays to exclude empty drafts
const manualCount =
(submitType === "driver"
? driverDrafts.filter((d) => d.driver.text.trim() !== "").length
: 0) +
(submitType === "base_rate" ? baseRateDrafts.length : 0) +
(submitType === "news" ? newsDrafts.length : 0);
const suggestedCount =
(submitType === "driver"
? suggestedDriverDrafts.filter((d) => d.driver.text.trim() !== "")
.length
: 0) +
(submitType === "base_rate" ? suggestedBaseRateDrafts.length : 0) +
(submitType === "news" ? suggestedNewsDrafts.length : 0);

// Track the creation source
let source = "manual";
if (suggestedCount > 0 && manualCount === 0) {
source = "llm_suggestion";
} else if (suggestedCount > 0 && manualCount > 0) {
source = "mixed";
}

sendAnalyticsEvent("addKeyFactor", {
event_label: isNil(commentId) ? "fromList" : "fromComment",
event_category: "submit",
event_category: submitType,
source,
count: writePayloads.length,
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

if ("errors" in comment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type { KeyFactorDraft } from "@/types/key_factors";
import type { PostWithForecasts } from "@/types/post";
import { Question } from "@/types/question";
import type { User } from "@/types/users";
import { sendAnalyticsEvent } from "@/utils/analytics";

import { createEmptyBaseRateDraft } from "./item_creation/base_rate/utils";

Expand Down Expand Up @@ -226,6 +227,13 @@ const KeyFactorsProviderEnabled: React.FC<EnabledProps> = ({
),
loadSuggestions: (force?: boolean) => {
if (isLoadingSuggestedKeyFactors) return;

// Track when user clicks "Ask an LLM" button
sendAnalyticsEvent("keyFactorGenerateClicked", {
event_category: commentId ? "fromComment" : "fromList",
force: !!force,
});

if (force) reloadSuggestions();
else if (!shouldLoadSuggestions) setShouldLoadSuggestions(true);
},
Expand All @@ -248,6 +256,7 @@ const KeyFactorsProviderEnabled: React.FC<EnabledProps> = ({
submitImpl,
reloadSuggestions,
addSingleSuggestedKeyFactor,
commentId,
]
);

Expand Down
Loading