@@ -28,7 +28,7 @@ import {adminRequestDoc} from '../api/admin.js'
2828import { AdminSession } from '../session.js'
2929import { AbortError } from '../error.js'
3030import { outputDebug } from '../output.js'
31- import { recordTiming } from '../analytics.js'
31+ import { recordTiming , recordEvent , recordError } from '../analytics.js'
3232
3333export type ThemeParams = Partial < Pick < Theme , 'name' | 'role' | 'processing' | 'src' > >
3434export type AssetParams = Pick < ThemeAsset , 'key' > & Partial < Pick < ThemeAsset , 'value' | 'attachment' > >
@@ -41,6 +41,7 @@ const THEME_API_NETWORK_BEHAVIOUR: RequestModeInput = {
4141
4242export async function fetchTheme ( id : number , session : AdminSession ) : Promise < Theme | undefined > {
4343 const gid = composeThemeGid ( id )
44+ recordEvent ( 'theme-api:fetch-theme' )
4445
4546 try {
4647 const { theme} = await adminRequestDoc ( {
@@ -61,21 +62,23 @@ export async function fetchTheme(id: number, session: AdminSession): Promise<The
6162 }
6263
6364 // eslint-disable-next-line no-catch-all/no-catch-all
64- } catch ( _error ) {
65+ } catch ( error ) {
6566 /**
6667 * Consumers of this and other theme APIs in this file expect either a theme
6768 * or `undefined`.
6869 *
6970 * Error handlers should not inspect GraphQL error messages directly, as
7071 * they are internationalized.
7172 */
73+ recordError ( error )
7274 outputDebug ( `Error fetching theme with ID: ${ id } ` )
7375 }
7476}
7577
7678export async function fetchThemes ( session : AdminSession ) : Promise < Theme [ ] > {
7779 const themes : Theme [ ] = [ ]
7880 let after : string | null = null
81+ recordEvent ( 'theme-api:fetch-themes' )
7982
8083 while ( true ) {
8184 // eslint-disable-next-line no-await-in-loop
@@ -111,6 +114,7 @@ export async function fetchThemes(session: AdminSession): Promise<Theme[]> {
111114
112115export async function themeCreate ( params : ThemeParams , session : AdminSession ) : Promise < Theme | undefined > {
113116 const themeSource = params . src ?? SkeletonThemeCdn
117+ recordEvent ( 'theme-api:create-theme' )
114118 const { themeCreate} = await adminRequestDoc ( {
115119 query : ThemeCreate ,
116120 session,
@@ -130,7 +134,7 @@ export async function themeCreate(params: ThemeParams, session: AdminSession): P
130134 const { theme, userErrors} = themeCreate
131135 if ( userErrors . length ) {
132136 const userErrors = themeCreate . userErrors . map ( ( error ) => error . message ) . join ( ', ' )
133- throw new AbortError ( userErrors )
137+ throw recordError ( new AbortError ( userErrors ) )
134138 }
135139
136140 if ( ! theme ) {
@@ -147,6 +151,7 @@ export async function themeCreate(params: ThemeParams, session: AdminSession): P
147151export async function fetchThemeAssets ( id : number , filenames : Key [ ] , session : AdminSession ) : Promise < ThemeAsset [ ] > {
148152 const assets : ThemeAsset [ ] = [ ]
149153 let after : string | null = null
154+ recordEvent ( 'theme-api:fetch-assets' )
150155
151156 while ( true ) {
152157 // eslint-disable-next-line no-await-in-loop
@@ -191,6 +196,7 @@ export async function fetchThemeAssets(id: number, filenames: Key[], session: Ad
191196export async function deleteThemeAssets ( id : number , filenames : Key [ ] , session : AdminSession ) : Promise < Result [ ] > {
192197 const batchSize = 50
193198 const results : Result [ ] = [ ]
199+ recordEvent ( 'theme-api:delete-assets' )
194200
195201 for ( let i = 0 ; i < filenames . length ; i += batchSize ) {
196202 const batch = filenames . slice ( i , i + batchSize )
@@ -220,6 +226,7 @@ export async function deleteThemeAssets(id: number, filenames: Key[], session: A
220226 if ( userErrors . length > 0 ) {
221227 userErrors . forEach ( ( error ) => {
222228 if ( error . filename ) {
229+ recordError ( `Asset deletion failed for ${ error . filename } : ${ error . message } ` )
223230 results . push ( {
224231 key : error . filename ,
225232 success : false ,
@@ -242,6 +249,7 @@ export async function bulkUploadThemeAssets(
242249 session : AdminSession ,
243250) : Promise < Result [ ] > {
244251 const results : Result [ ] = [ ]
252+ recordEvent ( 'theme-api:bulk-upload-assets' )
245253 for ( let i = 0 ; i < assets . length ; i += 50 ) {
246254 const chunk = assets . slice ( i , i + 50 )
247255 const files = prepareFilesForUpload ( chunk )
@@ -314,6 +322,7 @@ function processUploadResults(uploadResults: ThemeFilesUpsertMutation): Result[]
314322 if ( ! error . filename ) {
315323 unexpectedGraphQLError ( `Error uploading theme files: ${ error . message } ` )
316324 }
325+ recordError ( `Asset upload failed for ${ error . filename } : ${ error . message } ` )
317326 results . push ( {
318327 key : error . filename ,
319328 success : false ,
@@ -328,6 +337,7 @@ function processUploadResults(uploadResults: ThemeFilesUpsertMutation): Result[]
328337export async function fetchChecksums ( id : number , session : AdminSession ) : Promise < Checksum [ ] > {
329338 const checksums : Checksum [ ] = [ ]
330339 let after : string | null = null
340+ recordEvent ( 'theme-api:fetch-checksums' )
331341
332342 while ( true ) {
333343 // eslint-disable-next-line no-await-in-loop
@@ -341,7 +351,7 @@ export async function fetchChecksums(id: number, session: AdminSession): Promise
341351
342352 if ( ! response ?. theme ?. files ?. nodes || ! response ?. theme ?. files ?. pageInfo ) {
343353 const userErrors = response . theme ?. files ?. userErrors . map ( ( error ) => error . filename ) . join ( ', ' )
344- throw new AbortError ( `Failed to fetch checksums for: ${ userErrors } ` )
354+ throw recordError ( new AbortError ( `Failed to fetch checksums for: ${ userErrors } ` ) )
345355 }
346356
347357 const { nodes, pageInfo} = response . theme . files
@@ -367,6 +377,7 @@ export async function themeUpdate(id: number, params: ThemeParams, session: Admi
367377 if ( name ) {
368378 input . name = name
369379 }
380+ recordEvent ( 'theme-api:update-theme' )
370381
371382 const { themeUpdate} = await adminRequestDoc ( {
372383 query : ThemeUpdate ,
@@ -382,7 +393,7 @@ export async function themeUpdate(id: number, params: ThemeParams, session: Admi
382393 const { theme, userErrors} = themeUpdate
383394 if ( userErrors . length ) {
384395 const userErrors = themeUpdate . userErrors . map ( ( error ) => error . message ) . join ( ', ' )
385- throw new AbortError ( userErrors )
396+ throw recordError ( new AbortError ( userErrors ) )
386397 }
387398
388399 if ( ! theme ) {
@@ -398,6 +409,7 @@ export async function themeUpdate(id: number, params: ThemeParams, session: Admi
398409}
399410
400411export async function themePublish ( id : number , session : AdminSession ) : Promise < Theme | undefined > {
412+ recordEvent ( 'theme-api:publish-theme' )
401413 const { themePublish} = await adminRequestDoc ( {
402414 query : ThemePublish ,
403415 session,
@@ -412,7 +424,7 @@ export async function themePublish(id: number, session: AdminSession): Promise<T
412424 const { theme, userErrors} = themePublish
413425 if ( userErrors . length ) {
414426 const userErrors = themePublish . userErrors . map ( ( error ) => error . message ) . join ( ', ' )
415- throw new AbortError ( userErrors )
427+ throw recordError ( new AbortError ( userErrors ) )
416428 }
417429
418430 if ( ! theme ) {
@@ -428,6 +440,7 @@ export async function themePublish(id: number, session: AdminSession): Promise<T
428440}
429441
430442export async function themeDelete ( id : number , session : AdminSession ) : Promise < boolean | undefined > {
443+ recordEvent ( 'theme-api:delete-theme' )
431444 const { themeDelete} = await adminRequestDoc ( {
432445 query : ThemeDelete ,
433446 session,
@@ -442,7 +455,7 @@ export async function themeDelete(id: number, session: AdminSession): Promise<bo
442455 const { deletedThemeId, userErrors} = themeDelete
443456 if ( userErrors . length ) {
444457 const userErrors = themeDelete . userErrors . map ( ( error ) => error . message ) . join ( ', ' )
445- throw new AbortError ( userErrors )
458+ throw recordError ( new AbortError ( userErrors ) )
446459 }
447460
448461 if ( ! deletedThemeId ) {
@@ -465,6 +478,7 @@ export async function themeDuplicate(
465478 session : AdminSession ,
466479) : Promise < ThemeDuplicateResult > {
467480 let requestId : string | undefined
481+ recordEvent ( 'theme-api:duplicate-theme' )
468482
469483 const { themeDuplicate} = await adminRequestDoc ( {
470484 query : ThemeDuplicate ,
@@ -481,6 +495,7 @@ export async function themeDuplicate(
481495
482496 if ( ! themeDuplicate ) {
483497 // An unexpected error occurred during the GraphQL request execution
498+ recordError ( 'Failed to duplicate theme' )
484499 return {
485500 theme : undefined ,
486501 userErrors : [ { message : 'Failed to duplicate theme' } ] ,
@@ -521,6 +536,7 @@ export async function themeDuplicate(
521536}
522537
523538export async function metafieldDefinitionsByOwnerType ( type : MetafieldOwnerType , session : AdminSession ) {
539+ recordEvent ( 'theme-api:fetch-metafield-definitions' )
524540 const { metafieldDefinitions} = await adminRequestDoc ( {
525541 query : MetafieldDefinitionsByOwnerType ,
526542 session,
@@ -540,6 +556,7 @@ export async function metafieldDefinitionsByOwnerType(type: MetafieldOwnerType,
540556}
541557
542558export async function passwordProtected ( session : AdminSession ) : Promise < boolean > {
559+ recordEvent ( 'theme-api:check-password-protection' )
543560 const { onlineStore} = await adminRequestDoc ( {
544561 query : OnlineStorePasswordProtection ,
545562 session,
@@ -554,7 +571,7 @@ export async function passwordProtected(session: AdminSession): Promise<boolean>
554571}
555572
556573function unexpectedGraphQLError ( message : string ) : never {
557- throw new AbortError ( message )
574+ throw recordError ( new AbortError ( message ) )
558575}
559576
560577function themeGid ( id : number ) : string {
@@ -583,7 +600,7 @@ export async function parseThemeFileContent(
583600 return { attachment : Buffer . from ( arrayBuffer ) . toString ( 'base64' ) }
584601 } catch ( error ) {
585602 // Raise error if we can't download the file
586- throw new AbortError ( `Error downloading content from URL: ${ body . url } ` )
603+ throw recordError ( new AbortError ( `Error downloading content from URL: ${ body . url } ` ) )
587604 }
588605 }
589606}
0 commit comments