-
Notifications
You must be signed in to change notification settings - Fork 43
feat(wallet-dashboard): Integrate Amplitude #4930
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c5bb370
feat(dashboard): Integrate Amplitude
marc2332 5745b53
manypkg
marc2332 ad7f78f
prettier fix dashboard
marc2332 02efa4e
missing header
marc2332 f00759a
Merge branch 'develop' into tooling-dashboard/integrate-amplitude
marc2332 56ea0b2
Merge branch 'develop' into tooling-dashboard/integrate-amplitude
marc2332 3a46540
feat(dashboard): create prependLicense file in dashboard
5959902
fix(explorer): update paths to pull amplitude in explorer scripts
1a39ae4
Merge branches 'tooling-dashboard/integrate-amplitude' and 'develop' …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,14 @@ | ||
{ | ||
"Zone": "eu", | ||
"OrgId": "100007351", | ||
"WorkspaceId": "72fb85fc-aed9-46ef-83a2-9345888a1678", | ||
"SourceId": "ca44ad20-3cfd-4618-aa11-4b8befb0b123", | ||
"Branch": "main", | ||
"Version": "1.0.0", | ||
"VersionId": "954386e3-441d-4aa5-b9ad-1f01e0a20e55", | ||
"Runtime": "browser:typescript-ampli-v2", | ||
"Platform": "Browser", | ||
"Language": "TypeScript", | ||
"SDK": "@amplitude/analytics-browser@^1.0", | ||
"Path": "./lib/utils/analytics/ampli" | ||
} |
This file contains hidden or 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 hidden or 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,24 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
'use client'; | ||
|
||
import { ampli, initAmplitude } from '@/lib/utils/analytics'; | ||
import { useEffect } from 'react'; | ||
|
||
async function load() { | ||
await initAmplitude(); | ||
await ampli.openedWalletDashboard({ | ||
pagePath: location.pathname, | ||
pagePathFragment: `${location.pathname}${location.search}${location.hash}`, | ||
walletDashboardRev: process.env.NEXT_PUBLIC_DASHBOARD_REV, | ||
}); | ||
} | ||
|
||
export function Amplitude() { | ||
useEffect(() => { | ||
load(); | ||
}, []); | ||
|
||
return null; | ||
} |
This file contains hidden or 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
221 changes: 221 additions & 0 deletions
221
apps/wallet-dashboard/lib/utils/analytics/ampli/index.ts
This file contains hidden or 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,221 @@ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
// @ts-nocheck | ||
/** | ||
* Ampli - A strong typed wrapper for your Analytics | ||
* | ||
* This file is generated by Amplitude. | ||
* To update run 'ampli pull web' | ||
* | ||
* Required dependencies: @amplitude/analytics-browser@^1.3.0 | ||
* Tracking Plan Version: 1 | ||
* Build: 1.0.0 | ||
* Runtime: browser:typescript-ampli-v2 | ||
* | ||
* [View Tracking Plan](https://data.eu.amplitude.com/iota-foundation/IOTA%20Wallet%20Dashboard/events/main/latest) | ||
* | ||
* [Full Setup Instructions](https://data.eu.amplitude.com/iota-foundation/IOTA%20Wallet%20Dashboard/implementation/web) | ||
*/ | ||
|
||
import * as amplitude from '@amplitude/analytics-browser'; | ||
|
||
export type Environment = 'iotawalletdashboard'; | ||
|
||
export const ApiKey: Record<Environment, string> = { | ||
iotawalletdashboard: '4d570cb7dc58e267349bde33f7b8bdeb', | ||
}; | ||
|
||
/** | ||
* Default Amplitude configuration options. Contains tracking plan information. | ||
*/ | ||
export const DefaultConfiguration: BrowserOptions = { | ||
plan: { | ||
version: '1', | ||
branch: 'main', | ||
source: 'web', | ||
versionId: '954386e3-441d-4aa5-b9ad-1f01e0a20e55', | ||
}, | ||
...{ | ||
ingestionMetadata: { | ||
sourceName: 'browser-typescript-ampli', | ||
sourceVersion: '2.0.0', | ||
}, | ||
}, | ||
serverZone: amplitude.Types.ServerZone.EU, | ||
}; | ||
|
||
export interface LoadOptionsBase { | ||
disabled?: boolean; | ||
} | ||
|
||
export type LoadOptionsWithEnvironment = LoadOptionsBase & { | ||
environment: Environment; | ||
client?: { configuration?: BrowserOptions }; | ||
}; | ||
export type LoadOptionsWithApiKey = LoadOptionsBase & { | ||
client: { apiKey: string; configuration?: BrowserOptions }; | ||
}; | ||
export type LoadOptionsWithClientInstance = LoadOptionsBase & { | ||
client: { instance: BrowserClient }; | ||
}; | ||
|
||
export type LoadOptions = | ||
| LoadOptionsWithEnvironment | ||
| LoadOptionsWithApiKey | ||
| LoadOptionsWithClientInstance; | ||
|
||
export interface OpenedWalletDashboardProperties { | ||
activeNetwork?: string; | ||
activeOrigin?: string; | ||
pagePath?: string; | ||
pagePathFragment?: string; | ||
walletDashboardRev?: string; | ||
} | ||
|
||
export class OpenedWalletDashboard implements BaseEvent { | ||
event_type = 'Opened wallet dashboard'; | ||
|
||
constructor(public event_properties?: OpenedWalletDashboardProperties) { | ||
this.event_properties = event_properties; | ||
} | ||
} | ||
|
||
export type PromiseResult<T> = { promise: Promise<T | void> }; | ||
|
||
const getVoidPromiseResult = () => ({ promise: Promise.resolve() }); | ||
|
||
// prettier-ignore | ||
export class Ampli { | ||
private disabled: boolean = false; | ||
private amplitude?: BrowserClient; | ||
|
||
get client(): BrowserClient { | ||
this.isInitializedAndEnabled(); | ||
return this.amplitude!; | ||
} | ||
|
||
get isLoaded(): boolean { | ||
return this.amplitude != null; | ||
} | ||
|
||
private isInitializedAndEnabled(): boolean { | ||
if (!this.amplitude) { | ||
console.error('ERROR: Ampli is not yet initialized. Have you called ampli.load() on app start?'); | ||
return false; | ||
} | ||
return !this.disabled; | ||
} | ||
|
||
/** | ||
* Initialize the Ampli SDK. Call once when your application starts. | ||
* | ||
* @param options Configuration options to initialize the Ampli SDK with. | ||
*/ | ||
load(options: LoadOptions): PromiseResult<void> { | ||
this.disabled = options.disabled ?? false; | ||
|
||
if (this.amplitude) { | ||
console.warn('WARNING: Ampli is already intialized. Ampli.load() should be called once at application startup.'); | ||
return getVoidPromiseResult(); | ||
} | ||
|
||
let apiKey: string | null = null; | ||
if (options.client && 'apiKey' in options.client) { | ||
apiKey = options.client.apiKey; | ||
} else if ('environment' in options) { | ||
apiKey = ApiKey[options.environment]; | ||
} | ||
|
||
if (options.client && 'instance' in options.client) { | ||
this.amplitude = options.client.instance; | ||
} else if (apiKey) { | ||
this.amplitude = amplitude.createInstance(); | ||
const configuration = (options.client && 'configuration' in options.client) ? options.client.configuration : {}; | ||
return this.amplitude.init(apiKey, undefined, { ...DefaultConfiguration, ...configuration }); | ||
} else { | ||
console.error("ERROR: ampli.load() requires 'environment', 'client.apiKey', or 'client.instance'"); | ||
} | ||
|
||
return getVoidPromiseResult(); | ||
} | ||
|
||
/** | ||
* Identify a user and set user properties. | ||
* | ||
* @param userId The user's id. | ||
* @param options Optional event options. | ||
*/ | ||
identify( | ||
userId: string | undefined, | ||
options?: EventOptions, | ||
): PromiseResult<Result> { | ||
if (!this.isInitializedAndEnabled()) { | ||
return getVoidPromiseResult(); | ||
} | ||
|
||
if (userId) { | ||
options = {...options, user_id: userId}; | ||
} | ||
|
||
const amplitudeIdentify = new amplitude.Identify(); | ||
return this.amplitude!.identify( | ||
amplitudeIdentify, | ||
options, | ||
); | ||
} | ||
|
||
/** | ||
* Flush the event. | ||
*/ | ||
flush() : PromiseResult<Result> { | ||
if (!this.isInitializedAndEnabled()) { | ||
return getVoidPromiseResult(); | ||
} | ||
|
||
return this.amplitude!.flush(); | ||
} | ||
|
||
/** | ||
* Track event | ||
* | ||
* @param event The event to track. | ||
* @param options Optional event options. | ||
*/ | ||
track(event: Event, options?: EventOptions): PromiseResult<Result> { | ||
if (!this.isInitializedAndEnabled()) { | ||
return getVoidPromiseResult(); | ||
} | ||
|
||
return this.amplitude!.track(event, undefined, options); | ||
} | ||
|
||
/** | ||
* Opened wallet dashboard | ||
* | ||
* [View in Tracking Plan](https://data.eu.amplitude.com/iota-foundation/IOTA%20Wallet%20Dashboard/events/main/latest/Opened%20wallet%20dashboard) | ||
* | ||
* Event has no description in tracking plan. | ||
* | ||
* @param properties The event's properties (e.g. activeNetwork) | ||
* @param options Amplitude event options. | ||
*/ | ||
openedWalletDashboard( | ||
properties?: OpenedWalletDashboardProperties, | ||
options?: EventOptions, | ||
) { | ||
return this.track(new OpenedWalletDashboard(properties), options); | ||
} | ||
} | ||
|
||
export const ampli = new Ampli(); | ||
|
||
// BASE TYPES | ||
type BrowserOptions = amplitude.Types.BrowserOptions; | ||
|
||
export type BrowserClient = amplitude.Types.BrowserClient; | ||
export type BaseEvent = amplitude.Types.BaseEvent; | ||
export type IdentifyEvent = amplitude.Types.IdentifyEvent; | ||
export type GroupEvent = amplitude.Types.GroupIdentifyEvent; | ||
export type Event = amplitude.Types.Event; | ||
export type EventOptions = amplitude.Types.EventOptions; | ||
export type Result = amplitude.Types.Result; |
This file contains hidden or 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,32 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// Modifications Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import * as amplitude from '@amplitude/analytics-browser'; | ||
import { LogLevel, TransportType, type UserSession } from '@amplitude/analytics-types'; | ||
import { PersistableStorage } from '@iota/core'; | ||
|
||
import { ampli } from './ampli'; | ||
|
||
const IS_PROD_ENV = process.env.NODE_ENV == 'production'; | ||
|
||
export const persistableStorage = new PersistableStorage<UserSession>(); | ||
|
||
export async function initAmplitude() { | ||
await ampli.load({ | ||
environment: 'iotawalletdashboard', | ||
// Flip this if you'd like to test Amplitude locally | ||
disabled: !IS_PROD_ENV, | ||
client: { | ||
configuration: { | ||
cookieStorage: persistableStorage, | ||
logLevel: IS_PROD_ENV ? LogLevel.Warn : amplitude.Types.LogLevel.Debug, | ||
}, | ||
}, | ||
}); | ||
|
||
window.addEventListener('pagehide', () => { | ||
amplitude.setTransport(TransportType.SendBeacon); | ||
amplitude.flush(); | ||
}); | ||
} |
This file contains hidden or 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,5 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './ampli'; | ||
export * from './amplitude'; |
This file contains hidden or 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 hidden or 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,15 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// Modifications Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { readFile, writeFile } from 'fs/promises'; | ||
|
||
const LICENSE = | ||
'// Copyright (c) Mysten Labs, Inc.\n// Modifications Copyright (c) 2024 IOTA Stiftung\n// SPDX-License-Identifier: Apache-2.0\n\n'; | ||
|
||
async function prependLicense(filename) { | ||
const content = await readFile(filename, 'utf8'); | ||
writeFile(filename, LICENSE + content); | ||
} | ||
|
||
prependLicense('lib/utils/analytics/ampli/index.ts'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.