Skip to content

The Open Source DocuSign Alternative.

feat: optimise posthog usage and add error tracking (#3301)

Lucas Smithae4b56dd88cf
diff --git a/apps/remix/app/components/dialogs/envelope-distribute-dialog.tsx b/apps/remix/app/components/dialogs/envelope-distribute-dialog.tsx
index ed495f83a..0cd5e3c4c 100644
--- a/apps/remix/app/components/dialogs/envelope-distribute-dialog.tsx
+++ b/apps/remix/app/components/dialogs/envelope-distribute-dialog.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { useCurrentEnvelopeEditor } from '@documenso/lib/client-only/providers/envelope-editor-provider';
import { useCurrentOrganisation } from '@documenso/lib/client-only/providers/organisation';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
@@ -71,6 +72,7 @@ export const EnvelopeDistributeDialog = ({
const { toast } = useToast();
const { t, i18n } = useLingui();
const navigate = useNavigate();
+ const analytics = useAnalytics();
const [isOpen, setIsOpen] = useState(false);
const [isSyncing, setIsSyncing] = useState(false);
@@ -200,6 +202,12 @@ export const EnvelopeDistributeDialog = ({
} catch (err) {
const error = AppError.parseError(err);
+ analytics.captureException(err, {
+ source: 'editor',
+ location: 'distribute_document',
+ envelopeId: envelope.id,
+ });
+
const errorMessage = getDistributeErrorMessage(error.code);
toast({
diff --git a/apps/remix/app/components/dialogs/envelope-redistribute-dialog.tsx b/apps/remix/app/components/dialogs/envelope-redistribute-dialog.tsx
index 1a54c7893..b2d5da538 100644
--- a/apps/remix/app/components/dialogs/envelope-redistribute-dialog.tsx
+++ b/apps/remix/app/components/dialogs/envelope-redistribute-dialog.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { getRecipientType } from '@documenso/lib/client-only/recipient-type';
import { AppError } from '@documenso/lib/errors/app-error';
import type { TEnvelope } from '@documenso/lib/types/envelope';
@@ -51,6 +52,7 @@ export const EnvelopeRedistributeDialog = ({ envelope, envelopeType, trigger }:
const { toast } = useToast();
const { t, i18n } = useLingui();
+ const analytics = useAnalytics();
const [isOpen, setIsOpen] = useState(false);
@@ -95,6 +97,13 @@ export const EnvelopeRedistributeDialog = ({ envelope, envelopeType, trigger }:
setIsOpen(false);
} catch (err) {
const error = AppError.parseError(err);
+
+ analytics.captureException(err, {
+ source: 'editor',
+ location: 'redistribute_document',
+ envelopeId: envelope.id,
+ });
+
const errorMessage = getDistributeErrorMessage(error.code);
toast({
diff --git a/apps/remix/app/components/embed/embed-direct-template-client-page.tsx b/apps/remix/app/components/embed/embed-direct-template-client-page.tsx
index 20a0a23fa..3e2ca1d38 100644
--- a/apps/remix/app/components/embed/embed-direct-template-client-page.tsx
+++ b/apps/remix/app/components/embed/embed-direct-template-client-page.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { useThrottleFn } from '@documenso/lib/client-only/hooks/use-throttle-fn';
import { DEFAULT_DOCUMENT_DATE_FORMAT } from '@documenso/lib/constants/date-formats';
import { APP_I18N_OPTIONS } from '@documenso/lib/constants/i18n';
@@ -77,6 +78,7 @@ export const EmbedDirectTemplateClientPage = ({
}: EmbedDirectTemplateClientPageProps) => {
const { _ } = useLingui();
const { toast } = useToast();
+ const analytics = useAnalytics();
const [searchParams] = useSearchParams();
@@ -264,6 +266,13 @@ export const EmbedDirectTemplateClientPage = ({
const error = AppError.parseError(err);
const errorMessage = getDirectTemplateErrorMessage(error.code);
+ analytics.captureException(err, {
+ source: 'embed',
+ location: 'direct_template',
+ recipientId: recipient.id,
+ envelopeId,
+ });
+
toast({
title: _(errorMessage.title),
description: _(errorMessage.description),
@@ -308,6 +317,14 @@ export const EmbedDirectTemplateClientPage = ({
}
} catch (err) {
console.error(err);
+
+ analytics.captureException(err, {
+ source: 'embed',
+ location: 'embed_init',
+ recipientId: recipient.id,
+ envelopeId,
+ });
+
setHasFinishedInit(true);
}
diff --git a/apps/remix/app/components/embed/embed-document-signing-page-v1.tsx b/apps/remix/app/components/embed/embed-document-signing-page-v1.tsx
index c8726ae22..739a5c8a6 100644
--- a/apps/remix/app/components/embed/embed-document-signing-page-v1.tsx
+++ b/apps/remix/app/components/embed/embed-document-signing-page-v1.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { useThrottleFn } from '@documenso/lib/client-only/hooks/use-throttle-fn';
import { APP_I18N_OPTIONS } from '@documenso/lib/constants/i18n';
import { PDF_VIEWER_PAGE_SELECTOR } from '@documenso/lib/constants/pdf-viewer';
@@ -75,6 +76,7 @@ export const EmbedSignDocumentV1ClientPage = ({
}: EmbedSignDocumentV1ClientPageProps) => {
const { _ } = useLingui();
const { toast } = useToast();
+ const analytics = useAnalytics();
const { fullName, email, signature, setFullName, setEmail, setSignature } = useRequiredDocumentSigningContext();
@@ -154,6 +156,14 @@ export const EmbedSignDocumentV1ClientPage = ({
setHasCompletedDocument(true);
} catch (err) {
+ analytics.captureException(err, {
+ source: 'embed',
+ location: 'complete_document',
+ recipientId: recipient.id,
+ documentId,
+ envelopeId,
+ });
+
if (window.parent) {
window.parent.postMessage(
{
@@ -236,6 +246,15 @@ export const EmbedSignDocumentV1ClientPage = ({
}
} catch (err) {
console.error(err);
+
+ analytics.captureException(err, {
+ source: 'embed',
+ location: 'embed_init',
+ recipientId: recipient.id,
+ documentId,
+ envelopeId,
+ });
+
setHasFinishedInit(true);
}
diff --git a/apps/remix/app/components/embed/embed-document-signing-page-v2.tsx b/apps/remix/app/components/embed/embed-document-signing-page-v2.tsx
index 969511713..55e7cb6d1 100644
--- a/apps/remix/app/components/embed/embed-document-signing-page-v2.tsx
+++ b/apps/remix/app/components/embed/embed-document-signing-page-v2.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { APP_I18N_OPTIONS } from '@documenso/lib/constants/i18n';
import { ZSignDocumentEmbedDataSchema } from '@documenso/lib/types/embed-document-sign-schema';
import { mapSecondaryIdToDocumentId } from '@documenso/lib/utils/envelope';
@@ -25,6 +26,7 @@ export const EmbedSignDocumentV2ClientPage = ({
allowWhitelabelling = false,
}: EmbedSignDocumentV2ClientPageProps) => {
const { _ } = useLingui();
+ const analytics = useAnalytics();
const { envelope, recipient, envelopeData, setFullName, setEmail, fullName, email } =
useRequiredEnvelopeSigningContext();
@@ -170,6 +172,14 @@ export const EmbedSignDocumentV2ClientPage = ({
}
} catch (err) {
console.error(err);
+
+ analytics.captureException(err, {
+ source: 'embed',
+ location: 'embed_init',
+ recipientId: recipient.id,
+ envelopeId: envelope.id,
+ });
+
setHasFinishedInit(true);
}
diff --git a/apps/remix/app/components/embed/multisign/multi-sign-document-signing-view.tsx b/apps/remix/app/components/embed/multisign/multi-sign-document-signing-view.tsx
index 15902715d..8964033a6 100644
--- a/apps/remix/app/components/embed/multisign/multi-sign-document-signing-view.tsx
+++ b/apps/remix/app/components/embed/multisign/multi-sign-document-signing-view.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { PDF_VIEWER_PAGE_SELECTOR } from '@documenso/lib/constants/pdf-viewer';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { getDocumentDataUrlForPdfViewer } from '@documenso/lib/utils/envelope-download';
@@ -57,6 +58,7 @@ export const MultiSignDocumentSigningView = ({
}: MultiSignDocumentSigningViewProps) => {
const { _ } = useLingui();
const { toast } = useToast();
+ const analytics = useAnalytics();
const { fullName, email, signature, setFullName, setSignature } = useRequiredDocumentSigningContext();
@@ -101,6 +103,13 @@ export const MultiSignDocumentSigningView = ({
console.error(err);
+ analytics.captureException(err, {
+ source: 'embed',
+ location: 'sign_field',
+ recipientId,
+ documentId: document?.id,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while signing the document.`),
@@ -120,6 +129,13 @@ export const MultiSignDocumentSigningView = ({
}
console.error(err);
+
+ analytics.captureException(err, {
+ source: 'embed',
+ location: 'remove_field',
+ recipientId,
+ documentId: document?.id,
+ });
}
};
@@ -140,6 +156,13 @@ export const MultiSignDocumentSigningView = ({
recipientId,
});
} catch (err) {
+ analytics.captureException(err, {
+ source: 'embed',
+ location: 'complete_document',
+ recipientId,
+ documentId: document?.id,
+ });
+
onDocumentError?.();
const error = AppError.parseError(err);
diff --git a/apps/remix/app/components/general/direct-template/direct-template-page.tsx b/apps/remix/app/components/general/direct-template/direct-template-page.tsx
index 9f6ea47dc..58ea7d50f 100644
--- a/apps/remix/app/components/general/direct-template/direct-template-page.tsx
+++ b/apps/remix/app/components/general/direct-template/direct-template-page.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { RECIPIENT_ROLES_DESCRIPTION } from '@documenso/lib/constants/recipient-roles';
import { AppError } from '@documenso/lib/errors/app-error';
import type { TTemplate } from '@documenso/lib/types/template';
@@ -41,6 +42,7 @@ export const DirectTemplatePageView = ({
const { _ } = useLingui();
const { toast } = useToast();
+ const analytics = useAnalytics();
const { email, fullName, setEmail } = useRequiredDocumentSigningContext();
const { recipient, setRecipient } = useRequiredDocumentSigningAuthContext();
@@ -124,6 +126,13 @@ export const DirectTemplatePageView = ({
const error = AppError.parseError(err);
const errorMessage = getDirectTemplateErrorMessage(error.code);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'direct_template',
+ recipientId: directTemplateRecipient.id,
+ envelopeId: template.envelopeId,
+ });
+
toast({
title: _(errorMessage.title),
description: _(errorMessage.description),
diff --git a/apps/remix/app/components/general/document-signing/document-signing-checkbox-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-checkbox-field.tsx
index 7e9a7adb6..dcf2e2d81 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-checkbox-field.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-checkbox-field.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import type { TRecipientActionAuth } from '@documenso/lib/types/document-auth';
@@ -39,6 +40,7 @@ export const DocumentSigningCheckboxField = ({
const { _ } = useLingui();
const { toast } = useToast();
const { revalidate } = useRevalidator();
+ const analytics = useAnalytics();
const { recipient, isAssistantMode } = useDocumentSigningRecipientContext();
@@ -126,6 +128,13 @@ export const DocumentSigningCheckboxField = ({
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: isAssistantMode
@@ -157,6 +166,13 @@ export const DocumentSigningCheckboxField = ({
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'remove_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while removing the field.`),
@@ -216,6 +232,13 @@ export const DocumentSigningCheckboxField = ({
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while updating the signature.`),
diff --git a/apps/remix/app/components/general/document-signing/document-signing-complete-dialog.tsx b/apps/remix/app/components/general/document-signing/document-signing-complete-dialog.tsx
index 41abb63f1..f5508f727 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-complete-dialog.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-complete-dialog.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { type TRecipientAccessAuth, ZDocumentAccessAuthSchema } from '@documenso/lib/types/document-auth';
import { fieldsContainUnsignedRequiredField } from '@documenso/lib/utils/advanced-fields-helpers';
@@ -88,6 +89,7 @@ export const DocumentSigningCompleteDialog = ({
position,
disableNameInput = false,
}: DocumentSigningCompleteDialogProps) => {
+ const analytics = useAnalytics();
const { t, i18n } = useLingui();
const { toast } = useToast();
@@ -179,6 +181,11 @@ export const DocumentSigningCompleteDialog = ({
return;
}
+ analytics.captureException(error, {
+ source: 'signing',
+ location: 'complete_document',
+ });
+
// This dialog owns the completion error toast for every signing surface
// so the user gets a specific, actionable message. Callers should run
// their own side effects (e.g. embeds posting document-error) and
diff --git a/apps/remix/app/components/general/document-signing/document-signing-date-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-date-field.tsx
index 1d05d3017..51135630c 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-date-field.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-date-field.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { convertToLocalSystemFormat, DEFAULT_DOCUMENT_DATE_FORMAT } from '@documenso/lib/constants/date-formats';
import { DEFAULT_DOCUMENT_TIME_ZONE } from '@documenso/lib/constants/time-zones';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
@@ -39,6 +40,7 @@ export const DocumentSigningDateField = ({
const { _ } = useLingui();
const { toast } = useToast();
const { revalidate } = useRevalidator();
+ const analytics = useAnalytics();
const { recipient, isAssistantMode } = useDocumentSigningRecipientContext();
@@ -85,6 +87,13 @@ export const DocumentSigningDateField = ({
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: isAssistantMode
@@ -113,6 +122,13 @@ export const DocumentSigningDateField = ({
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'remove_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while removing the field.`),
diff --git a/apps/remix/app/components/general/document-signing/document-signing-dropdown-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-dropdown-field.tsx
index 2ac26f87c..549eb6128 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-dropdown-field.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-dropdown-field.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import type { TRecipientActionAuth } from '@documenso/lib/types/document-auth';
@@ -35,6 +36,7 @@ export const DocumentSigningDropdownField = ({
const { _ } = useLingui();
const { toast } = useToast();
const { revalidate } = useRevalidator();
+ const analytics = useAnalytics();
const { recipient, isAssistantMode } = useDocumentSigningRecipientContext();
@@ -86,6 +88,13 @@ export const DocumentSigningDropdownField = ({
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: isAssistantMode
@@ -120,6 +129,13 @@ export const DocumentSigningDropdownField = ({
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'remove_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while removing the field.`),
diff --git a/apps/remix/app/components/general/document-signing/document-signing-email-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-email-field.tsx
index 408914336..8e88d99be 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-email-field.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-email-field.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import type { TRecipientActionAuth } from '@documenso/lib/types/document-auth';
@@ -33,6 +34,7 @@ export const DocumentSigningEmailField = ({ field, onSignField, onUnsignField }:
const { _ } = useLingui();
const { toast } = useToast();
const { revalidate } = useRevalidator();
+ const analytics = useAnalytics();
const { email: providedEmail } = useRequiredDocumentSigningContext();
@@ -78,6 +80,13 @@ export const DocumentSigningEmailField = ({ field, onSignField, onUnsignField }:
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: isAssistantMode
@@ -106,6 +115,13 @@ export const DocumentSigningEmailField = ({ field, onSignField, onUnsignField }:
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'remove_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while removing the field.`),
diff --git a/apps/remix/app/components/general/document-signing/document-signing-initials-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-initials-field.tsx
index 6a9b74a3d..b79bf40fe 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-initials-field.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-initials-field.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import type { TRecipientActionAuth } from '@documenso/lib/types/document-auth';
@@ -38,6 +39,7 @@ export const DocumentSigningInitialsField = ({
const { toast } = useToast();
const { _ } = useLingui();
const { revalidate } = useRevalidator();
+ const analytics = useAnalytics();
const { fullName } = useRequiredDocumentSigningContext();
const { recipient, isAssistantMode } = useDocumentSigningRecipientContext();
@@ -84,6 +86,13 @@ export const DocumentSigningInitialsField = ({
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: isAssistantMode
@@ -112,6 +121,13 @@ export const DocumentSigningInitialsField = ({
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'remove_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while removing the field.`),
diff --git a/apps/remix/app/components/general/document-signing/document-signing-name-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-name-field.tsx
index 71ebe6995..0b4660e5d 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-name-field.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-name-field.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import type { TRecipientActionAuth } from '@documenso/lib/types/document-auth';
@@ -39,6 +40,7 @@ export const DocumentSigningNameField = ({ field, onSignField, onUnsignField }:
const { _ } = useLingui();
const { toast } = useToast();
const { revalidate } = useRevalidator();
+ const analytics = useAnalytics();
const { fullName: providedFullName, setFullName: setProvidedFullName } = useRequiredDocumentSigningContext();
@@ -116,6 +118,13 @@ export const DocumentSigningNameField = ({ field, onSignField, onUnsignField }:
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: isAssistantMode
@@ -144,6 +153,13 @@ export const DocumentSigningNameField = ({ field, onSignField, onUnsignField }:
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'remove_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while removing the field.`),
diff --git a/apps/remix/app/components/general/document-signing/document-signing-number-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-number-field.tsx
index 01f0e9714..0dd92c0ed 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-number-field.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-number-field.tsx
@@ -1,4 +1,5 @@
import { validateNumberField } from '@documenso/lib/advanced-fields-validation/validate-number';
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import type { TRecipientActionAuth } from '@documenso/lib/types/document-auth';
@@ -47,6 +48,7 @@ export const DocumentSigningNumberField = ({ field, onSignField, onUnsignField }
const { _ } = useLingui();
const { toast } = useToast();
const { revalidate } = useRevalidator();
+ const analytics = useAnalytics();
const { recipient, isAssistantMode } = useDocumentSigningRecipientContext();
@@ -142,6 +144,13 @@ export const DocumentSigningNumberField = ({ field, onSignField, onUnsignField }
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: isAssistantMode
@@ -193,6 +202,13 @@ export const DocumentSigningNumberField = ({ field, onSignField, onUnsignField }
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'remove_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while removing the field.`),
diff --git a/apps/remix/app/components/general/document-signing/document-signing-page-view-v1.tsx b/apps/remix/app/components/general/document-signing/document-signing-page-view-v1.tsx
index 1979c63a2..4fa8077a0 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-page-view-v1.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-page-view-v1.tsx
@@ -1,4 +1,3 @@
-import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { DEFAULT_DOCUMENT_DATE_FORMAT } from '@documenso/lib/constants/date-formats';
import { PDF_VIEWER_PAGE_SELECTOR } from '@documenso/lib/constants/pdf-viewer';
import { DEFAULT_DOCUMENT_TIME_ZONE } from '@documenso/lib/constants/time-zones';
@@ -83,8 +82,6 @@ export const DocumentSigningPageViewV1 = ({
? authUser.twoFactorEnabled && authUser.email === recipient.email
: false;
- const analytics = useAnalytics();
-
const [selectedSignerId, setSelectedSignerId] = useState<number | null>(allRecipients?.[0]?.id);
const [isExpanded, setIsExpanded] = useState(false);
@@ -118,12 +115,6 @@ export const DocumentSigningPageViewV1 = ({
await completeDocumentWithToken(payload);
- analytics.capture('App: Recipient has completed signing', {
- signerId: recipient.id,
- documentId: document.id,
- timestamp: new Date().toISOString(),
- });
-
if (documentMeta?.redirectUrl) {
window.location.href = documentMeta.redirectUrl;
} else {
diff --git a/apps/remix/app/components/general/document-signing/document-signing-radio-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-radio-field.tsx
index e9d6af196..b155344db 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-radio-field.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-radio-field.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import type { TRecipientActionAuth } from '@documenso/lib/types/document-auth';
@@ -31,6 +32,7 @@ export const DocumentSigningRadioField = ({ field, onSignField, onUnsignField }:
const { _ } = useLingui();
const { toast } = useToast();
const { revalidate } = useRevalidator();
+ const analytics = useAnalytics();
const { recipient, targetSigner, isAssistantMode } = useDocumentSigningRecipientContext();
@@ -91,6 +93,13 @@ export const DocumentSigningRadioField = ({ field, onSignField, onUnsignField }:
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: isAssistantMode
@@ -120,6 +129,13 @@ export const DocumentSigningRadioField = ({ field, onSignField, onUnsignField }:
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'remove_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while removing the selection.`),
diff --git a/apps/remix/app/components/general/document-signing/document-signing-reject-dialog.tsx b/apps/remix/app/components/general/document-signing/document-signing-reject-dialog.tsx
index ba70ad048..f0d07eac8 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-reject-dialog.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-reject-dialog.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { trpc } from '@documenso/trpc/react';
import { Button } from '@documenso/ui/primitives/button';
import {
@@ -41,6 +42,7 @@ export function DocumentSigningRejectDialog({
}: DocumentSigningRejectDialogProps) {
const { t } = useLingui();
const { toast } = useToast();
+ const analytics = useAnalytics();
const [searchParams] = useSearchParams();
const [isOpen, setIsOpen] = useState(false);
@@ -76,6 +78,12 @@ export function DocumentSigningRejectDialog({
window.location.href = `/sign/${token}/rejected`;
}
} catch (err) {
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'reject_document',
+ documentId,
+ });
+
toast({
title: t`Error`,
description: t`An error occurred while rejecting the document. Please try again.`,
diff --git a/apps/remix/app/components/general/document-signing/document-signing-signature-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-signature-field.tsx
index 3ff8b1d68..ecab0b1bc 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-signature-field.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-signature-field.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import type { TRecipientActionAuth } from '@documenso/lib/types/document-auth';
@@ -47,6 +48,7 @@ export const DocumentSigningSignatureField = ({
const { _ } = useLingui();
const { toast } = useToast();
const { revalidate } = useRevalidator();
+ const analytics = useAnalytics();
const { recipient } = useDocumentSigningRecipientContext();
@@ -157,6 +159,13 @@ export const DocumentSigningSignatureField = ({
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while signing the document.`),
@@ -183,6 +192,13 @@ export const DocumentSigningSignatureField = ({
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'remove_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while removing the signature.`),
diff --git a/apps/remix/app/components/general/document-signing/document-signing-text-field.tsx b/apps/remix/app/components/general/document-signing/document-signing-text-field.tsx
index f3f12063a..b99b7337c 100644
--- a/apps/remix/app/components/general/document-signing/document-signing-text-field.tsx
+++ b/apps/remix/app/components/general/document-signing/document-signing-text-field.tsx
@@ -1,4 +1,5 @@
import { validateTextField } from '@documenso/lib/advanced-fields-validation/validate-text';
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { DO_NOT_INVALIDATE_QUERY_ON_MUTATION } from '@documenso/lib/constants/trpc';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import type { TRecipientActionAuth } from '@documenso/lib/types/document-auth';
@@ -50,6 +51,7 @@ export const DocumentSigningTextField = ({ field, onSignField, onUnsignField }:
const { _ } = useLingui();
const { toast } = useToast();
const { revalidate } = useRevalidator();
+ const analytics = useAnalytics();
const { recipient, isAssistantMode } = useDocumentSigningRecipientContext();
@@ -170,6 +172,13 @@ export const DocumentSigningTextField = ({ field, onSignField, onUnsignField }:
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: isAssistantMode
@@ -200,6 +209,13 @@ export const DocumentSigningTextField = ({ field, onSignField, onUnsignField }:
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'remove_field',
+ fieldType: field.type,
+ recipientId: field.recipientId,
+ });
+
toast({
title: _(msg`Error`),
description: _(msg`An error occurred while removing the field.`),
diff --git a/apps/remix/app/components/general/document/document-upload-button-legacy.tsx b/apps/remix/app/components/general/document/document-upload-button-legacy.tsx
index c53cd93c0..5be052dfc 100644
--- a/apps/remix/app/components/general/document/document-upload-button-legacy.tsx
+++ b/apps/remix/app/components/general/document/document-upload-button-legacy.tsx
@@ -1,5 +1,4 @@
import { useLimits } from '@documenso/ee/server-only/limits/provider/client';
-import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { useCurrentOrganisation } from '@documenso/lib/client-only/providers/organisation';
import { useSession } from '@documenso/lib/client-only/providers/session';
import { DEFAULT_DOCUMENT_TIME_ZONE, TIME_ZONES } from '@documenso/lib/constants/time-zones';
@@ -38,7 +37,6 @@ export const DocumentUploadButtonLegacy = ({ className, type }: DocumentUploadBu
const team = useCurrentTeam();
const navigate = useNavigate();
- const analytics = useAnalytics();
const organisation = useCurrentOrganisation();
const userTimezone =
@@ -103,12 +101,6 @@ export const DocumentUploadButtonLegacy = ({ className, type }: DocumentUploadBu
description: _(msg`Your document has been uploaded successfully.`),
duration: 5000,
});
-
- analytics.capture('App: Document Uploaded', {
- userId: user.id,
- documentId: id,
- timestamp: new Date().toISOString(),
- });
}
// Handle legacy template creation.
diff --git a/apps/remix/app/components/general/envelope-editor/envelope-editor-fields-page-renderer.tsx b/apps/remix/app/components/general/envelope-editor/envelope-editor-fields-page-renderer.tsx
index 698f83651..c9deb51ad 100644
--- a/apps/remix/app/components/general/envelope-editor/envelope-editor-fields-page-renderer.tsx
+++ b/apps/remix/app/components/general/envelope-editor/envelope-editor-fields-page-renderer.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { useDebouncedValue } from '@documenso/lib/client-only/hooks/use-debounced-value';
import type { TLocalField } from '@documenso/lib/client-only/hooks/use-editor-fields';
import { usePageRenderer } from '@documenso/lib/client-only/hooks/use-page-renderer';
@@ -39,6 +40,7 @@ import { EnvelopeRecipientSelectorCommand } from './envelope-recipient-selector'
export const EnvelopeEditorFieldsPageRenderer = ({ pageData }: { pageData: PageRenderData }) => {
const { t, i18n } = useLingui();
+ const analytics = useAnalytics();
const { envelope, editorFields, getRecipientColorKey } = useCurrentEnvelopeEditor();
const { currentEnvelopeItem, setRenderError } = useCurrentEnvelopeRender();
@@ -276,6 +278,13 @@ export const EnvelopeEditorFieldsPageRenderer = ({ pageData }: { pageData: PageR
unsafeRenderFieldOnLayer(field);
} catch (err) {
console.error(err);
+
+ analytics.captureException(err, {
+ source: 'editor',
+ location: 'envelope_page_render',
+ envelopeId: envelope.id,
+ });
+
setRenderError(true);
}
};
diff --git a/apps/remix/app/components/general/envelope-editor/envelope-editor-upload-page.tsx b/apps/remix/app/components/general/envelope-editor/envelope-editor-upload-page.tsx
index c6fb38938..73493c385 100644
--- a/apps/remix/app/components/general/envelope-editor/envelope-editor-upload-page.tsx
+++ b/apps/remix/app/components/general/envelope-editor/envelope-editor-upload-page.tsx
@@ -1,4 +1,5 @@
import { useLimits } from '@documenso/ee/server-only/limits/provider/client';
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { useEnvelopeAutosave } from '@documenso/lib/client-only/hooks/use-envelope-autosave';
import { useCurrentEnvelopeEditor } from '@documenso/lib/client-only/providers/envelope-editor-provider';
import { useCurrentOrganisation } from '@documenso/lib/client-only/providers/organisation';
@@ -45,6 +46,7 @@ export const EnvelopeEditorUploadPage = () => {
const { t, i18n } = useLingui();
const { maximumEnvelopeItemCount, remaining } = useLimits();
const { toast } = useToast();
+ const analytics = useAnalytics();
const {
envelope,
@@ -213,6 +215,12 @@ export const EnvelopeEditorUploadPage = () => {
const { data } = await createPromise.catch((error) => {
console.error(error);
+ analytics.captureException(error, {
+ source: isEmbedded ? 'embed' : 'editor',
+ location: 'create_envelope_items',
+ envelopeId: envelope.id,
+ });
+
// Set error state on files in batch upload.
setLocalFiles((prev) =>
prev.map((uploadingFile) =>
@@ -290,6 +298,12 @@ export const EnvelopeEditorUploadPage = () => {
} catch (error) {
console.error(error);
+ analytics.captureException(error, {
+ source: isEmbedded ? 'embed' : 'editor',
+ location: 'replace_pdf',
+ envelopeId: envelope.id,
+ });
+
toast({
title: t`Replace failed`,
description: t`Something went wrong while replacing the PDF`,
diff --git a/apps/remix/app/components/general/envelope-editor/envelope-generic-page-renderer.tsx b/apps/remix/app/components/general/envelope-editor/envelope-generic-page-renderer.tsx
index 7fd72c703..015012a73 100644
--- a/apps/remix/app/components/general/envelope-editor/envelope-generic-page-renderer.tsx
+++ b/apps/remix/app/components/general/envelope-editor/envelope-generic-page-renderer.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { usePageRenderer } from '@documenso/lib/client-only/hooks/use-page-renderer';
import {
type PageRenderData,
@@ -18,6 +19,7 @@ type GenericLocalField = TEnvelope['fields'][number] & {
export const EnvelopeGenericPageRenderer = ({ pageData }: { pageData: PageRenderData }) => {
const { i18n } = useLingui();
+ const analytics = useAnalytics();
const {
envelopeStatus,
@@ -114,6 +116,13 @@ export const EnvelopeGenericPageRenderer = ({ pageData }: { pageData: PageRender
unsafeRenderFieldOnLayer(field);
} catch (err) {
console.error(err);
+
+ analytics.captureException(err, {
+ source: 'editor',
+ location: 'envelope_page_render',
+ envelopeId: currentEnvelopeItem?.envelopeId,
+ });
+
setRenderError(true);
}
};
diff --git a/apps/remix/app/components/general/envelope-signing/envelope-signer-page-renderer.tsx b/apps/remix/app/components/general/envelope-signing/envelope-signer-page-renderer.tsx
index 8caab7ac0..1c5c54ca0 100644
--- a/apps/remix/app/components/general/envelope-signing/envelope-signer-page-renderer.tsx
+++ b/apps/remix/app/components/general/envelope-signing/envelope-signer-page-renderer.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { usePageRenderer } from '@documenso/lib/client-only/hooks/use-page-renderer';
import {
type PageRenderData,
@@ -53,6 +54,7 @@ export const EnvelopeSignerPageRenderer = ({ pageData }: { pageData: PageRenderD
const { executeActionAuthProcedure } = useRequiredDocumentSigningAuthContext();
const { toast } = useToast();
+ const analytics = useAnalytics();
const {
envelopeData,
@@ -426,6 +428,14 @@ export const EnvelopeSignerPageRenderer = ({ pageData }: { pageData: PageRenderD
unsafeRenderFieldOnLayer(unparsedField, fieldCanvasStyleCache);
} catch (err) {
console.error(err);
+
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'page_render',
+ recipientId: recipient.id,
+ envelopeId: envelope.id,
+ });
+
setRenderError(true);
}
};
@@ -507,6 +517,14 @@ export const EnvelopeSignerPageRenderer = ({ pageData }: { pageData: PageRenderD
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'sign_field',
+ fieldType: payload.type,
+ recipientId: recipient.id,
+ envelopeId: envelope.id,
+ });
+
toast({
title: t`Error`,
description: t`An error occurred while signing the field.`,
diff --git a/apps/remix/app/components/general/envelope-signing/envelope-signing-complete-dialog.tsx b/apps/remix/app/components/general/envelope-signing/envelope-signing-complete-dialog.tsx
index 29f1dbeee..c522419ca 100644
--- a/apps/remix/app/components/general/envelope-signing/envelope-signing-complete-dialog.tsx
+++ b/apps/remix/app/components/general/envelope-signing/envelope-signing-complete-dialog.tsx
@@ -118,12 +118,6 @@ export const EnvelopeSignerCompleteDialog = () => {
title: t`Document already signed`,
description: t`This document was already signed and no further action was taken.`,
});
- } else {
- analytics.capture('App: Recipient has completed signing', {
- signerId: recipient.id,
- documentId: envelope.id,
- timestamp: new Date().toISOString(),
- });
}
if (onDocumentCompleted) {
@@ -148,6 +142,13 @@ export const EnvelopeSignerCompleteDialog = () => {
const error = AppError.parseError(err);
if (error.code !== AppErrorCode.TWO_FACTOR_AUTH_FAILED) {
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'complete_document',
+ recipientId: recipient.id,
+ envelopeId: envelope.id,
+ });
+
onDocumentError?.();
}
@@ -221,6 +222,13 @@ export const EnvelopeSignerCompleteDialog = () => {
} catch (err) {
console.log('err', err);
+ analytics.captureException(err, {
+ source: 'signing',
+ location: 'complete_document_next_signer',
+ recipientId: recipient.id,
+ envelopeId: envelope.id,
+ });
+
onDocumentError?.();
// Rethrow so DocumentSigningCompleteDialog can toast a specific
diff --git a/apps/remix/app/components/general/envelope/envelope-drop-zone-wrapper.tsx b/apps/remix/app/components/general/envelope/envelope-drop-zone-wrapper.tsx
index 3b1a0b188..cf7bfb67c 100644
--- a/apps/remix/app/components/general/envelope/envelope-drop-zone-wrapper.tsx
+++ b/apps/remix/app/components/general/envelope/envelope-drop-zone-wrapper.tsx
@@ -93,14 +93,6 @@ export const EnvelopeDropZoneWrapper = ({ children, type, className }: EnvelopeD
duration: 5000,
});
- if (type === EnvelopeType.DOCUMENT) {
- analytics.capture('App: Document Uploaded', {
- userId: user.id,
- documentId: id,
- timestamp: new Date().toISOString(),
- });
- }
-
const pathPrefix = type === EnvelopeType.DOCUMENT ? formatDocumentsPath(team.url) : formatTemplatesPath(team.url);
const aiQueryParam = team.preferences.aiFeaturesEnabled ? '?ai=true' : '';
@@ -109,6 +101,11 @@ export const EnvelopeDropZoneWrapper = ({ children, type, className }: EnvelopeD
} catch (err) {
const error = AppError.parseError(err);
+ analytics.captureException(err, {
+ source: 'editor',
+ location: 'upload_document',
+ });
+
const errorMessage = getUploadErrorMessage(error.code);
toast({
diff --git a/apps/remix/app/components/general/envelope/envelope-upload-button.tsx b/apps/remix/app/components/general/envelope/envelope-upload-button.tsx
index 25611fd40..c177eb8fb 100644
--- a/apps/remix/app/components/general/envelope/envelope-upload-button.tsx
+++ b/apps/remix/app/components/general/envelope/envelope-upload-button.tsx
@@ -1,4 +1,5 @@
import { useLimits } from '@documenso/ee/server-only/limits/provider/client';
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { useCurrentOrganisation } from '@documenso/lib/client-only/providers/organisation';
import { useSession } from '@documenso/lib/client-only/providers/session';
import { TIME_ZONES } from '@documenso/lib/constants/time-zones';
@@ -34,6 +35,7 @@ export const EnvelopeUploadButton = ({ className, type, folderId }: EnvelopeUplo
const { t, i18n } = useLingui();
const { toast } = useToast();
const { user } = useSession();
+ const analytics = useAnalytics();
const team = useCurrentTeam();
@@ -112,6 +114,11 @@ export const EnvelopeUploadButton = ({ className, type, folderId }: EnvelopeUplo
console.error(err);
+ analytics.captureException(err, {
+ source: 'editor',
+ location: 'upload_document',
+ });
+
const errorMessage = getUploadErrorMessage(error.code);
toast({
diff --git a/apps/remix/app/components/general/pdf-viewer/pdf-viewer.tsx b/apps/remix/app/components/general/pdf-viewer/pdf-viewer.tsx
index c795485f2..3fa39a232 100644
--- a/apps/remix/app/components/general/pdf-viewer/pdf-viewer.tsx
+++ b/apps/remix/app/components/general/pdf-viewer/pdf-viewer.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import type { ImageLoadingState, PageRenderData } from '@documenso/lib/client-only/providers/envelope-render-provider';
import { PDF_VIEWER_PAGE_CLASSNAME } from '@documenso/lib/constants/pdf-viewer';
import { cn } from '@documenso/ui/lib/utils';
@@ -68,6 +69,7 @@ export default function PDFViewer({
}: PDFViewerProps) {
const { t } = useLingui();
const { toast } = useToast();
+ const analytics = useAnalytics();
const $el = useRef<HTMLDivElement>(null);
@@ -150,6 +152,11 @@ export default function PDFViewer({
console.error(err);
setLoadingState('error');
+ analytics.captureException(err, {
+ source: 'pdf_viewer',
+ location: 'pdf_load',
+ });
+
toast({
title: t`Error`,
description: t`An error occurred while loading the document.`,
@@ -366,6 +373,8 @@ const PdfViewerPage = ({
* Manages rendering a page from a pdf.
*/
const usePdfPageImage = ({ pageNumber, pdf, scale, scaledWidth, scaledHeight }: PdfViewerPageProps) => {
+ const analytics = useAnalytics();
+
const [imageLoadingState, setImageLoadingState] = useState<ImageLoadingState>('loading');
const [imageUrl, setImageUrl] = useState('');
@@ -457,6 +466,12 @@ const usePdfPageImage = ({ pageNumber, pdf, scale, scaledWidth, scaledHeight }:
if (!isCancelled) {
console.error(err);
+
+ analytics.captureException(err, {
+ source: 'pdf_viewer',
+ location: 'pdf_page_render',
+ });
+
setImageLoadingState('error');
}
} finally {
diff --git a/apps/remix/app/entry.client.tsx b/apps/remix/app/entry.client.tsx
index f76430676..57daa65e3 100644
--- a/apps/remix/app/entry.client.tsx
+++ b/apps/remix/app/entry.client.tsx
@@ -18,6 +18,23 @@ import './utils/polyfills/promise-with-resolvers';
* the page early, leaving dead event handlers (broken dropdowns, native form
* submits).
*/
+/**
+ * Signing and direct template URLs contain recipient tokens which must never
+ * be sent to PostHog. Recipient context is attached explicitly via
+ * `recipientId` where needed instead.
+ */
+const redactTokensFromUrl = (value: string) => {
+ return value.replace(/(\/(?:sign|d|direct)\/)([^/?#]+)/g, '$1:token');
+};
+
+const URL_EVENT_PROPERTIES = [
+ '$current_url',
+ '$pathname',
+ '$referrer',
+ '$initial_referrer',
+ '$prev_pageview_pathname',
+] as const;
+
function initPosthog() {
const postHogConfig = extractPostHogConfig();
@@ -25,7 +42,34 @@ function initPosthog() {
void import('posthog-js').then(({ default: posthog }) => {
posthog.init(postHogConfig.key, {
api_host: postHogConfig.host,
+ // Only create person profiles for identified (authenticated) users,
+ // anonymous recipients on signing pages stay anonymous.
+ person_profiles: 'identified_only',
+ // Explicit events only, autocapture on signing pages blows up usage
+ // without providing actionable data.
+ autocapture: false,
+ capture_pageview: true,
+ capture_pageleave: false,
capture_exceptions: true,
+ before_send: (event) => {
+ if (!event) {
+ return null;
+ }
+
+ for (const property of URL_EVENT_PROPERTIES) {
+ const value = event.properties?.[property];
+
+ if (typeof value === 'string') {
+ event.properties[property] = redactTokensFromUrl(value);
+ }
+ }
+
+ if (event.$set_once && typeof event.$set_once['$initial_current_url'] === 'string') {
+ event.$set_once['$initial_current_url'] = redactTokensFromUrl(event.$set_once['$initial_current_url']);
+ }
+
+ return event;
+ },
});
});
}
diff --git a/apps/remix/app/root.tsx b/apps/remix/app/root.tsx
index 75b37495c..baffaa6ec 100644
--- a/apps/remix/app/root.tsx
+++ b/apps/remix/app/root.tsx
@@ -1,4 +1,5 @@
import { getOptionalSession } from '@documenso/auth/server/lib/utils/get-session';
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { SessionProvider } from '@documenso/lib/client-only/providers/session';
import { getBasePath } from '@documenso/lib/constants/app';
import { APP_I18N_OPTIONS, type SupportedLanguageCodes } from '@documenso/lib/constants/i18n';
@@ -9,6 +10,7 @@ import { getOrganisationSession } from '@documenso/trpc/server/organisation-rout
import { Toaster } from '@documenso/ui/primitives/toaster';
import { TooltipProvider } from '@documenso/ui/primitives/tooltip';
import { NuqsAdapter } from 'nuqs/adapters/react-router/v7';
+import { useEffect } from 'react';
import {
data,
isRouteErrorResponse,
@@ -200,11 +202,19 @@ export default function App() {
}
export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
+ const analytics = useAnalytics();
+
const errorCode = isRouteErrorResponse(error) ? error.status : 500;
if (errorCode !== 404) {
console.error('[RootErrorBoundary]', error);
}
+ useEffect(() => {
+ if (errorCode !== 404) {
+ analytics.captureException(error, { source: 'app', location: 'root_boundary' });
+ }
+ }, [error]);
+
return <GenericErrorLayout errorCode={errorCode} />;
}
diff --git a/apps/remix/app/routes/_authenticated+/t.$teamUrl+/documents.$id._layout.tsx b/apps/remix/app/routes/_authenticated+/t.$teamUrl+/documents.$id._layout.tsx
index 578658441..0931f0474 100644
--- a/apps/remix/app/routes/_authenticated+/t.$teamUrl+/documents.$id._layout.tsx
+++ b/apps/remix/app/routes/_authenticated+/t.$teamUrl+/documents.$id._layout.tsx
@@ -1,4 +1,5 @@
import { getSession } from '@documenso/auth/server/lib/utils/get-session';
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { getEnvelopeById } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
import { getTeamByUrl } from '@documenso/lib/server-only/team/get-team';
@@ -7,6 +8,7 @@ import { msg } from '@lingui/core/macro';
import { Trans } from '@lingui/react/macro';
import { EnvelopeType } from '@prisma/client';
import { ChevronLeftIcon } from 'lucide-react';
+import { useEffect } from 'react';
import type { ShouldRevalidateFunctionArgs } from 'react-router';
import { isRouteErrorResponse, Link, Outlet, redirect } from 'react-router';
@@ -66,8 +68,18 @@ export default function DocumentsLayout() {
}
export function ErrorBoundary({ error, params }: Route.ErrorBoundaryProps) {
+ const analytics = useAnalytics();
+
const errorCode = isRouteErrorResponse(error) ? error.status : 500;
+ useEffect(() => {
+ analytics.captureException(error, {
+ source: 'editor',
+ location: 'editor_layout_boundary',
+ envelopeId: params.id,
+ });
+ }, [error]);
+
const errorCodeMap = {
404: {
subHeading: msg`404 Document not found`,
diff --git a/apps/remix/app/routes/_authenticated+/t.$teamUrl+/templates.$id._layout.tsx b/apps/remix/app/routes/_authenticated+/t.$teamUrl+/templates.$id._layout.tsx
index 3edd8fd34..b0faefe1b 100644
--- a/apps/remix/app/routes/_authenticated+/t.$teamUrl+/templates.$id._layout.tsx
+++ b/apps/remix/app/routes/_authenticated+/t.$teamUrl+/templates.$id._layout.tsx
@@ -1,4 +1,5 @@
import { getSession } from '@documenso/auth/server/lib/utils/get-session';
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { getEnvelopeById } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
import { getTeamByUrl } from '@documenso/lib/server-only/team/get-team';
@@ -8,6 +9,7 @@ import { msg } from '@lingui/core/macro';
import { Trans } from '@lingui/react/macro';
import { EnvelopeType } from '@prisma/client';
import { ChevronLeftIcon } from 'lucide-react';
+import { useEffect } from 'react';
import { isRouteErrorResponse, Link, Outlet, redirect, type ShouldRevalidateFunctionArgs } from 'react-router';
import { GenericErrorLayout } from '~/components/general/generic-error-layout';
@@ -84,8 +86,18 @@ export default function TemplatesLayout() {
}
export function ErrorBoundary({ error, params }: Route.ErrorBoundaryProps) {
+ const analytics = useAnalytics();
+
const errorCode = isRouteErrorResponse(error) ? error.status : 500;
+ useEffect(() => {
+ analytics.captureException(error, {
+ source: 'editor',
+ location: 'editor_layout_boundary',
+ envelopeId: params.id,
+ });
+ }, [error]);
+
const errorCodeMap = {
404: {
subHeading: msg`404 Template not found`,
diff --git a/apps/remix/app/routes/_recipient+/_layout.tsx b/apps/remix/app/routes/_recipient+/_layout.tsx
index 940218adb..fde4a7998 100644
--- a/apps/remix/app/routes/_recipient+/_layout.tsx
+++ b/apps/remix/app/routes/_recipient+/_layout.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { useOptionalSession } from '@documenso/lib/client-only/providers/session';
import { cn } from '@documenso/ui/lib/utils';
import { Button } from '@documenso/ui/primitives/button';
@@ -5,6 +6,7 @@ import { i18n } from '@lingui/core';
import { msg } from '@lingui/core/macro';
import { Trans } from '@lingui/react/macro';
import { ChevronLeft } from 'lucide-react';
+import { useEffect } from 'react';
import { isRouteErrorResponse, Link, Outlet } from 'react-router';
import { Header as AuthenticatedHeader } from '~/components/general/app-header';
import { GenericErrorLayout } from '~/components/general/generic-error-layout';
@@ -48,8 +50,14 @@ export default function RecipientLayout({ matches }: Route.ComponentProps) {
}
export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
+ const analytics = useAnalytics();
+
const errorCode = isRouteErrorResponse(error) ? error.status : 500;
+ useEffect(() => {
+ analytics.captureException(error, { source: 'signing', location: 'recipient_layout_boundary' });
+ }, [error]);
+
return (
<GenericErrorLayout
errorCode={errorCode}
diff --git a/apps/remix/app/routes/embed+/_v0+/_layout.tsx b/apps/remix/app/routes/embed+/_v0+/_layout.tsx
index 10910f601..03a8aef40 100644
--- a/apps/remix/app/routes/embed+/_v0+/_layout.tsx
+++ b/apps/remix/app/routes/embed+/_v0+/_layout.tsx
@@ -1,3 +1,4 @@
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import {
IS_GOOGLE_SSO_ENABLED,
IS_MICROSOFT_SSO_ENABLED,
@@ -5,6 +6,7 @@ import {
OIDC_PROVIDER_LABEL,
} from '@documenso/lib/constants/auth';
import { Trans } from '@lingui/react/macro';
+import { useEffect } from 'react';
import { isRouteErrorResponse, Outlet, useRouteError } from 'react-router';
import { EmbedAuthenticationRequired } from '~/components/embed/embed-authentication-required';
@@ -47,10 +49,30 @@ export default function Layout() {
export function ErrorBoundary({ loaderData }: Route.ErrorBoundaryProps) {
const { isGoogleSSOEnabled, isMicrosoftSSOEnabled, isOIDCSSOEnabled, oidcProviderLabel } = loaderData || {};
+ const analytics = useAnalytics();
const error = useRouteError();
console.log({ routeError: error });
+ useEffect(() => {
+ const isExpectedEmbedResponse =
+ isRouteErrorResponse(error) &&
+ [
+ 'embed-authentication-required',
+ 'embed-paywall',
+ 'embed-waiting-for-turn',
+ 'embed-recipient-expired',
+ 'embed-document-rejected',
+ 'embed-document-completed',
+ ].includes(error.data?.type);
+
+ if (isExpectedEmbedResponse) {
+ return;
+ }
+
+ analytics.captureException(error, { source: 'embed', location: 'embed_layout_boundary' });
+ }, [error]);
+
if (isRouteErrorResponse(error)) {
if (error.status === 401 && error.data.type === 'embed-authentication-required') {
return (
diff --git a/apps/remix/app/routes/embed+/_v0+/direct.$token.tsx b/apps/remix/app/routes/embed+/_v0+/direct.$token.tsx
index b041e01ca..f8e6d0630 100644
--- a/apps/remix/app/routes/embed+/_v0+/direct.$token.tsx
+++ b/apps/remix/app/routes/embed+/_v0+/direct.$token.tsx
@@ -2,11 +2,13 @@ import { getOptionalSession } from '@documenso/auth/server/lib/utils/get-session
import { EnvelopeRenderProvider } from '@documenso/lib/client-only/providers/envelope-render-provider';
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
+import { captureServerEvent } from '@documenso/lib/server-only/analytics/capture-server-event';
import { getEnvelopeForDirectTemplateSigning } from '@documenso/lib/server-only/envelope/get-envelope-for-direct-template-signing';
import { getEnvelopeRequiredAccessData } from '@documenso/lib/server-only/envelope/get-envelope-required-access-data';
import { getOrganisationClaimByTeamId } from '@documenso/lib/server-only/organisation/get-organisation-claims';
import { getTemplateByDirectLinkToken } from '@documenso/lib/server-only/template/get-template-by-direct-link-token';
import { DocumentAccessAuth } from '@documenso/lib/types/document-auth';
+import { fireAndForget } from '@documenso/lib/universal/fire-and-forget';
import { extractDocumentAuthMethods } from '@documenso/lib/utils/document-auth';
import { prisma } from '@documenso/prisma';
import { data } from 'react-router';
@@ -93,6 +95,30 @@ async function handleV1Loader({ params, request }: Route.LoaderArgs) {
const fields = template.fields.filter((field) => field.recipientId === directTemplateRecipientId);
+ fireAndForget(async () => {
+ const team = await prisma.team.findFirst({
+ where: {
+ id: template.teamId,
+ },
+ select: {
+ organisationId: true,
+ },
+ });
+
+ captureServerEvent({
+ event: 'App: Embed Session Started',
+ userId: user?.id,
+ organisationId: team?.organisationId,
+ teamId: template.teamId,
+ properties: {
+ type: 'signing',
+ version: 'v0',
+ recipientId: recipient.id,
+ envelopeId: template.envelopeId,
+ },
+ });
+ });
+
return {
token,
user,
@@ -194,6 +220,30 @@ async function handleV2Loader({ params, request }: Route.LoaderArgs) {
);
}
+ fireAndForget(async () => {
+ const team = await prisma.team.findFirst({
+ where: {
+ id: envelope.teamId,
+ },
+ select: {
+ organisationId: true,
+ },
+ });
+
+ captureServerEvent({
+ event: 'App: Embed Session Started',
+ userId: user?.id,
+ organisationId: team?.organisationId,
+ teamId: envelope.teamId,
+ properties: {
+ type: 'signing',
+ version: 'v0',
+ recipientId: recipient.id,
+ envelopeId: envelope.id,
+ },
+ });
+ });
+
return {
token,
user,
diff --git a/apps/remix/app/routes/embed+/_v0+/sign.$token.tsx b/apps/remix/app/routes/embed+/_v0+/sign.$token.tsx
index 997bf4e4a..f36ced9af 100644
--- a/apps/remix/app/routes/embed+/_v0+/sign.$token.tsx
+++ b/apps/remix/app/routes/embed+/_v0+/sign.$token.tsx
@@ -2,6 +2,7 @@ import { getOptionalSession } from '@documenso/auth/server/lib/utils/get-session
import { EnvelopeRenderProvider } from '@documenso/lib/client-only/providers/envelope-render-provider';
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
+import { captureServerEvent } from '@documenso/lib/server-only/analytics/capture-server-event';
import { getDocumentAndSenderByToken } from '@documenso/lib/server-only/document/get-document-by-token';
import { viewedDocument } from '@documenso/lib/server-only/document/viewed-document';
import { getEnvelopeForRecipientSigning } from '@documenso/lib/server-only/envelope/get-envelope-for-recipient-signing';
@@ -13,6 +14,7 @@ import { getIsRecipientsTurnToSign } from '@documenso/lib/server-only/recipient/
import { getRecipientByToken } from '@documenso/lib/server-only/recipient/get-recipient-by-token';
import { getRecipientsForAssistant } from '@documenso/lib/server-only/recipient/get-recipients-for-assistant';
import { DocumentAccessAuth } from '@documenso/lib/types/document-auth';
+import { fireAndForget } from '@documenso/lib/universal/fire-and-forget';
import { isDocumentCompleted } from '@documenso/lib/utils/document';
import { extractDocumentAuthMethods } from '@documenso/lib/utils/document-auth';
import { isRecipientExpired } from '@documenso/lib/utils/recipients';
@@ -139,6 +141,30 @@ async function handleV1Loader({ params, request }: Route.LoaderArgs) {
})
: [];
+ fireAndForget(async () => {
+ const team = await prisma.team.findFirst({
+ where: {
+ id: document.teamId,
+ },
+ select: {
+ organisationId: true,
+ },
+ });
+
+ captureServerEvent({
+ event: 'App: Embed Session Started',
+ userId: user?.id,
+ organisationId: team?.organisationId,
+ teamId: document.teamId,
+ properties: {
+ type: 'signing',
+ version: 'v0',
+ recipientId: recipient.id,
+ envelopeId: document.envelopeId,
+ },
+ });
+ });
+
return {
token,
user,
@@ -272,6 +298,30 @@ async function handleV2Loader({ params, request }: Route.LoaderArgs) {
recipientAccessAuth: derivedRecipientAccessAuth,
}).catch(() => null);
+ fireAndForget(async () => {
+ const team = await prisma.team.findFirst({
+ where: {
+ id: envelope.teamId,
+ },
+ select: {
+ organisationId: true,
+ },
+ });
+
+ captureServerEvent({
+ event: 'App: Embed Session Started',
+ userId: user?.id,
+ organisationId: team?.organisationId,
+ teamId: envelope.teamId,
+ properties: {
+ type: 'signing',
+ version: 'v0',
+ recipientId: recipient.id,
+ envelopeId: envelope.id,
+ },
+ });
+ });
+
return {
token,
user,
diff --git a/apps/remix/app/routes/embed+/v1+/authoring+/_layout.tsx b/apps/remix/app/routes/embed+/v1+/authoring+/_layout.tsx
index d55fe5e5c..b048d5663 100644
--- a/apps/remix/app/routes/embed+/v1+/authoring+/_layout.tsx
+++ b/apps/remix/app/routes/embed+/v1+/authoring+/_layout.tsx
@@ -1,8 +1,11 @@
import { APP_I18N_OPTIONS } from '@documenso/lib/constants/i18n';
+import { captureServerEvent } from '@documenso/lib/server-only/analytics/capture-server-event';
import { verifyEmbeddingPresignToken } from '@documenso/lib/server-only/embedding-presign/verify-embedding-presign-token';
import { getOrganisationClaimByTeamId } from '@documenso/lib/server-only/organisation/get-organisation-claims';
import { ZBaseEmbedAuthoringSchema } from '@documenso/lib/types/embed-authoring-base-schema';
+import { fireAndForget } from '@documenso/lib/universal/fire-and-forget';
import { dynamicActivate } from '@documenso/lib/utils/i18n';
+import { prisma } from '@documenso/prisma';
import { TrpcProvider } from '@documenso/trpc/react';
import { Spinner } from '@documenso/ui/primitives/spinner';
import { Trans } from '@lingui/react/macro';
@@ -35,6 +38,40 @@ export const loader = async ({ request }: Route.LoaderArgs) => {
});
allowEmbedAuthoringWhiteLabel = organisationClaim.flags.embedAuthoringWhiteLabel ?? false;
+
+ // Derive the kind of authoring session from the child route path, e.g.
+ // /embed/v1/authoring/document/create -> resource 'document', mode 'create'.
+ // Completed and error pages are not new sessions and are skipped.
+ const [resource, mode] = url.pathname.split('/').filter(Boolean).slice(3);
+
+ const isAuthoringSession =
+ (resource === 'document' || resource === 'template') && (mode === 'create' || mode === 'edit');
+
+ if (isAuthoringSession) {
+ fireAndForget(async () => {
+ const team = await prisma.team.findFirst({
+ where: {
+ id: result.teamId,
+ },
+ select: {
+ organisationId: true,
+ },
+ });
+
+ captureServerEvent({
+ event: 'App: Embed Session Started',
+ userId: result.userId,
+ organisationId: team?.organisationId,
+ teamId: result.teamId,
+ properties: {
+ type: 'authoring',
+ version: 'v1',
+ resource,
+ mode,
+ },
+ });
+ });
+ }
}
return {
diff --git a/apps/remix/app/routes/embed+/v1+/multisign+/_index.tsx b/apps/remix/app/routes/embed+/v1+/multisign+/_index.tsx
index 4bafee963..1759a6121 100644
--- a/apps/remix/app/routes/embed+/v1+/multisign+/_index.tsx
+++ b/apps/remix/app/routes/embed+/v1+/multisign+/_index.tsx
@@ -1,8 +1,11 @@
import { getOptionalSession } from '@documenso/auth/server/lib/utils/get-session';
+import { captureServerEvent } from '@documenso/lib/server-only/analytics/capture-server-event';
import { getDocumentAndSenderByToken } from '@documenso/lib/server-only/document/get-document-by-token';
import { getOrganisationClaimByTeamId } from '@documenso/lib/server-only/organisation/get-organisation-claims';
import { getRecipientByToken } from '@documenso/lib/server-only/recipient/get-recipient-by-token';
import { ZSignDocumentEmbedDataSchema } from '@documenso/lib/types/embed-document-sign-schema';
+import { fireAndForget } from '@documenso/lib/universal/fire-and-forget';
+import { prisma } from '@documenso/prisma';
import { Trans } from '@lingui/react/macro';
import { SigningStatus } from '@prisma/client';
import { useEffect, useLayoutEffect, useState } from 'react';
@@ -42,6 +45,15 @@ export async function loader({ request }: Route.LoaderArgs) {
const firstDocument = envelopes[0]?.document;
if (!firstDocument) {
+ captureServerEvent({
+ event: 'App: Embed Session Started',
+ userId: user?.id,
+ properties: {
+ type: 'signing',
+ version: 'v1',
+ },
+ });
+
return superLoaderJson({
envelopes,
user,
@@ -55,6 +67,29 @@ export async function loader({ request }: Route.LoaderArgs) {
const allowWhitelabelling = organisationClaim.flags.embedSigningWhiteLabel;
const hidePoweredBy = organisationClaim.flags.hidePoweredBy;
+ fireAndForget(async () => {
+ const team = await prisma.team.findFirst({
+ where: {
+ id: firstDocument.teamId,
+ },
+ select: {
+ organisationId: true,
+ },
+ });
+
+ captureServerEvent({
+ event: 'App: Embed Session Started',
+ userId: user?.id,
+ organisationId: team?.organisationId,
+ teamId: firstDocument.teamId,
+ properties: {
+ type: 'signing',
+ version: 'v1',
+ envelopeId: firstDocument.envelopeId,
+ },
+ });
+ });
+
return superLoaderJson({
envelopes,
user,
diff --git a/apps/remix/app/routes/embed+/v2+/authoring+/_layout.tsx b/apps/remix/app/routes/embed+/v2+/authoring+/_layout.tsx
index eb8b81ef4..87ded302a 100644
--- a/apps/remix/app/routes/embed+/v2+/authoring+/_layout.tsx
+++ b/apps/remix/app/routes/embed+/v2+/authoring+/_layout.tsx
@@ -1,5 +1,6 @@
import { PAID_PLAN_LIMITS } from '@documenso/ee/server-only/limits/constants';
import { LimitsProvider } from '@documenso/ee/server-only/limits/provider/client';
+import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics';
import { OrganisationProvider } from '@documenso/lib/client-only/providers/organisation';
import { APP_I18N_OPTIONS } from '@documenso/lib/constants/i18n';
import { verifyEmbeddingPresignToken } from '@documenso/lib/server-only/embedding-presign/verify-embedding-presign-token';
@@ -12,7 +13,7 @@ import type { OrganisationSession } from '@documenso/trpc/server/organisation-ro
import { Spinner } from '@documenso/ui/primitives/spinner';
import { Trans } from '@lingui/react/macro';
import { OrganisationMemberRole, OrganisationType, TeamMemberRole } from '@prisma/client';
-import { useLayoutEffect, useState } from 'react';
+import { useEffect, useLayoutEffect, useState } from 'react';
import { isRouteErrorResponse, Outlet, useLoaderData } from 'react-router';
import { match } from 'ts-pattern';
@@ -166,8 +167,17 @@ export default function AuthoringLayout() {
}
export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
+ const analytics = useAnalytics();
+
const errorCode = isRouteErrorResponse(error) ? error.status : 500;
+ useEffect(() => {
+ analytics.captureException(error, {
+ source: 'embed',
+ location: 'embed_authoring_boundary',
+ });
+ }, [error]);
+
return (
<div>
{match(errorCode)
diff --git a/apps/remix/app/routes/embed+/v2+/authoring+/envelope.create._index.tsx b/apps/remix/app/routes/embed+/v2+/authoring+/envelope.create._index.tsx
index bd000b8a8..2213ae56b 100644
--- a/apps/remix/app/routes/embed+/v2+/authoring+/envelope.create._index.tsx
+++ b/apps/remix/app/routes/embed+/v2+/authoring+/envelope.create._index.tsx
@@ -1,5 +1,6 @@
import { EnvelopeEditorProvider } from '@documenso/lib/client-only/providers/envelope-editor-provider';
import type { SupportedLanguageCodes } from '@documenso/lib/constants/i18n';
+import { captureServerEvent } from '@documenso/lib/server-only/analytics/capture-server-event';
import { verifyEmbeddingPresignToken } from '@documenso/lib/server-only/embedding-presign/verify-embedding-presign-token';
import { getTeamSettings } from '@documenso/lib/server-only/team/get-team-settings';
import { ZDefaultRecipientsSchema } from '@documenso/lib/types/default-recipients';
@@ -10,6 +11,7 @@ import {
ZEmbedCreateEnvelopeAuthoringSchema,
} from '@documenso/lib/types/envelope-editor';
import type { TEnvelopeFieldAndMeta } from '@documenso/lib/types/field-meta';
+import { fireAndForget } from '@documenso/lib/universal/fire-and-forget';
import { extractDerivedDocumentMeta } from '@documenso/lib/utils/document';
import { buildEmbeddedEditorOptions, buildEmbeddedFeatures } from '@documenso/lib/utils/embed-config';
import { prisma } from '@documenso/prisma';
@@ -76,6 +78,30 @@ export const loader = async ({ request }: Route.LoaderArgs) => {
},
});
+ fireAndForget(async () => {
+ const team = result.teamId
+ ? await prisma.team.findFirst({
+ where: {
+ id: result.teamId,
+ },
+ select: {
+ organisationId: true,
+ },
+ })
+ : null;
+
+ captureServerEvent({
+ event: 'App: Embed Session Started',
+ userId: result.userId,
+ organisationId: team?.organisationId,
+ teamId: result.teamId ?? undefined,
+ properties: {
+ type: 'authoring',
+ version: 'v2',
+ },
+ });
+ });
+
return superLoaderJson({
token,
tokenUserId: result.userId,
diff --git a/apps/remix/app/routes/embed+/v2+/authoring+/envelope.edit.$id.tsx b/apps/remix/app/routes/embed+/v2+/authoring+/envelope.edit.$id.tsx
index 155f5f96c..b35c08b0c 100644
--- a/apps/remix/app/routes/embed+/v2+/authoring+/envelope.edit.$id.tsx
+++ b/apps/remix/app/routes/embed+/v2+/authoring+/envelope.edit.$id.tsx
@@ -1,5 +1,6 @@
import { EnvelopeEditorProvider } from '@documenso/lib/client-only/providers/envelope-editor-provider';
import type { SupportedLanguageCodes } from '@documenso/lib/constants/i18n';
+import { captureServerEvent } from '@documenso/lib/server-only/analytics/capture-server-event';
import { verifyEmbeddingPresignToken } from '@documenso/lib/server-only/embedding-presign/verify-embedding-presign-token';
import { getEditorEnvelopeById } from '@documenso/lib/server-only/envelope/get-editor-envelope-by-id';
import { getTeamSettings } from '@documenso/lib/server-only/team/get-team-settings';
@@ -94,6 +95,18 @@ export const loader = async ({ request, params }: Route.LoaderArgs) => {
brandingLogo = settings.brandingLogo;
}
+ captureServerEvent({
+ event: 'App: Embed Session Started',
+ userId: result.userId,
+ organisationId: envelope.team.organisationId,
+ teamId: result.teamId ?? undefined,
+ properties: {
+ type: 'authoring',
+ version: 'v2',
+ envelopeId: envelope.id,
+ },
+ });
+
return superLoaderJson({
token,
envelope,
diff --git a/packages/lib/client-only/hooks/use-analytics.ts b/packages/lib/client-only/hooks/use-analytics.ts
index ce302f7df..7d8953393 100644
--- a/packages/lib/client-only/hooks/use-analytics.ts
+++ b/packages/lib/client-only/hooks/use-analytics.ts
@@ -31,18 +31,21 @@ export function useAnalytics() {
};
/**
- * Capture an analytic event.
+ * Capture an exception event.
*
* @param error The error to capture.
- * @param properties Properties to attach to the event.
+ * @param properties Properties to attach to the event, such as `source`, `location`,
+ * `recipientId` or `envelopeId`. Never attach recipient tokens.
*/
- const captureException = (error: Error, properties?: Record<string, unknown>) => {
+ const captureException = (error: unknown, properties?: Record<string, unknown>) => {
if (!isPostHogEnabled) {
return;
}
+ const errorToCapture = error instanceof Error ? error : new Error(String(error));
+
void getPosthog().then(({ default: posthog }) => {
- posthog.captureException(error, properties);
+ posthog.captureException(errorToCapture, properties);
});
};
diff --git a/packages/lib/client-only/providers/envelope-editor-provider.tsx b/packages/lib/client-only/providers/envelope-editor-provider.tsx
index ea49f638c..70e929dbe 100644
--- a/packages/lib/client-only/providers/envelope-editor-provider.tsx
+++ b/packages/lib/client-only/providers/envelope-editor-provider.tsx
@@ -20,6 +20,7 @@ import { useSearchParams } from 'react-router';
import type { TDocumentEmailSettings } from '../../types/document-email';
import { formatDocumentsPath, formatTemplatesPath } from '../../utils/teams';
+import { useAnalytics } from '../hooks/use-analytics';
import type { TLocalField } from '../hooks/use-editor-fields';
import { useEditorFields } from '../hooks/use-editor-fields';
import { useEditorRecipients } from '../hooks/use-editor-recipients';
@@ -104,6 +105,7 @@ export const EnvelopeEditorProvider = ({
}: EnvelopeEditorProviderProps) => {
const { t } = useLingui();
const { toast } = useToast();
+ const analytics = useAnalytics();
const [_searchParams, setSearchParams] = useSearchParams();
@@ -241,6 +243,12 @@ export const EnvelopeEditorProvider = ({
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: isEmbedded ? 'embed' : 'editor',
+ location: 'autosave_recipients',
+ envelopeId: envelope.id,
+ });
+
setAutosaveError(true);
toast({
@@ -304,6 +312,12 @@ export const EnvelopeEditorProvider = ({
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: isEmbedded ? 'embed' : 'editor',
+ location: 'autosave_fields',
+ envelopeId: envelope.id,
+ });
+
setAutosaveError(true);
toast({
@@ -359,6 +373,12 @@ export const EnvelopeEditorProvider = ({
} catch (err) {
console.error(err);
+ analytics.captureException(err, {
+ source: isEmbedded ? 'embed' : 'editor',
+ location: 'autosave_meta',
+ envelopeId: envelope.id,
+ });
+
setAutosaveError(true);
toast({
diff --git a/packages/lib/server-only/analytics/capture-server-event.ts b/packages/lib/server-only/analytics/capture-server-event.ts
new file mode 100644
index 000000000..ba04ba96a
--- /dev/null
+++ b/packages/lib/server-only/analytics/capture-server-event.ts
@@ -0,0 +1,83 @@
+import crypto from 'node:crypto';
+import { PostHog } from 'posthog-node';
+
+import { env } from '../../utils/env';
+
+/**
+ * Server-side product analytics client.
+ *
+ * Uses the same PostHog project as the client-side integration
+ * (NEXT_PUBLIC_POSTHOG_KEY). Not to be confused with the self-hoster
+ * telemetry client which reports to a separate project.
+ *
+ * Events are sent directly to PostHog rather than through the `/ingest`
+ * reverse proxy used by the browser.
+ */
+const POSTHOG_HOST = 'https://eu.i.posthog.com';
+
+let client: PostHog | null | undefined;
+
+const getAnalyticsClient = () => {
+ if (client !== undefined) {
+ return client;
+ }
+
+ const apiKey = env('NEXT_PUBLIC_POSTHOG_KEY');
+
+ // Low volume events, flush immediately so nothing is lost on shutdown.
+ client = apiKey ? new PostHog(apiKey, { host: POSTHOG_HOST, flushAt: 1 }) : null;
+
+ return client;
+};
+
+type CaptureServerEventOptions = {
+ event: string;
+
+ /**
+ * The authenticated user this event belongs to, if any.
+ *
+ * Used as the distinct ID so server events line up with client-side
+ * identified users. Anonymous events (e.g. embed signer sessions) get a
+ * random distinct ID instead.
+ */
+ userId?: number;
+ organisationId?: string;
+ teamId?: number;
+ properties?: Record<string, unknown>;
+};
+
+/**
+ * Capture a product analytics event from the server.
+ *
+ * No-ops when NEXT_PUBLIC_POSTHOG_KEY is not configured. Events are captured
+ * as anonymous events (no person profile processing) to keep usage costs low,
+ * cross-referencing is done via the `userId`, `organisationId` and `teamId`
+ * event properties.
+ *
+ * Never pass recipient tokens or document contents as properties.
+ */
+export const captureServerEvent = ({
+ event,
+ userId,
+ organisationId,
+ teamId,
+ properties = {},
+}: CaptureServerEventOptions) => {
+ const posthog = getAnalyticsClient();
+
+ if (!posthog) {
+ return;
+ }
+
+ posthog.capture({
+ distinctId: userId ? String(userId) : `anon_${crypto.randomUUID()}`,
+ event,
+ properties: {
+ ...properties,
+ userId,
+ organisationId,
+ teamId,
+ $process_person_profile: false,
+ },
+ });
+};
diff --git a/packages/lib/universal/fire-and-forget.ts b/packages/lib/universal/fire-and-forget.ts
new file mode 100644
index 000000000..cdc46839b
--- /dev/null
+++ b/packages/lib/universal/fire-and-forget.ts
@@ -0,0 +1,14 @@
+/**
+ * Run a function without awaiting it or letting it affect the caller.
+ *
+ * Both synchronous throws and promise rejections are swallowed, making this
+ * safe to call from request handlers and loaders for side work such as
+ * analytics that must never block or fail the response.
+ */
+export const fireAndForget = (fn: () => unknown) => {
+ try {
+ void Promise.resolve(fn()).catch(() => null);
+ } catch {
+ // Swallow synchronous errors.
+ }
+};
diff --git a/packages/trpc/server/api-token-router/create-api-token.ts b/packages/trpc/server/api-token-router/create-api-token.ts
index a01389f48..b48322478 100644
--- a/packages/trpc/server/api-token-router/create-api-token.ts
+++ b/packages/trpc/server/api-token-router/create-api-token.ts
@@ -1,4 +1,7 @@
+import { captureServerEvent } from '@documenso/lib/server-only/analytics/capture-server-event';
import { createApiToken } from '@documenso/lib/server-only/public-api/create-api-token';
+import { fireAndForget } from '@documenso/lib/universal/fire-and-forget';
+import { prisma } from '@documenso/prisma';
import { authenticatedProcedure } from '../trpc';
import { ZCreateApiTokenRequestSchema, ZCreateApiTokenResponseSchema } from './create-api-token.types';
@@ -15,10 +18,26 @@ export const createApiTokenRoute = authenticatedProcedure
},
});
- return await createApiToken({
+ const createdToken = await createApiToken({
userId: ctx.user.id,
teamId,
tokenName,
expiresIn: expirationDate,
});
+
+ fireAndForget(async () => {
+ const team = await prisma.team.findFirst({
+ where: { id: teamId },
+ select: { organisationId: true },
+ });
+
+ captureServerEvent({
+ event: 'App: API Token Created',
+ userId: ctx.user.id,
+ teamId,
+ organisationId: team?.organisationId,
+ });
+ });
+
+ return createdToken;
});
diff --git a/packages/trpc/server/template-router/router.ts b/packages/trpc/server/template-router/router.ts
index 99d4de784..e11e1f25d 100644
--- a/packages/trpc/server/template-router/router.ts
+++ b/packages/trpc/server/template-router/router.ts
@@ -1,6 +1,7 @@
import { getServerLimits } from '@documenso/ee/server-only/limits/server';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { jobs } from '@documenso/lib/jobs/client';
+import { captureServerEvent } from '@documenso/lib/server-only/analytics/capture-server-event';
import { getDocumentWithDetailsById } from '@documenso/lib/server-only/document/get-document-with-details-by-id';
import { sendDocument } from '@documenso/lib/server-only/document/send-document';
import { convertToPdf } from '@documenso/lib/server-only/document-conversion';
@@ -21,12 +22,14 @@ import { findTemplates } from '@documenso/lib/server-only/template/find-template
import { getOrganisationTemplateById } from '@documenso/lib/server-only/template/get-organisation-template-by-id';
import { getTemplateById } from '@documenso/lib/server-only/template/get-template-by-id';
import { toggleTemplateDirectLink } from '@documenso/lib/server-only/template/toggle-template-direct-link';
+import { fireAndForget } from '@documenso/lib/universal/fire-and-forget';
import { putNormalizedPdfFileServerSide } from '@documenso/lib/universal/upload/put-file.server';
import { getPresignPostUrl } from '@documenso/lib/universal/upload/server-actions';
import { mapSecondaryIdToTemplateId } from '@documenso/lib/utils/envelope';
import { mapFieldToLegacyField } from '@documenso/lib/utils/fields';
import { mapRecipientToLegacyRecipient } from '@documenso/lib/utils/recipients';
import { mapEnvelopeToTemplateLite } from '@documenso/lib/utils/templates';
+import { prisma } from '@documenso/prisma';
import type { Envelope } from '@prisma/client';
import { DocumentDataType, EnvelopeType } from '@prisma/client';
@@ -744,7 +747,7 @@ export const templateRouter = router({
});
}
- return await createTemplateDirectLink({
+ const directLink = await createTemplateDirectLink({
userId,
teamId,
id: {
@@ -753,6 +756,25 @@ export const templateRouter = router({
},
directRecipientId,
});
+
+ fireAndForget(async () => {
+ const team = await prisma.team.findFirst({
+ where: { id: template.teamId },
+ select: { organisationId: true },
+ });
+
+ captureServerEvent({
+ event: 'App: Template Direct Link Enabled',
+ userId,
+ teamId: template.teamId,
+ organisationId: team?.organisationId,
+ properties: {
+ envelopeId: template.envelopeId,
+ },
+ });
+ });
+
+ return directLink;
}),
/**
diff --git a/packages/trpc/server/webhook-router/router.ts b/packages/trpc/server/webhook-router/router.ts
index 47fd237b4..423a39e1b 100644
--- a/packages/trpc/server/webhook-router/router.ts
+++ b/packages/trpc/server/webhook-router/router.ts
@@ -1,9 +1,12 @@
+import { captureServerEvent } from '@documenso/lib/server-only/analytics/capture-server-event';
import { createWebhook } from '@documenso/lib/server-only/webhooks/create-webhook';
import { deleteWebhookById } from '@documenso/lib/server-only/webhooks/delete-webhook-by-id';
import { editWebhook } from '@documenso/lib/server-only/webhooks/edit-webhook';
import { getWebhookById } from '@documenso/lib/server-only/webhooks/get-webhook-by-id';
import { getWebhooksByTeamId } from '@documenso/lib/server-only/webhooks/get-webhooks-by-team-id';
import { triggerTestWebhook } from '@documenso/lib/server-only/webhooks/trigger-test-webhook';
+import { fireAndForget } from '@documenso/lib/universal/fire-and-forget';
+import { prisma } from '@documenso/prisma';
import { authenticatedProcedure, router } from '../trpc';
import { findWebhookCallsRoute } from './find-webhook-calls';
@@ -51,7 +54,7 @@ export const webhookRouter = router({
createWebhook: authenticatedProcedure.input(ZCreateWebhookRequestSchema).mutation(async ({ input, ctx }) => {
const { enabled, eventTriggers, secret, webhookUrl } = input;
- return await createWebhook({
+ const webhook = await createWebhook({
enabled,
secret,
webhookUrl,
@@ -59,6 +62,25 @@ export const webhookRouter = router({
teamId: ctx.teamId,
userId: ctx.user.id,
});
+
+ fireAndForget(async () => {
+ const team = await prisma.team.findFirst({
+ where: { id: ctx.teamId },
+ select: { organisationId: true },
+ });
+
+ captureServerEvent({
+ event: 'App: Webhook Created',
+ userId: ctx.user.id,
+ teamId: ctx.teamId,
+ organisationId: team?.organisationId,
+ properties: {
+ triggerCount: eventTriggers.length,
+ },
+ });
+ });
+
+ return webhook;
}),
deleteWebhook: authenticatedProcedure.input(ZDeleteWebhookRequestSchema).mutation(async ({ input, ctx }) => {