-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Mixpanel analytics provider (#383)
- Loading branch information
Showing
5 changed files
with
87 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,22 @@ | ||
import { camelcase, titleize } from '../../render/Helpers/String' | ||
import { UserEventParams } from '../../users/UserEvent' | ||
import { snakeCase } from '../../utilities' | ||
import Provider from '../Provider' | ||
|
||
export type AnalyticsProviderName = 'segment' | ||
export type AnalyticsProviderName = 'segment' | 'mixpanel' | 'posthog' | ||
|
||
export type AnalyticsUserEvent = UserEventParams & { external_id: string } | ||
|
||
export type Convention = 'snake_case' | 'camel_case' | 'title_case' | ||
|
||
export abstract class AnalyticsProvider extends Provider { | ||
abstract track(event: AnalyticsUserEvent): Promise<void> | ||
|
||
tranformEventName(event: string, convention: Convention) { | ||
switch (convention) { | ||
case 'camel_case': return camelcase(event) | ||
case 'snake_case': return snakeCase(event) | ||
case 'title_case': return titleize(event) | ||
} | ||
} | ||
} |
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,70 @@ | ||
import { ProviderControllers, ProviderParams, ProviderSchema } from '../Provider' | ||
import { AnalyticsProvider, AnalyticsUserEvent } from './AnalyticsProvider' | ||
import { createController } from '../ProviderService' | ||
import { logger } from '../../config/logger' | ||
|
||
type Convention = 'snake_case' | 'camel_case' | 'title_case' | ||
|
||
interface MixpanelDataParams { | ||
project_token: string | ||
region?: string | ||
is_default: boolean | ||
event_name_convention: Convention | ||
} | ||
|
||
interface MixpanelProviderParams extends ProviderParams { | ||
data: MixpanelDataParams | ||
} | ||
|
||
export default class MixpanelAnalyticsProvider extends AnalyticsProvider { | ||
project_token!: string | ||
region?: string | ||
event_name_convention: Convention = 'snake_case' | ||
|
||
static namespace = 'mixpanel' | ||
static meta = { | ||
name: 'Mixpanel', | ||
url: 'https://mixpanel.com', | ||
icon: 'https://parcelvoy.com/providers/mixpanel.svg', | ||
} | ||
|
||
static schema = ProviderSchema<MixpanelProviderParams, MixpanelDataParams>('mixpanelAnalyticsProviderParams', { | ||
type: 'object', | ||
required: ['project_token', 'is_default'], | ||
properties: { | ||
project_token: { type: 'string' }, | ||
region: { type: 'string', nullable: true, enum: ['api', 'api-eu'] }, | ||
is_default: { type: 'boolean' }, | ||
event_name_convention: { type: 'string', enum: ['snake_case', 'camel_case', 'title_case'] }, | ||
}, | ||
}) | ||
|
||
async track(event: AnalyticsUserEvent) { | ||
|
||
const response = await fetch(`https://${this.region}.mixpanel.com/import`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Basic ${btoa(this.project_token + ':')}`, | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'parcelvoy/v1 (+https://github.com/parcelvoy/platform)', | ||
}, | ||
body: JSON.stringify([{ | ||
event: this.tranformEventName(event.name, this.event_name_convention), | ||
properties: { | ||
...event.data, | ||
distinct_id: event.external_id, | ||
}, | ||
}]), | ||
}) | ||
|
||
if (!response.ok) { | ||
const responseBody = await response.json() | ||
logger.error('Mixpanel error', responseBody) | ||
} | ||
} | ||
|
||
static controllers(): ProviderControllers { | ||
return { admin: createController('analytics', this) } | ||
} | ||
} |
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