Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posthog analytics Integration for iq.wiki backend #581

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/App/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import TagRepository from './Tag/tag.repository'
import EventsService from './Wiki/events.service'
import AppService from './app.service'
import WikiController from './Wiki/controllers/wiki.controller'
import { PosthogModule } from 'nestjs-posthog'

// istanbul ignore next
@Module({
Expand Down Expand Up @@ -93,6 +94,24 @@ import WikiController from './Wiki/controllers/wiki.controller'
],
},
}),
PosthogModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
const apiKey = configService.get<string>('POSTHOG_API_KEY')
const host = configService.get<string>('POSTHOG_API_URL')
if (!apiKey ||!host) {
console.error('Posthog configuration is missing apiKey or host')
}
return {
apiKey: apiKey || '',
options: {
host,
},
mock: true,
}
},
}),
SitemapModule,
MailerModule,
httpModule(20000),
Expand Down
90 changes: 57 additions & 33 deletions src/Relayer/services/relayer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import WikiAbi from '../utils/wiki.abi'
import USER_ACTIVITY_LIMIT from '../../globalVars'
import ActivityRepository from '../../App/Activities/activity.repository'
import AppService from '../../App/app.service'
import { PosthogService } from 'nestjs-posthog'

@Injectable()
class RelayerService {
Expand All @@ -24,6 +25,7 @@ class RelayerService {
private configService: ConfigService,
private httpService: HttpService,
private activityRepository: ActivityRepository,
private posthogService: PosthogService,
) {
this.signer = this.getRelayerInstance()
this.wikiInstance = this.getWikiContractInstance(this.signer)
Expand Down Expand Up @@ -120,40 +122,62 @@ class RelayerService {
}

let result
if (this.appService.apiLevel() !== 'prod') {
const txConfig = {
gasPrice: ethers.utils.parseUnits('0.7', 'gwei'),
try {
if (this.appService.apiLevel() !== 'prod') {
const txConfig = {
gasPrice: ethers.utils.parseUnits('0.7', 'gwei'),
}
result = await this.wikiInstance.postBySig(
ipfs,
userAddr,
deadline,
v,
r,
s,
txConfig,
)
} else {
const gas = await this.getUpdatedGas()

const txConfig = this.appService.privateSigner()
? {
gasPrice: ethers.utils.parseUnits(gas, 'gwei'),
gasLimit: 50000,
}
: {
gasLimit: 50000,
}

result = await this.wikiInstance.postBySig(
ipfs,
userAddr,
deadline,
v,
r,
s,
txConfig,
)
}
result = await this.wikiInstance.postBySig(
ipfs,
userAddr,
deadline,
v,
r,
s,
txConfig,
)
} else {
const gas = await this.getUpdatedGas()

const txConfig = this.appService.privateSigner()
? {
gasPrice: ethers.utils.parseUnits(gas, 'gwei'),
gasLimit: 50000,
}
: {
gasLimit: 50000,
}

result = await this.wikiInstance.postBySig(
ipfs,
userAddr,
deadline,
v,
r,
s,
txConfig,
)
this.posthogService.capture({
kesar marked this conversation as resolved.
Show resolved Hide resolved
distinctId: userAddr,
event: 'Successful Transaction',
properties: {
ipfs,
userAddr,
result,
},
})
} catch (error) {
this.posthogService.capture({
kesar marked this conversation as resolved.
Show resolved Hide resolved
distinctId: userAddr,
event: 'Failed Transaction',
properties: {
ipfs,
userAddr,
error,
},
})
throw error
}
return result
}
Expand Down
Loading