Skip to content

Commit d23cb8b

Browse files
densumeshcdxker
authored andcommitted
feature: add pixel on load and add button to manually add pixel in settings
1 parent a2496b8 commit d23cb8b

File tree

5 files changed

+99
-69
lines changed

5 files changed

+99
-69
lines changed

clients/trieve-shopify-extension/app/components/DatasetSettings.tsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import {
55
Button,
66
Card,
77
FormLayout,
8+
Icon,
89
InlineStack,
910
Select,
1011
Text,
1112
TextField,
1213
} from "@shopify/polaris";
14+
import { CheckCircleIcon } from "@shopify/polaris-icons";
1315
import { useEffect, useState } from "react";
1416
import { CrawlOptions, Dataset, DatasetConfigurationDTO } from "trieve-ts-sdk";
1517

@@ -37,10 +39,6 @@ export type DatasetConfig = Exclude<
3739
LLM_API_KEY?: string | null;
3840
};
3941

40-
export type RevenueTrackingOptions = {
41-
checkout_selector: string;
42-
};
43-
4442
export const defaultServerEnvsConfiguration: DatasetConfig = {
4543
LLM_BASE_URL: "",
4644
LLM_DEFAULT_MODEL: "",
@@ -70,23 +68,17 @@ export const defaultServerEnvsConfiguration: DatasetConfig = {
7068
BM25_AVG_LEN: 256,
7169
};
7270

73-
export const defaultRevenueTrackingOptions: RevenueTrackingOptions = {
74-
checkout_selector: "",
75-
};
76-
7771
export const DatasetSettings = ({
7872
initalCrawlOptions,
79-
initalRevenueTrackingOptions,
73+
webPixelInstalled,
8074
shopDataset,
8175
}: {
8276
initalCrawlOptions: ExtendedCrawlOptions;
83-
initalRevenueTrackingOptions: RevenueTrackingOptions;
77+
webPixelInstalled: boolean;
8478
shopDataset: Dataset;
8579
}) => {
8680
const [unsavedCrawlOptions, setUnsavedCrawlOptions] =
8781
useState(initalCrawlOptions);
88-
const [unsavedRevenueTrackingOptions, setUnsavedRevenueTrackingOptions] =
89-
useState(initalRevenueTrackingOptions);
9082
const shopify = useAppBridge();
9183
const submit = useSubmit();
9284
const [datasetSettings, setDatasetSettings] = useState<DatasetConfig>(
@@ -147,7 +139,6 @@ export const DatasetSettings = ({
147139
const onRevenueTrackingSettingsSave = async () => {
148140
submit(
149141
{
150-
revenue_tracking_options: JSON.stringify(unsavedRevenueTrackingOptions),
151142
dataset_id: shopDataset.id,
152143
type: "revenue_tracking",
153144
},
@@ -206,6 +197,30 @@ export const DatasetSettings = ({
206197
</InlineStack>
207198
</BlockStack>
208199
</Card>
200+
<Card>
201+
<BlockStack gap="200">
202+
<Text variant="headingLg" as="h1">
203+
Revenue Tracking Settings
204+
</Text>
205+
206+
<FormLayout>
207+
<BlockStack gap="200">
208+
<div className="max-w-fit">
209+
{webPixelInstalled ? (
210+
<Button disabled fullWidth={false} icon={CheckCircleIcon} size="slim">
211+
Revenue Tracker Installed
212+
</Button>
213+
) : (
214+
<Button onClick={onRevenueTrackingSettingsSave}>Install Revenue Tracker</Button>
215+
)}
216+
</div>
217+
<Text as="p" tone="subdued" variant="bodySm">
218+
Install the revenue tracker to start tracking revenue.
219+
</Text>
220+
</BlockStack>
221+
</FormLayout>
222+
</BlockStack>
223+
</Card>
209224
<Card>
210225
<BlockStack gap="200">
211226
<Text variant="headingLg" as="h1">

clients/trieve-shopify-extension/app/components/onboarding/AddComponentOnboarding.tsx

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,7 @@ import { useTrieve } from "app/context/trieveContext";
1919
import { TrieveKey } from "app/types";
2020
import { AdminApiCaller } from "app/loaders";
2121

22-
const createWebPixel = async (adminApi: AdminApiCaller, trieveKey: TrieveKey) => {
23-
const response = await adminApi(
24-
`
25-
#graphql
26-
mutation webPixelCreate($webPixel: WebPixelInput!) {
27-
webPixelCreate(webPixel: $webPixel) {
28-
userErrors {
29-
code
30-
field
31-
message
32-
}
33-
webPixel {
34-
settings
35-
id
36-
}
37-
}
38-
}
39-
`,
40-
{
41-
variables: {
42-
webPixel: {
43-
settings: `{"apiKey":"${trieveKey.key}", "datasetId": "${trieveKey.currentDatasetId}"}`,
44-
},
45-
},
46-
},
47-
);
48-
console.log("Web pixel created", response);
49-
};
22+
5023

5124
const getShortThemeId = (fullGid: string): string | null => {
5225
const regex = /gid:\/\/shopify\/OnlineStoreTheme\/(\d+)/;
@@ -208,7 +181,6 @@ export const AddComponentOnboarding: OnboardingBody = ({
208181
useEffect(() => {
209182
if (globalComplete) {
210183
if (trieve.organizationId && trieve.trieve.apiKey != null) {
211-
createWebPixel(adminApi, trieveKey);
212184
trackCustomerEvent(
213185
trieve.trieve.baseUrl,
214186
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { TrieveKey } from "app/types";
2+
3+
import { AdminApiCaller } from "app/loaders";
4+
5+
export const isWebPixelInstalled = async (adminApi: AdminApiCaller, trieveKey: TrieveKey) => {
6+
const response = await adminApi<{ webPixel: { id: string | null, settings: string } }>(
7+
`
8+
#graphql
9+
query {
10+
webPixel {
11+
id
12+
settings
13+
}
14+
}
15+
`,
16+
);
17+
18+
if (response.error) {
19+
console.error(response.error);
20+
throw response.error;
21+
}
22+
return response.data.webPixel.id !== null;
23+
};
24+
25+
export const createWebPixel = async (adminApi: AdminApiCaller, trieveKey: TrieveKey) => {
26+
const response = await adminApi(
27+
`
28+
#graphql
29+
mutation webPixelCreate($webPixel: WebPixelInput!) {
30+
webPixelCreate(webPixel: $webPixel) {
31+
userErrors {
32+
code
33+
field
34+
message
35+
}
36+
webPixel {
37+
settings
38+
id
39+
}
40+
}
41+
}
42+
`,
43+
{
44+
variables: {
45+
webPixel: {
46+
settings: `{"apiKey":"${trieveKey.key}", "datasetId": "${trieveKey.currentDatasetId}"}`,
47+
},
48+
},
49+
},
50+
);
51+
console.log("Web pixel created", response);
52+
};

clients/trieve-shopify-extension/app/routes/app._dashboard.settings.tsx

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { sdkFromKey, validateTrieveAuth } from "app/auth";
66
import {
77
DatasetSettings as DatasetSettings,
88
ExtendedCrawlOptions,
9-
RevenueTrackingOptions,
109
} from "app/components/DatasetSettings";
1110
import { useTrieve } from "app/context/trieveContext";
1211
import { AdminApiCaller } from "app/loaders";
@@ -17,6 +16,7 @@ import { authenticate } from "app/shopify.server";
1716
import { type Dataset } from "trieve-ts-sdk";
1817
import { AppInstallData } from "./app.setup";
1918
import { ResetSettings } from "app/components/ResetSettings";
19+
import { createWebPixel, isWebPixelInstalled } from "app/queries/webPixel";
2020

2121
const setAppMetafields = async (
2222
adminApi: AdminApiCaller,
@@ -111,7 +111,7 @@ export const loader = async ({
111111
request,
112112
}: LoaderFunctionArgs): Promise<{
113113
crawlSettings: ExtendedCrawlOptions | undefined;
114-
revenueTrackingSettings: RevenueTrackingOptions | undefined;
114+
webPixelInstalled: boolean;
115115
}> => {
116116
const { session } = await authenticate.admin(request);
117117
const key = await validateTrieveAuth(request);
@@ -140,16 +140,11 @@ export const loader = async ({
140140
},
141141
})) as any;
142142

143-
const revenueTrackingSettings: RevenueTrackingOptions = {
144-
checkout_selector:
145-
(await getAppMetafields(fetcher)).find(
146-
(metafield) => metafield.key === "checkout_selector",
147-
)?.value ?? "",
148-
};
143+
const webPixelInstalled = await isWebPixelInstalled(fetcher, key);
149144

150145
return {
151146
crawlSettings: crawlSettings?.crawlSettings,
152-
revenueTrackingSettings,
147+
webPixelInstalled,
153148
};
154149
};
155150

@@ -211,19 +206,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
211206

212207
return { success: true };
213208
} else if (type === "revenue_tracking") {
214-
const revenueTrackingSettingsString = formData.get(
215-
"revenue_tracking_options",
216-
);
217-
const revenueTrackingSettings = JSON.parse(
218-
revenueTrackingSettingsString as string,
219-
);
220-
await setAppMetafields(fetcher, [
221-
{
222-
key: "checkout_selector",
223-
value: revenueTrackingSettings.checkout_selector,
224-
},
225-
]).catch(console.error);
226-
209+
await createWebPixel(fetcher, key);
227210
return { success: true };
228211
}
229212
return { success: false };
@@ -232,16 +215,14 @@ export const action = async ({ request }: ActionFunctionArgs) => {
232215
export default function Dataset() {
233216
const { trieve } = useTrieve();
234217
const { data: shopDataset } = useSuspenseQuery(shopDatasetQuery(trieve));
235-
const { crawlSettings, revenueTrackingSettings } =
218+
const { crawlSettings, webPixelInstalled } =
236219
useLoaderData<typeof loader>();
237220

238221
return (
239222
<Box paddingBlockStart="400">
240223
<DatasetSettings
241224
initalCrawlOptions={crawlSettings as ExtendedCrawlOptions}
242-
initalRevenueTrackingOptions={
243-
revenueTrackingSettings as RevenueTrackingOptions
244-
}
225+
webPixelInstalled={webPixelInstalled}
245226
shopDataset={shopDataset as Dataset}
246227
/>
247228
<div className="h-4"></div>

clients/trieve-shopify-extension/app/routes/app._dashboard.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import {
1212
import { TrieveProvider } from "app/context/trieveContext";
1313
import { authenticate } from "app/shopify.server";
1414
import { useDehydratedState } from "app/dehydrate";
15-
import { StrongTrieveKey } from "app/types";
15+
import { StrongTrieveKey, TrieveKey } from "app/types";
1616
import { Dataset, OrganizationWithSubAndPlan } from "trieve-ts-sdk";
1717
import { useState, Suspense } from "react";
1818
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
1919
import { shopDatasetQuery } from "app/queries/shopDataset";
20+
import { buildAdminApiFetcherForServer } from "app/loaders/serverLoader";
21+
import { createWebPixel, isWebPixelInstalled } from "app/queries/webPixel";
2022

2123
export const loader = async (args: LoaderFunctionArgs) => {
2224
const key = await validateTrieveAuth(args.request, false);
@@ -36,6 +38,14 @@ export const loader = async (args: LoaderFunctionArgs) => {
3638
// Fill in dataset info
3739
const queryClient = new QueryClient();
3840
queryClient.setQueryData(shopDatasetQuery(trieve).queryKey, dataset);
41+
const fetcher = buildAdminApiFetcherForServer(
42+
session.shop,
43+
session.accessToken!,
44+
);
45+
46+
if (!(await isWebPixelInstalled(fetcher, key))) {
47+
await createWebPixel(fetcher, key);
48+
}
3949

4050
return {
4151
key: key as StrongTrieveKey,

0 commit comments

Comments
 (0)