Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
8eef757f
Unverified
Commit
8eef757f
authored
Jul 11, 2022
by
Zach Pomerantz
Committed by
GitHub
Jul 11, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: analytics initialization (#4070)
* refactor: analytics initialization * fix: typings
parent
b1c29b3b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
47 deletions
+26
-47
index.ts
src/components/AmplitudeAnalytics/index.ts
+26
-47
No files found.
src/components/AmplitudeAnalytics/index.ts
View file @
8eef757f
...
@@ -6,8 +6,8 @@ import { Identify, identify, init, track } from '@amplitude/analytics-browser'
...
@@ -6,8 +6,8 @@ import { Identify, identify, init, track } from '@amplitude/analytics-browser'
* Uniswap has two Amplitude projects: test and production. You must be a
* Uniswap has two Amplitude projects: test and production. You must be a
* member of the organization on Amplitude to view details.
* member of the organization on Amplitude to view details.
*/
*/
export
function
initializeAnalytics
()
{
export
function
initializeAnalytics
(
isDevEnvironment
=
process
.
env
.
NODE_ENV
===
'
development
'
)
{
if
(
process
.
env
.
NODE_ENV
===
'
development
'
)
return
if
(
isDevEnvironment
)
return
const
API_KEY
=
process
.
env
.
REACT_APP_AMPLITUDE_KEY
const
API_KEY
=
process
.
env
.
REACT_APP_AMPLITUDE_KEY
if
(
typeof
API_KEY
===
'
undefined
'
)
{
if
(
typeof
API_KEY
===
'
undefined
'
)
{
...
@@ -25,7 +25,7 @@ export function initializeAnalytics() {
...
@@ -25,7 +25,7 @@ export function initializeAnalytics() {
city
:
false
,
city
:
false
,
region
:
false
,
region
:
false
,
country
:
false
,
country
:
false
,
dma
:
false
,
//
Disables designated market area tracking
dma
:
false
,
//
designated market area
},
},
}
}
)
)
...
@@ -34,7 +34,7 @@ export function initializeAnalytics() {
...
@@ -34,7 +34,7 @@ export function initializeAnalytics() {
/** Sends an event to Amplitude. */
/** Sends an event to Amplitude. */
export
function
sendAnalyticsEvent
(
eventName
:
string
,
eventProperties
?:
Record
<
string
,
unknown
>
)
{
export
function
sendAnalyticsEvent
(
eventName
:
string
,
eventProperties
?:
Record
<
string
,
unknown
>
)
{
if
(
process
.
env
.
NODE_ENV
===
'
development
'
)
{
if
(
process
.
env
.
NODE_ENV
===
'
development
'
)
{
console
.
log
(
'
amplitude event log:
'
,
`
${
eventName
}
:
${
JSON
.
stringify
(
eventProperties
)}
`
)
console
.
debug
(
`[amplitude(
${
eventName
}
)]
:
${
JSON
.
stringify
(
eventProperties
)}
`
)
return
return
}
}
...
@@ -42,68 +42,47 @@ export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<s
...
@@ -42,68 +42,47 @@ export function sendAnalyticsEvent(eventName: string, eventProperties?: Record<s
}
}
/**
/**
* Class that exposes methods to m
odify
the User Model's properties in
* Class that exposes methods to m
utate
the User Model's properties in
* Amplitude that represents the current session's user.
* Amplitude that represents the current session's user.
*
*
* See https://help.amplitude.com/hc/en-us/articles/115002380567-User-properties-and-event-properties
* See https://help.amplitude.com/hc/en-us/articles/115002380567-User-properties-and-event-properties
* for details.
* for details.
*/
*/
class
UserModel
{
class
UserModel
{
private
_isDevEnvironemnt
=
true
constructor
(
private
isDevEnvironment
=
process
.
env
.
NODE_ENV
===
'
development
'
)
{}
constructor
(
)
{
private
log
(
method
:
string
,
...
parameters
:
unknown
[]
)
{
process
.
env
.
NODE_ENV
===
'
development
'
?
(
this
.
_isDevEnvironemnt
=
true
)
:
(
this
.
_isDevEnvironemnt
=
false
)
console
.
debug
(
`[amplitude(Identify)]:
${
method
}
(
${
parameters
}
)`
)
}
}
public
set
(
propertyName
:
string
,
propertyValue
:
string
|
number
)
{
private
call
(
mutate
:
(
event
:
Identify
)
=>
Identify
)
{
if
(
this
.
_isDevEnvironemnt
)
{
if
(
this
.
isDevEnvironment
)
{
console
.
log
(
'
amplitude user model update with operation set:
'
,
`
${
propertyName
}
:
${
propertyValue
}
`
)
const
log
=
(
_
:
Identify
,
method
:
string
)
=>
this
.
log
.
bind
(
this
,
method
)
mutate
(
new
Proxy
(
new
Identify
(),
{
get
:
log
}))
return
return
}
}
const
identifyObj
=
new
Identify
()
identify
(
mutate
(
new
Identify
()))
identifyObj
.
set
(
propertyName
,
propertyValue
)
identify
(
identifyObj
)
}
}
public
setOnce
(
propertyName
:
string
,
propertyValue
:
string
|
number
)
{
set
(
key
:
string
,
value
:
string
|
number
)
{
if
(
this
.
_isDevEnvironemnt
)
{
this
.
call
((
event
)
=>
event
.
set
(
key
,
value
))
console
.
log
(
'
amplitude user model update with operation setOnce:
'
,
`
${
propertyName
}
:
${
propertyValue
}
`
)
return
}
const
identifyObj
=
new
Identify
()
identifyObj
.
setOnce
(
propertyName
,
propertyValue
)
identify
(
identifyObj
)
}
}
public
add
(
propertyName
:
string
,
propertyValue
:
string
|
number
)
{
setOnce
(
key
:
string
,
value
:
string
|
number
)
{
if
(
this
.
_isDevEnvironemnt
)
{
this
.
call
((
event
)
=>
event
.
setOnce
(
key
,
value
))
console
.
log
(
'
amplitude user model update with operation add:
'
,
`
${
propertyName
}
:
${
propertyValue
}
`
)
return
}
const
identifyObj
=
new
Identify
()
identifyObj
.
add
(
propertyName
,
typeof
propertyValue
===
'
number
'
?
propertyValue
:
0
)
identify
(
identifyObj
)
}
}
public
postInsert
(
propertyName
:
string
,
propertyValue
:
string
|
number
)
{
add
(
key
:
string
,
value
:
string
|
number
)
{
if
(
this
.
_isDevEnvironemnt
)
{
this
.
call
((
event
)
=>
event
.
add
(
key
,
typeof
value
===
'
number
'
?
value
:
0
))
console
.
log
(
'
amplitude user model update with operation postInsert:
'
,
`
${
propertyName
}
:
${
propertyValue
}
`
)
return
}
const
identifyObj
=
new
Identify
()
identifyObj
.
postInsert
(
propertyName
,
propertyValue
)
identify
(
identifyObj
)
}
}
public
remove
(
propertyName
:
string
,
propertyValue
:
string
|
number
)
{
postInsert
(
key
:
string
,
value
:
string
|
number
)
{
if
(
this
.
_isDevEnvironemnt
)
{
this
.
call
((
event
)
=>
event
.
postInsert
(
key
,
value
))
console
.
log
(
'
amplitude user model update with operation remove:
'
,
`
${
propertyName
}
:
${
propertyValue
}
`
)
}
return
}
remove
(
key
:
string
,
value
:
string
|
number
)
{
const
identifyObj
=
new
Identify
()
this
.
call
((
event
)
=>
event
.
remove
(
key
,
value
))
identifyObj
.
remove
(
propertyName
,
propertyValue
)
identify
(
identifyObj
)
}
}
}
}
export
const
user
Model
=
new
UserModel
()
export
const
user
=
new
UserModel
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment