Skip to content

Commit

Permalink
Add payment method analytics (#4477)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo authored Apr 1, 2024
1 parent befc775 commit bb6c0a1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/server/handlers/billing/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/porter-dev/porter/api/server/shared/config"
"github.com/porter-dev/porter/api/server/shared/requestutils"
"github.com/porter-dev/porter/api/types"
"github.com/porter-dev/porter/internal/analytics"
"github.com/porter-dev/porter/internal/models"
"github.com/porter-dev/porter/internal/telemetry"
)
Expand Down Expand Up @@ -70,6 +71,7 @@ func (c *SetDefaultBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
ctx, span := telemetry.NewSpan(r.Context(), "set-default-billing-endpoint")
defer span.End()

user, _ := r.Context().Value(types.UserScope).(*models.User)
proj, _ := ctx.Value(types.ProjectScope).(*models.Project)

paymentMethodID, reqErr := requestutils.GetURLParamString(r, types.URLParamPaymentMethodID)
Expand All @@ -92,5 +94,9 @@ func (c *SetDefaultBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
telemetry.AttributeKV{Key: "payment-method-id", Value: paymentMethodID},
)

_ = c.Config().AnalyticsClient.Track(analytics.PaymentMethodAttachedTrack(&analytics.PaymentMethodCreateDeleteTrackOpts{
ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(user.ID, proj.ID),
}))

c.WriteResult(w, r, "")
}
9 changes: 9 additions & 0 deletions api/server/handlers/billing/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/porter-dev/porter/api/server/shared/config"
"github.com/porter-dev/porter/api/server/shared/requestutils"
"github.com/porter-dev/porter/api/types"
"github.com/porter-dev/porter/internal/analytics"
"github.com/porter-dev/porter/internal/models"
"github.com/porter-dev/porter/internal/telemetry"
)

Expand All @@ -32,6 +34,9 @@ func (c *DeleteBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ctx, span := telemetry.NewSpan(r.Context(), "delete-billing-endpoint")
defer span.End()

user, _ := r.Context().Value(types.UserScope).(*models.User)
proj, _ := ctx.Value(types.ProjectScope).(*models.Project)

paymentMethodID, reqErr := requestutils.GetURLParamString(r, types.URLParamPaymentMethodID)
if reqErr != nil {
err := telemetry.Error(ctx, span, reqErr, "error deleting payment method")
Expand All @@ -50,5 +55,9 @@ func (c *DeleteBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
telemetry.AttributeKV{Key: "payment-method-id", Value: paymentMethodID},
)

_ = c.Config().AnalyticsClient.Track(analytics.PaymentMethodDettachedTrack(&analytics.PaymentMethodCreateDeleteTrackOpts{
ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(user.ID, proj.ID),
}))

c.WriteResult(w, r, "")
}
5 changes: 5 additions & 0 deletions internal/analytics/track_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ const (

PorterAppUpdateFailure SegmentEvent = "Porter App Update Failure"

// billing

PaymentMethodAttached SegmentEvent = "Credit Card Linked"
PaymentMethodDettached SegmentEvent = "Credit Card Removed"

// new infra flow

// CloudProviderPermissionsGranted is a segment event that is triggered when a user grants cloud provider permissions
Expand Down
38 changes: 38 additions & 0 deletions internal/analytics/tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,44 @@ func ProjectDeleteTrack(opts *ProjectCreateDeleteTrackOpts) segmentTrack {
)
}

// PaymentMethodCreateDeleteTrackOpts are the options for creating a track when a payment method is attached/detached
type PaymentMethodCreateDeleteTrackOpts struct {
*ProjectScopedTrackOpts

Email string
FirstName string
LastName string
CompanyName string
}

// PaymentMethodAttachedTrack returns a track for when a payment method is attached
func PaymentMethodAttachedTrack(opts *PaymentMethodCreateDeleteTrackOpts) segmentTrack {
additionalProps := make(map[string]interface{})

additionalProps["email"] = opts.Email
additionalProps["name"] = opts.FirstName + " " + opts.LastName
additionalProps["company"] = opts.CompanyName

return getSegmentProjectTrack(
opts.ProjectScopedTrackOpts,
getDefaultSegmentTrack(additionalProps, PaymentMethodAttached),
)
}

// PaymentMethodDettachedTrack returns a track for when a payment method is detached
func PaymentMethodDettachedTrack(opts *PaymentMethodCreateDeleteTrackOpts) segmentTrack {
additionalProps := make(map[string]interface{})

additionalProps["email"] = opts.Email
additionalProps["name"] = opts.FirstName + " " + opts.LastName
additionalProps["company"] = opts.CompanyName

return getSegmentProjectTrack(
opts.ProjectScopedTrackOpts,
getDefaultSegmentTrack(additionalProps, PaymentMethodDettached),
)
}

// ClusterDeleteTrackOpts are the options for creating a track when a cluster is deleted
type ClusterDeleteTrackOpts struct {
*ProjectScopedTrackOpts
Expand Down

0 comments on commit bb6c0a1

Please sign in to comment.