Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #999 from uPortal-Project/rely-on-gtm-to-init-ga
Browse files Browse the repository at this point in the history
Rely on Google Tag Manager to init Google Analytics
  • Loading branch information
apetro committed Nov 2, 2020
2 parents 04fcae1 + b7a4c0a commit 0387a5c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ It is those war files that are being versioned.

+ Handle zero-options case in option-link widget type
+ Implement Snippets for the MyUW Tag Manager Account
+ Remove Google Analytics JavaScript import obviated by Google Tag Manager

## 17.0.4 - 2020-10-12

Expand Down
40 changes: 40 additions & 0 deletions components/js/ga.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var config = config || {};
var _gaq = _gaq || [];

function _gaLt(event){
var el = event.srcElement || event.target;

/* Loop up the tree through parent elements if clicked element is not a link (eg: an image in a link) */
while(el && (typeof el.tagName == 'undefined' || el.tagName.toLowerCase() != 'a' || !el.href))
el = el.parentNode;

if(el && el.href){
if(el.href.indexOf(location.host) == -1 && el.href != 'javascript:;'){ /* external link */
var eventOptions = {eventCategory: 'Outbound Link', eventAction: el.href, eventLabel : el.text };
window.ga('send', 'event', eventOptions);
console.log('ga event logged c: Outbound Link a: '+el.href+' l:'+ el.text);
// Click will open in a new window if it is the middle button or the
// meta or control keys are held
var target = (el.target && !el.target.match(/^_(self|parent|top)$/i)) ? el.target : false;
var newWindow = event.button == 1 || event.metaKey || event.ctrlKey || target;
/* HitCallback function to either open link in either same or new window */
var hitBack = function(link, target){
target ? window.open(link, target) : window.location.href = link;
};

if(newWindow){
setTimeout(function(){
window.open(el.href, target);
}.bind(el),500);
/* Prevent standard click */
event.preventDefault ? event.preventDefault() : event.returnValue = !1;
}
}

}
}

/* Attach the event to all clicks in the document after page has loaded */
var w = window;
w.addEventListener ? w.addEventListener("load",function(){document.body.addEventListener("click",_gaLt,!1)},!1)
: w.attachEvent && w.attachEvent("onload",function(){document.body.attachEvent("onclick",_gaLt)});
16 changes: 2 additions & 14 deletions docs/Google-analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@

## Introduction

[www.google.com/analytics](https://www.google.com/analytics) (GA for short) is
a great (and free) way to analyze traffic to a web application. By default
uportal-app-framework disables GA.

## Basic configuration

Add in a `/js/config.js` file that will overwrite the `components/js/config.js`.

<!-- eslint-disable no-unused-vars -->
```javascript
var config = {
gaID: 'UA-########-##',
};
```
Uses Google Tag Manager, with the MyUW and WPS organizations hard-coded.

## Site search

GA has a great feature called site search. It collects information about what
people are searching for within a site. To configure it in
uportal-app-framework is a couple steps.
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/frame.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<uw-body></uw-body>

<!--javascript-->
<script type="text/javascript" src="js/ga.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js" data-main="main"></script>

</body>
Expand Down
1 change: 1 addition & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<uw-body></uw-body>

<!--javascript-->
<script type="text/javascript" src="js/ga.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js" data-main="main"></script>

</body>
Expand Down

0 comments on commit 0387a5c

Please sign in to comment.