Skip to content

Commit

Permalink
move to ga4
Browse files Browse the repository at this point in the history
  • Loading branch information
arielscarpinelli committed Jul 4, 2024
1 parent de10560 commit de89ee2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/js/VideoPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class VideoPlayer extends React.PureComponent {

onPlayerReady = (event) => {
console.log("player ready");
ga && ga('send', 'event', 'video', 'unstarted');
gtag && gtag('event', 'video');
this.showAndUpdateProgress();
};

Expand Down
4 changes: 2 additions & 2 deletions src/js/VideoPlayerIframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export default class VideoPlayerIframe extends React.Component {
}

onPlayerReady = (event) => {
ga && ga('send', 'event', 'video', 'unstarted');
gtag && gtag('event', 'video');
};

onPlayerStateChange = (event) => {
switch (event.data) {
case YT.PlayerState.UNSTARTED:
console.log("player state change: unstarted");
ga && ga('send', 'event', 'video', 'unstarted');
gtag && gtag('event', 'video');
break;

case YT.PlayerState.ENDED:
Expand Down
4 changes: 2 additions & 2 deletions src/js/YoutubeSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ class YoutubeSession {
}

clear() {
ga && ga('send', 'event', 'session', 'logout');
gtag && gtag('event', 'session_logout');
localStorage.removeItem(SESSION_USER_INFO);
localStorage.removeItem(SESSION_TOKEN_KEY);
localStorage.removeItem(SESSION_TOKEN_EXPIRE_KEY);
localStorage.removeItem(REFRESH_TOKEN_KEY);
}

save(authResponse) {
ga && ga('send', 'event', 'session', 'login');
gtag && gtag('event', 'session_login');
this._saveSessionToken(authResponse);
localStorage.setItem(REFRESH_TOKEN_KEY, authResponse.refresh_token);
this._retrieveUserInfo();
Expand Down
24 changes: 14 additions & 10 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,31 @@ try {
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

(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');
const gtmTag = document.createElement('script');
gtmTag.src = "https://www.googletagmanager.com/gtag/js?id=G-JZNXHHJWW5";

firstScriptTag.parentNode.insertBefore(gtmTag, firstScriptTag);

window.dataLayer = window.dataLayer || [];

function gtag(){dataLayer.push(arguments);}

gtag('js', new Date());

const GA_LOCAL_STORAGE_KEY = 'ga:clientId';

ga('create', 'UA-102778294-1', {
gtag('config', 'G-JZNXHHJWW5', {
'storage': 'none',
'cookieDomain': 'none',
'clientId': localStorage.getItem(GA_LOCAL_STORAGE_KEY)
});
ga(function(tracker) {
}, function(tracker) {
const clientId = tracker.get('clientId');
localStorage.setItem(GA_LOCAL_STORAGE_KEY, clientId);
alert("GA clientId: " + clientId);
});

ga('set', 'checkProtocolTask', null); // Disable file protocol checking.
ga('set', 'legacyDomain', legacyDomain);
ga('send', 'pageview');
// gtag('set', 'checkProtocolTask', null); // Disable file protocol checking.
gtag('set', 'legacyDomain', legacyDomain);


} catch (err) {
Expand Down

0 comments on commit de89ee2

Please sign in to comment.