-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add posthog dep * feat: posthog analytics * feat: user events * fix: nil tenant * feat: tenant ident * chore: linting * Update pkg/analytics/posthog/posthog.go Co-authored-by: abelanger5 <[email protected]> * fix: typo --------- Co-authored-by: abelanger5 <[email protected]>
- Loading branch information
1 parent
f16a9cd
commit ca68eee
Showing
13 changed files
with
190 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package analytics | ||
|
||
type Analytics interface { | ||
Enqueue(event string, userId string, tenantId *string, data map[string]interface{}) | ||
Tenant(tenantId string, data map[string]interface{}) | ||
} | ||
|
||
type NoOpAnalytics struct{} | ||
|
||
func (a NoOpAnalytics) Enqueue(event string, userId string, tenantId *string, data map[string]interface{}) { | ||
} | ||
func (a NoOpAnalytics) Tenant(tenantId string, data map[string]interface{}) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package posthog | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/posthog/posthog-go" | ||
) | ||
|
||
type PosthogAnalytics struct { | ||
client *posthog.Client | ||
} | ||
|
||
type PosthogAnalyticsOpts struct { | ||
ApiKey string | ||
Endpoint string | ||
} | ||
|
||
func NewPosthogAnalytics(opts *PosthogAnalyticsOpts) (*PosthogAnalytics, error) { | ||
if opts.ApiKey == "" || opts.Endpoint == "" { | ||
return nil, fmt.Errorf("api key and endpoint are required if posthog is enabled") | ||
} | ||
|
||
phClient, err := posthog.NewWithConfig( | ||
opts.ApiKey, | ||
posthog.Config{ | ||
Endpoint: opts.Endpoint, | ||
}, | ||
) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed to create posthog client: %w", err) | ||
} | ||
|
||
return &PosthogAnalytics{ | ||
client: &phClient, | ||
}, nil | ||
} | ||
|
||
func (p *PosthogAnalytics) Enqueue(event string, userId string, tenantId *string, data map[string]interface{}) { | ||
|
||
var group posthog.Groups | ||
|
||
if tenantId != nil { | ||
group = posthog.NewGroups().Set("tenant", *tenantId) | ||
} | ||
|
||
var _ = (*p.client).Enqueue(posthog.Capture{ | ||
DistinctId: userId, | ||
Event: event, | ||
Properties: map[string]interface{}{ | ||
"$set": data, | ||
}, | ||
Groups: group, | ||
}) | ||
} | ||
|
||
func (p *PosthogAnalytics) Tenant(tenantId string, data map[string]interface{}) { | ||
var _ = (*p.client).Enqueue(posthog.GroupIdentify{ | ||
Type: "tenant", | ||
Key: tenantId, | ||
Properties: map[string]interface{}{ | ||
"$set": data, | ||
}, | ||
}) | ||
} |