Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
ensembl-web
ensembl-client
Commits
c5bb1aa1
Commit
c5bb1aa1
authored
Jul 01, 2022
by
Manoj Pandian Sakthivel
Browse files
Migrate GA to GA4
parent
e9cd3cfa
Pipeline
#294937
passed with stages
in 5 minutes and 8 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
108 additions
and
62 deletions
+108
-62
src/server/routes/unsupportedBrowserRouter.tsx
src/server/routes/unsupportedBrowserRouter.tsx
+9
-8
src/services/analytics-service.ts
src/services/analytics-service.ts
+30
-19
src/services/google-analytics/index.ts
src/services/google-analytics/index.ts
+55
-22
src/services/google-analytics/loadGoogleAnalytics.ts
src/services/google-analytics/loadGoogleAnalytics.ts
+14
-13
No files found.
src/server/routes/unsupportedBrowserRouter.tsx
View file @
c5bb1aa1
...
...
@@ -67,14 +67,15 @@ const unsupportedBrowserRouter = (_: Request, res: Response) => {
const
getAnalyticsScript
=
()
=>
process
.
env
.
REPORT_ANALYTICS
?.
toLowerCase
()
===
'
true
'
&&
process
.
env
.
GOOGLE_ANALYTICS_KEY
?
`<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '
${
process
.
env
.
GOOGLE_ANALYTICS_KEY
}
', 'auto');
ga('send', 'pageview');
</script>`
?
`<script async src="https://www.googletagmanager.com/gtag/js?id=
${
process
.
env
.
GOOGLE_ANALYTICS_KEY
}
"></>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '
${
process
.
env
.
GOOGLE_ANALYTICS_KEY
}
');
</script>
`
:
''
;
export
default
unsupportedBrowserRouter
;
src/services/analytics-service.ts
View file @
c5bb1aa1
...
...
@@ -39,50 +39,61 @@ class AnalyticsTracking {
private
setReporting
()
{
// don't send analytics other than in production deployment
// https://developers.google.com/analytics/devguides/collection/gtagjs/user-opt-out
if
(
!
config
.
shouldReportAnalytics
)
{
this
.
googleAnalytics
.
ga
(
'
set
'
,
'
sendHitTask
'
,
null
)
;
(
window
as
any
)[
`ga-disable-
${
googleAnalyticsKey
}
`
]
=
true
;
}
}
// Track a pageview
public
trackPageView
(
pagePath
:
string
)
{
this
.
googleAnalytics
.
pageview
(
pagePath
);
this
.
googleAnalytics
.
pageview
({
page_path
:
pagePath
});
}
// Track an event
public
trackEvent
(
g
a
:
AnalyticsOptions
)
{
g
a
.
species
&&
this
.
setSpeciesDimension
(
g
a
.
species
);
g
a
.
app
&&
this
.
setAppDimension
(
g
a
.
app
);
g
a
.
feature
&&
this
.
setFeatureDimension
(
g
a
.
feature
);
public
trackEvent
(
g
tag
:
AnalyticsOptions
)
{
g
tag
.
species
&&
this
.
setSpeciesDimension
(
g
tag
.
species
);
g
tag
.
app
&&
this
.
setAppDimension
(
g
tag
.
app
);
g
tag
.
feature
&&
this
.
setFeatureDimension
(
g
tag
.
feature
);
this
.
googleAnalytics
.
event
({
event
A
ction
:
g
a
.
action
,
event
C
ategory
:
g
a
.
category
,
event
L
abel
:
g
a
.
label
,
event
V
alue
:
g
a
.
value
event
_a
ction
:
g
tag
.
action
,
event
_c
ategory
:
g
tag
.
category
,
event
_l
abel
:
g
tag
.
label
,
event
_v
alue
:
g
tag
.
value
});
g
a
.
species
&&
this
.
setSpeciesDimension
(
null
);
g
a
.
feature
&&
this
.
setFeatureDimension
(
null
);
g
tag
.
species
&&
this
.
setSpeciesDimension
(
null
);
g
tag
.
feature
&&
this
.
setFeatureDimension
(
null
);
}
// Set app custom dimension
public
setAppDimension
(
app
:
string
|
null
)
{
this
.
googleAnalytics
.
ga
(
'
set
'
,
CustomDimensions
.
APP
,
app
);
this
.
googleAnalytics
.
setDimension
({
event_name
:
'
set_app_dimension
'
,
dimension_key
:
CustomDimensions
.
APP
,
dimension_value
:
app
});
}
// Set species custom dimension
public
setSpeciesDimension
(
speciesAnalyticsName
:
string
|
null
)
{
this
.
googleAnalytics
.
ga
(
'
set
'
,
CustomDimensions
.
SPECIES
,
speciesAnalyticsName
);
this
.
googleAnalytics
.
setDimension
({
event_name
:
'
set_species_dimension
'
,
dimension_key
:
CustomDimensions
.
SPECIES
,
dimension_value
:
speciesAnalyticsName
}
);
}
// Set feature custom dimension
public
setFeatureDimension
(
featureType
:
string
|
null
)
{
this
.
googleAnalytics
.
ga
(
'
set
'
,
CustomDimensions
.
FEATURE
,
featureType
);
this
.
googleAnalytics
.
setDimension
({
event_name
:
'
set_feature_dimension
'
,
dimension_key
:
CustomDimensions
.
FEATURE
,
dimension_value
:
featureType
});
}
}
...
...
src/services/google-analytics/index.ts
View file @
c5bb1aa1
...
...
@@ -16,46 +16,79 @@
import
loadGoogleAnalytics
from
'
./loadGoogleAnalytics
'
;
type
TrackPageView
=
{
page_title
?:
string
;
page_location
?:
string
;
page_path
:
string
;
};
type
TrackEvent
=
{
event_action
:
string
;
event_category
:
string
;
event_label
?:
string
;
event_value
?:
number
;
};
type
SetDimension
=
{
event_name
:
string
;
dimension_key
:
string
;
dimension_value
:
string
|
null
;
};
/**
* The interface of the GoogleAnalytics class below
* replicates the interface of the ReactGA library that we were using before
*/
class
GoogleAnalytics
{
static
googleAnalyticsKey
:
string
;
static
initialize
(
googleAnalyticsKey
:
string
)
{
loadGoogleAnalytics
(
googleAnalyticsKey
);
this
.
googleAnalyticsKey
=
googleAnalyticsKey
;
}
static
g
a
(...
args
:
any
[])
{
window
.
g
a
(...
args
);
static
g
tag
(...
args
:
any
[])
{
window
.
g
tag
(...
args
);
}
// referencee: example from Google's docs for
analytics
.js
// https://developers.google.com/analytics/devguides/collection/
analyticsjs/field-reference#hitType
static
pageview
(
pa
gePath
:
string
)
{
window
.
g
a
(
'
send
'
,
{
hitType
:
'
pageview
'
,
page
:
pagePath
// referencee: example from Google's docs for
gtag
.js
// https://developers.google.com/analytics/devguides/collection/
gtagjs/pages
static
pageview
(
pa
rams
:
TrackPageView
)
{
window
.
g
tag
(
'
event
'
,
'
page_view
'
,
{
send_to
:
this
.
googleAnalyticsKey
,
...
params
});
}
// referencee: Google's docs for analytics.js
// https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#eventCategory
static
event
(
params
:
{
eventAction
:
string
;
eventCategory
:
string
;
eventLabel
?:
string
;
eventValue
?:
number
;
})
{
// https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#event_category
static
event
(
params
:
TrackEvent
)
{
// clean up the params object before passing it to ga function
// by removing from it any empty fields
const
options
=
Object
.
entries
(
params
).
reduce
(
(
obj
,
[
key
,
value
]):
Partial
<
typeof
params
>
=>
{
return
value
!==
undefined
?
{
...
obj
,
[
key
]:
value
}
:
obj
;
},
{}
);
window
.
ga
(
'
send
'
,
'
event
'
,
options
);
const
options
:
{
event_category
:
string
;
event_label
?:
string
;
value
?:
string
;
}
=
{
event_category
:
params
.
event_category
};
if
(
params
.
event_label
)
{
options
[
'
event_label
'
]
=
params
.
event_label
;
}
if
(
params
.
event_value
)
{
options
[
'
value
'
]
=
params
.
event_label
;
}
window
.
gtag
(
'
event
'
,
params
.
event_action
,
options
);
}
static
setDimension
(
params
:
SetDimension
)
{
window
.
gtag
(
'
event
'
,
params
.
event_name
,
{
[
params
.
dimension_key
]:
params
.
dimension_value
});
}
}
...
...
src/services/google-analytics/loadGoogleAnalytics.ts
View file @
c5bb1aa1
...
...
@@ -27,45 +27,46 @@ import once from 'lodash/once';
*
*/
interface
G
A
{
interface
G
TAG
{
(...
args
:
any
[]):
void
;
// executes analytics commands
q
:
any
[];
// a container for queueing up analytics commands
l
:
number
;
// is used for timestamps
}
// extend the window interface with the google analytics object
declare
global
{
interface
Window
{
ga
:
GA
;
gtag
:
GTAG
;
dataLayer
:
any
[];
}
}
const
loadGoogleAnalytics
=
(
trackerId
:
string
)
=>
{
createGAShim
(
trackerId
);
loadScript
();
loadScript
(
trackerId
);
};
// create a simplified google analytics object and assign it to window
// to keep a queue of any pending analytics commands
// until the real google analytics object is ready to replace it
const
createGAShim
=
(
trackerId
:
string
)
=>
{
const
g
a
=
(...
args
:
any
[])
=>
{
const
g
tag
=
(...
args
:
any
[])
=>
{
// the sole purpose of the shim is to enqueue commands that it receives
// before the real Google Analytics script loads
window
.
ga
.
q
.
push
(
args
);
window
.
dataLayer
.
push
(
args
);
};
ga
.
q
=
[]
as
any
[];
// initialise the command queue
ga
.
l
=
new
Date
().
getTime
();
// Google uses this for timing hits
window
.
g
a
=
ga
;
window
.
dataLayer
=
window
.
dataLayer
||
[];
// initialise the command queue
window
.
gtag
=
gtag
;
window
.
g
tag
(
'
js
'
,
new
Date
());
// Google uses this for timing hits
window
.
ga
(
'
create
'
,
trackerId
,
'
auto
'
);
// immediately enqueue a command for creating a tracker
// The statement below automatically sends Pageview
// TODO: Check if we need to disable it by passing: `{send_page_view: false}` as the third arg
window
.
gtag
(
'
config
'
,
trackerId
);
};
const
loadScript
=
()
=>
{
const
loadScript
=
(
trackerId
:
string
)
=>
{
const
scriptElement
=
document
.
createElement
(
'
script
'
);
scriptElement
.
async
=
true
;
scriptElement
.
src
=
'
https://www.google
-analytics.com/analytics.js
'
;
scriptElement
.
src
=
`
https://www.google
tagmanager.com/gtag/js?id=
${
trackerId
}
`
;
document
.
body
.
appendChild
(
scriptElement
);
};
...
...
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