Skip to content

Commit

Permalink
Merge pull request #2661 from mgillian/feat/GA4
Browse files Browse the repository at this point in the history
feat/GA4 update google analytics scripts for ga4
  • Loading branch information
loulou2u committed Jun 12, 2023
2 parents 552f150 + 7239e48 commit 4f2ea10
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 77 deletions.
52 changes: 44 additions & 8 deletions uPortal-webapp/src/main/webapp/WEB-INF/jsp/GoogleAnalytics/init.jsp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,48 @@
<%@ include file="/WEB-INF/jsp/include.jsp"%>

<script type="text/javascript">
(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','//www.google-analytics.com/analytics.js','ga');
var up = up || {};
up.analytics = up.analytics || {};
up.analytics.model = ${up:json(data)};
var up = up || {};
(function($, _) {
up.analytics = up.analytics || {};
up.analytics.model = ${up:json(data)};
// copied directly from up-ga.js, need in order to include
// tag information in script parameter
var findPropertyConfig = function() {
if (up.analytics.model == null) {
return null;
}
if (_.isArray(up.analytics.model.hosts)) {
var propertyConfig = _.find(up.analytics.model.hosts, function(
propertyConfig
) {
if (propertyConfig.name == up.analytics.host) {
return propertyConfig;
}
});
if (propertyConfig != null) {
return propertyConfig;
}
}
return up.analytics.model.defaultConfig;
};
var props = findPropertyConfig();
var tagId = props.propertyId;
var s = document.createElement( 'script' );
s.setAttribute( 'src', "https://www.googletagmanager.com/gtag/js?id=" + tagId );
s.async = true;
document.body.appendChild( s );
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
up.gtag = gtag;
})(up.jQuery, up._);
</script>
145 changes: 76 additions & 69 deletions uPortal-webapp/src/main/webapp/media/skins/common/javascript/uportal/up-ga.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ var uportal = uportal || {};
return up.analytics.model.defaultConfig;
};

/**
* Set the dimensions that apply to the current user
*/
var getDimensions = function(propertyConfig) {
var dimensions = {};
_.each(propertyConfig.dimensionGroups || [], function(setting) {
dimensions['dimension' + setting.name] = setting.value;
});
return dimensions;
};

/**
* Create the tracker with the propertyId and configuration from the
* specified propertyConfig
Expand All @@ -57,19 +68,14 @@ var uportal = uportal || {};
createSettings[setting.name] = setting.value;
}
});
ga('create', propertyConfig.propertyId, createSettings); // Create the
};

/**
* Set the dimensions that apply to the current user
*/
var setDimensions = function(propertyConfig) {
var dimensions = {};
_.each(propertyConfig.dimensionGroups || [], function(setting) {
dimensions['dimension' + setting.name] = setting.value;
var dimensions = getDimensions(propertyConfig);
var dimensionKeys = [];
$.each(dimensions, function(key, value) {
dimensionKeys.push(key);
});
up.gtag('config', propertyConfig.propertyId, {
send_page_view: false,
});

ga('set', dimensions);
};

/**
Expand Down Expand Up @@ -98,7 +104,7 @@ var uportal = uportal || {};
/**
* Set variables specific to the current page
*/
var setPageVariables = function(fragmentName, tabName) {
var getPageVariables = function(fragmentName, tabName) {
if (up.analytics.pageData.tab != null) {
fragmentName =
fragmentName || up.analytics.pageData.tab.fragmentName;
Expand All @@ -113,11 +119,10 @@ var uportal = uportal || {};
} else {
title = 'No Tab';
}

ga('set', {
page: getTabUri(fragmentName, tabName),
title: title,
});
return {
page_location: getTabUri(fragmentName, tabName),
page_title: title
};
};

/**
Expand Down Expand Up @@ -171,17 +176,16 @@ var uportal = uportal || {};
/**
* Set variables specific to the specified portlet
*/
var setPortletVariables = function(windowId, portletData) {
var getPortletVariables = function(windowId, portletData) {
var portletTitle = getRenderedPortletTitle(windowId);

if (portletData == null) {
portletData = up.analytics.portletData[windowId];
}

ga('set', {
page: getPortletUri(portletData.fname),
title: 'Portlet: ' + portletTitle,
});
return {
page_title: 'Portlet: ' + portletTitle,
page_location: getPortletUri(portletData.fname)
};
};

/**
Expand Down Expand Up @@ -250,16 +254,11 @@ var uportal = uportal || {};
};
}

// Send the event
setPageVariables();
ga(
'send',
$.extend(
{
hitType: 'event',
hitCallback: clickFunction,
},
eventOpts
up.gtag('event', 'page_view',
$.extend({
event_callback: clickFunction
},
eventOpts
)
);

Expand Down Expand Up @@ -293,14 +292,16 @@ var uportal = uportal || {};
var fname = getFlyoutFname(clickedLink);

// Setup the page level state
setPageVariables();
var pageVariables = getPageVariables();

// Send the event and deal with the click
handleLinkClickEvent(event, clickedLink, {
eventCategory: 'Flyout Link',
eventAction: getPortletUri(fname),
eventLabel: portletFlyoutTitle,
});
handleLinkClickEvent(event, clickedLink,
$.extend({
event_category: 'Flyout Link',
event_action: getPortletUri(fname),
event_label: portletFlyoutTitle,
},
pageVariables));
}
);
};
Expand All @@ -316,18 +317,20 @@ var uportal = uportal || {};
var linkHost = clickedLink.prop('hostname');
if (linkHost != '' && linkHost != document.domain) {
var windowId = getExternaLinkWindowId(clickedLink);
var eventVariables = null;
if (windowId != null) {
setPortletVariables(windowId);
eventVariables = getPortletVariables(windowId);
} else {
setPageVariables();
eventVariables = getPageVariables();
}

// Send the event and deal with the click
handleLinkClickEvent(event, clickedLink, {
eventCategory: 'Outbound Link',
eventAction: clickedLink.prop('href'),
eventLabel: clickedLink.text(),
});
handleLinkClickEvent(event, clickedLink,
$.extend({
event_category: 'Outbound Link',
event_action: clickedLink.prop('href'),
event_label: clickedLink.text(),
}, eventVariables));
}
});
};
Expand All @@ -350,15 +353,13 @@ var uportal = uportal || {};
.text()
.trim();

setPageVariables(fragmentName, tabName);
ga('send', 'pageview');
var pageVariables = getPageVariables(fragmentName, tabName);

up.gtag('event', 'page_view', pageVariables);
});
};

$(document).ready(function() {
// Fail safe, if the analytics library isn't loaded make sure the function
// exists
window['ga'] = window['ga'] || function() {};

var propertyConfig = findPropertyConfig();

Expand All @@ -371,20 +372,25 @@ var uportal = uportal || {};
createTracker(propertyConfig);

// Set Dimensions
setDimensions(propertyConfig);
var dimensions = getDimensions(propertyConfig);

up.gtag('event', 'page_view', dimensions);

// Page Event
setPageVariables();
var pageVariables = getPageVariables();

// Don't bother sending the view in MAX WindowState
if (up.analytics.pageData.urlState != 'MAX') {
ga('send', 'pageview');
up.gtag('event', 'page_view',
$.extend(pageVariables, dimensions));
}
ga('send', {
hitType: 'timing',
timingCategory: 'tab',
timingVar: getTabUri(),
timingValue: up.analytics.pageData.executionTimeNano / 1000000,
});

up.gtag('event', 'timing_complete',
$.extend({
event_category: 'tab',
name: getTabUri(),
value: up.analytics.pageData.executionTimeNano
}, pageVariables, dimensions));

// Portlet Events
_.each(up.analytics.portletData, function(portletData, windowId) {
Expand All @@ -393,14 +399,15 @@ var uportal = uportal || {};
return;
}

setPortletVariables(windowId, portletData);
ga('send', 'pageview');
ga('send', {
hitType: 'timing',
timingCategory: 'portlet',
timingVar: getPortletUri(portletData.fname),
timingValue: portletData.executionTimeNano / 1000000,
});
var portletVariables = getPortletVariables(windowId, portletData);
up.gtag('event', 'page_view', portletVariables);
up.gtag('event', 'timing_complete',
$.extend({
event_category: 'tab',
name: getTabUri(),
value: up.analytics.pageData.executionTimeNano
}, portletVariables, dimensions));

});

// Add handlers to deal with click events on flyouts
Expand Down

0 comments on commit 4f2ea10

Please sign in to comment.