Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.

Commit a7dcbd9

Browse files
committed
Add posthog event capturing for quickconnect demo
1 parent cf395ae commit a7dcbd9

7 files changed

Lines changed: 134 additions & 14 deletions

File tree

frontend/package-lock.json

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "frontend",
33
"version": "0.1.0",
44
"private": true,
5-
"engines" : {
6-
"node" : "^16.0.0"
5+
"engines": {
6+
"node": "^16.0.0"
77
},
88
"dependencies": {
99
"@kyper/button": "^3.1.0",
@@ -16,6 +16,7 @@
1616
"@testing-library/jest-dom": "^5.16.2",
1717
"@testing-library/react": "^12.1.4",
1818
"@testing-library/user-event": "^13.5.0",
19+
"posthog-js": "^1.33.0",
1920
"react": "^16.14.0",
2021
"react-dom": "^16.14.0",
2122
"react-scripts": "5.0.0",

frontend/src/App.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
import { useState } from 'react';
2-
31
import LaunchButton from "./components/LaunchButton";
42
import UserEndpoints from "./components/UserEndpoints";
3+
import posthog from 'posthog-js';
54

5+
import { useEffect, useState } from 'react';
66

77
function App() {
88
const [userGuid, setUserGuid] = useState(null);
99
const [memberGuid, setMemberGuid] = useState(null);
1010

11+
useEffect(() => {
12+
posthog.init('phc_kequjnByvXoLjRawiaNEMoai4tcBWsi9iLlIWPYB7JS', { api_host: 'https://app.posthog.com', autocapture: false })
13+
}, [])
14+
1115

1216
return (
1317
<div className="App">
1418
<div className="body">
1519
{userGuid === null && memberGuid === null ? (
16-
<LaunchButton setUserGuid={setUserGuid} setMemberGuid={setMemberGuid} />
20+
<LaunchButton setUserGuid={setUserGuid} setMemberGuid={setMemberGuid} posthog={posthog} />
1721
) :
1822
(
1923
<UserEndpoints userGuid={userGuid} memberGuid={memberGuid} />

frontend/src/components/LaunchButton.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Dots } from '@kyper/progressindicators';
99
import { Text } from '@kyper/text'
1010
import { Trash } from '@kyper/icon/Trash'
1111

12-
function LaunchButton({ setUserGuid, setMemberGuid }) {
12+
function LaunchButton({ setUserGuid, setMemberGuid, posthog }) {
1313
const [connectWidgetUrl, setConnectWidgetUrl] = useState("");
1414
const [errorMessage, setErrorMessage] = useState(null);
1515
const [isLoading, setIsLoading] = useState(false);
@@ -218,6 +218,14 @@ function LaunchButton({ setUserGuid, setMemberGuid }) {
218218
widgetUrl={connectWidgetUrl}
219219
onEvent={(event) => {
220220
console.log('MX PostMessage: ', event)
221+
const user_guid = event.metadata.user_guid;
222+
if (user_guid) {
223+
console.log('identifying user on posthog');
224+
posthog.identify(user_guid);
225+
}
226+
227+
posthog.capture(`Widget Event: ${event.type}`);
228+
221229
if (event.type === 'mx/connect/memberConnected') {
222230
setUserGuid(event.metadata.user_guid)
223231
setMemberGuid(event.metadata.member_guid)

ruby/Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ gem 'dotenv', '~> 2.7'
99

1010
gem 'mx-platform-ruby'
1111

12+
gem 'posthog-ruby'
13+
1214
group :test do
1315
# Test it with RSpec (BDD for Ruby)
1416
gem 'rspec-core'

ruby/Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ GEM
22
remote: https://rubygems.org/
33
specs:
44
colorize (0.8.1)
5+
concurrent-ruby (1.1.9)
56
dotenv (2.7.6)
67
faraday (1.10.0)
78
faraday-em_http (~> 1.0)
@@ -38,6 +39,8 @@ GEM
3839
ruby2_keywords (~> 0.0.1)
3940
mx-platform-ruby (0.13.1)
4041
faraday (~> 1.0, >= 1.0.1)
42+
posthog-ruby (2.0.0)
43+
concurrent-ruby (~> 1, < 1.1.10)
4144
rack (2.2.3.1)
4245
rack-protection (2.2.0)
4346
rack
@@ -62,6 +65,7 @@ DEPENDENCIES
6265
dotenv (~> 2.7)
6366
httparty (~> 0.20.0)
6467
mx-platform-ruby
68+
posthog-ruby
6569
rack (>= 2.2.3.1)
6670
rspec-core
6771
sinatra

ruby/app.rb

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
require 'base64'
99
require 'date'
1010
require 'json'
11+
require 'mx-platform-ruby'
12+
require 'posthog'
1113
require 'sinatra'
1214
require 'sinatra/cross_origin'
13-
require 'mx-platform-ruby'
1415

1516
set :port, ENV['APP_PORT'] || 8000
1617

@@ -38,6 +39,12 @@
3839
api_client.default_headers['Accept'] = 'application/vnd.mx.api.v1+json'
3940
mx_platform_api = ::MxPlatformRuby::MxPlatformApi.new(api_client)
4041

42+
posthog = PostHog::Client.new({
43+
api_key: ENV['POST_HOG_API_KEY'],
44+
host: ENV['POST_HOG_HOST'], # You can remove this line if you're using https://app.posthog.com
45+
on_error: proc { |_status, msg| print msg }
46+
})
47+
4148
# Checks the env file and production config if in production mode
4249
test_config(mx_platform_api)
4350

@@ -69,13 +76,11 @@ def create_user(user_id, mx_platform_api)
6976
end
7077

7178
delete '/api/user/:guid' do
72-
begin
73-
mx_platform_api.delete_user(params[:guid])
74-
{ :user_guid => params[:guid] }.to_json
75-
rescue ::MxPlatformRuby::ApiError => e
76-
puts "Error when calling MxPlatformApi->delete_user: #{e.message}"
77-
[400, e.response_body]
78-
end
79+
mx_platform_api.delete_user(params[:guid])
80+
{ user_guid: params[:guid] }.to_json
81+
rescue ::MxPlatformRuby::ApiError => e
82+
puts "Error when calling MxPlatformApi->delete_user: #{e.message}"
83+
[400, e.response_body]
7984
end
8085

8186
post '/api/get_mxconnect_widget_url' do
@@ -87,6 +92,13 @@ def create_user(user_id, mx_platform_api)
8792

8893
# create user if no user_guid given
8994
user_guid = data['user_guid'].nil? ? create_user(external_id, mx_platform_api) : data['user_guid']
95+
posthog.capture({
96+
distinct_id: user_guid,
97+
event: 'widget_request_api',
98+
properties: {
99+
test: '123'
100+
}
101+
})
90102

91103
request_body = ::MxPlatformRuby::WidgetRequestBody.new(
92104
widget_url: ::MxPlatformRuby::WidgetRequest.new(
@@ -111,12 +123,23 @@ def create_user(user_id, mx_platform_api)
111123
get '/users/:user_guid/members/:member_guid/verify' do
112124
content_type :json
113125
begin
126+
posthog.capture({
127+
distinct_id: params[:user_guid],
128+
event: 'begin verify job'
129+
})
114130
# if widget was not in verification mode
115131
# mx_platform_api.verify_member(member_guid, user_guid)
116132
# poll member status answer MFAs
117133
response = mx_platform_api.list_account_numbers_by_member(params[:member_guid], params[:user_guid])
118134
response.to_hash.to_json
119135
rescue ::MxPlatformRuby::ApiError => e
136+
posthog.capture({
137+
distinct_id: params[:user_guid],
138+
event: 'verify failed',
139+
properties: {
140+
error_message: "Error when calling MxPlatformApi->list_account_numbers_by_member: #{e.message}"
141+
}
142+
})
120143
puts "Error when calling MxPlatformApi->list_account_numbers_by_member: #{e.message}"
121144
[400, e.response_body]
122145
end
@@ -125,6 +148,10 @@ def create_user(user_id, mx_platform_api)
125148
post '/users/:user_guid/members/:member_guid/identify' do
126149
content_type :json
127150
begin
151+
posthog.capture({
152+
distinct_id: params[:user_guid],
153+
event: 'begin identify job'
154+
})
128155
response = mx_platform_api.identify_member(
129156
params[:member_guid],
130157
params[:user_guid]
@@ -143,6 +170,10 @@ def create_user(user_id, mx_platform_api)
143170
params[:member_guid],
144171
params[:user_guid]
145172
)
173+
posthog.capture({
174+
distinct_id: params[:user_guid],
175+
event: 'finish identify job', properties: { response: response.to_hash }
176+
})
146177
response.to_hash.to_json
147178
rescue ::MxPlatformRuby::ApiError => e
148179
puts "Error when calling MxPlatformApi->list_account_owners_by_member: #{e.message}"
@@ -154,6 +185,10 @@ def create_user(user_id, mx_platform_api)
154185
content_type :json
155186
begin
156187
response = mx_platform_api.list_user_accounts(params[:user_guid])
188+
posthog.capture({
189+
distinct_id: params[:user_guid],
190+
event: 'finish check_balance job', properties: { response: response.to_hash }
191+
})
157192
response.to_hash.to_json
158193
rescue ::MxPlatformRuby::ApiError => e
159194
puts "Error when calling MxPlatformApi->list_user_accounts: #{e.message}"
@@ -164,6 +199,10 @@ def create_user(user_id, mx_platform_api)
164199
post '/users/:user_guid/members/:member_guid/check_balance' do
165200
content_type :json
166201
begin
202+
posthog.capture({
203+
distinct_id: params[:user_guid],
204+
event: 'begin check_balance job'
205+
})
167206
response = mx_platform_api.check_balances(
168207
params[:member_guid],
169208
params[:user_guid]
@@ -182,6 +221,10 @@ def create_user(user_id, mx_platform_api)
182221
params[:member_guid],
183222
params[:user_guid]
184223
)
224+
posthog.capture({
225+
distinct_id: params[:user_guid],
226+
event: 'getting transactions', properties: { response: response.to_hash }
227+
})
185228
response.to_hash.to_json
186229
rescue ::MxPlatformRuby::ApiError => e
187230
puts "Error when calling MxPlatformApi->list_transactions_by_member: #{e.message}"
@@ -196,6 +239,10 @@ def create_user(user_id, mx_platform_api)
196239
params[:member_guid],
197240
params[:user_guid]
198241
)
242+
posthog.capture({
243+
distinct_id: params[:user_guid],
244+
event: 'getting member status', properties: { response: response.to_hash }
245+
})
199246
response.to_hash.to_json
200247
rescue ::MxPlatformRuby::ApiError => e
201248
puts "Error when calling MxPlatformApi->read_member_status: #{e.message}"

0 commit comments

Comments
 (0)