Skip to content

Commit

Permalink
refactor: update urls (#10409)
Browse files Browse the repository at this point in the history
* refactor: update urls

* fix: mixpanel
  • Loading branch information
guanbinrui committed Aug 15, 2023
1 parent 020e45c commit b48771e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/flags/src/flags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const flags = {
mixpanel_enabled: prodOnly,
mixpanel_event_enabled: prodOnly,
mixpanel_exception_enabled: prodOnly,
mixpanel_project_token: 'b815b822fd131650e92ff8539eb5e793',

// wallet connect
wc_v1_bridge_url: 'https://bridge.walletconnect.org',
Expand Down
42 changes: 17 additions & 25 deletions packages/web3-telemetry/src/apis/Mixpanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import {
import type { EventID } from '../types/index.js'
import { getABTestSeed, joinsABTest } from '../entry-helpers.js'

const PROJECT_TOKEN = 'b815b822fd131650e92ff8539eb5e793'

function resolveCORS(url: string) {
return `https://cors-next.r2d2.to/?${encodeURIComponent(url)}`
}

export interface Event {
event: EventID

Expand Down Expand Up @@ -92,7 +86,10 @@ export interface Event {
export class MixpanelEventAPI {
private lastEvent?: Event

constructor(private env: BuildInfoFile) {}
constructor(
private token: string,
private env: BuildInfoFile,
) {}

private attachEvent(event: Event): Event {
return (this.lastEvent = {
Expand Down Expand Up @@ -136,12 +133,10 @@ export class MixpanelEventAPI {
const event = this.attachEvent(_)

const response = await fetch(
resolveCORS(
urlcat('https://api.mixpanel.com/import', {
strict: 1,
project_id: PROJECT_TOKEN,
}),
),
urlcat('https://mixpanel.r2d2.to/import', {
strict: 1,
project_id: this.token,
}),
{
method: 'POST',
headers: {
Expand All @@ -167,12 +162,10 @@ export class MixpanelEventAPI {
const event = this.attachEvent(_)

const response = await fetch(
resolveCORS(
urlcat('https://api.mixpanel.com/track', {
// in the debug mode the API returns more information
verbose: process.env.NODE_ENV === 'development' ? 1 : 0,
}),
),
urlcat('https://mixpanel.r2d2.to/track', {
// in the debug mode the API returns more information
verbose: process.env.NODE_ENV === 'development' ? 1 : 0,
}),
{
method: 'POST',
headers: {
Expand All @@ -185,7 +178,7 @@ export class MixpanelEventAPI {
event: event.event,
properties: {
...event.properties,
token: PROJECT_TOKEN,
token: this.token,
},
},
]),
Expand All @@ -194,14 +187,13 @@ export class MixpanelEventAPI {

const json:
| {
error: string
error?: string
status: string
}
| 0
| 1 = await response.json()
| 0 = await response.json()

if (json === 0) throw new Error('No data objects in the body are invalid.')
if (json === 1) return
return new Error(json.error)
if (typeof json === 'object' && json.error) throw new Error(json.error)
return
}
}
2 changes: 1 addition & 1 deletion packages/web3-telemetry/src/providers/Mixpanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class MixpanelAPI extends BaseAPI<Event, never> implements Provider {
super(Flags.mixpanel_sample_rate)
}

private eventAPI = new MixpanelEventAPI(this.env)
private eventAPI = new MixpanelEventAPI(Flags.mixpanel_project_token, this.env)

private createEvent(options: EventOptions): Event {
return omitBy<Event>(
Expand Down

0 comments on commit b48771e

Please sign in to comment.