Skip to content

Commit

Permalink
Analytics services integration (#7313)
Browse files Browse the repository at this point in the history
* Analytics services integration

* Analytics services integration - add settings help

* Analytics services integration - move configuration to config.properties

* Analytics services integration - better name for web analytics service

* Analytics improvements (#84)

* Analytics services integration / Send events by protocol

* Analytics services integration / Add example config for matomo.

* Update web/src/main/webResources/WEB-INF/config.properties

Co-authored-by: François Prunayre <[email protected]>

* Test / Fix bean initialization

Failing test were:

```
15:47:17,320 [INFO] Results:
15:47:17,320 [INFO] 
Error: 7,320 [ERROR] Failures: 
Error: 7,320 [ERROR]   AlternateLogoForPdfExportTest.whenGeneratingPdfWithPropertyNotSetSiteLogoIsUsed:114 Status expected:<200> but was:<400>
Error: 7,321 [ERROR]   AlternateLogoForPdfExportTest.whenGeneratingPdfWithPropertySetPdfLogoIsUsed:74 Status expected:<200> but was:<400>
Error: 7,321 [ERROR]   AlternateLogoForPdfExportTest.whenNotGeneratingPdfWithPropertySetSiteLogoIsUsed:93 Status expected:<200> but was:<400>
Error: 7,321 [ERROR] Errors: 
Error: 7,321 [ERROR]   FormatterApiIntegrationTest.testExec:97 » XPath Exception in extension functio.
```

---------

Co-authored-by: François Prunayre <[email protected]>
Co-authored-by: Juan Luis Rodríguez Ponce <[email protected]>
  • Loading branch information
3 people authored Jun 6, 2024
1 parent e3c8665 commit 0dec1ad
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2001-2023 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
* Rome - Italy. email: [email protected]
*/

package org.fao.geonet.analytics;

public class WebAnalyticsConfiguration {
private String service;
private String javascriptCode;

public String getService() {
return service;
}

public void setService(String service) {
this.service = service;
}

public String getJavascriptCode() {
return javascriptCode;
}

public void setJavascriptCode(String javascriptCode) {
this.javascriptCode = javascriptCode;
}
}
15 changes: 15 additions & 0 deletions core/src/main/java/org/fao/geonet/util/XslUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.http.impl.client.DefaultHttpClient;
import org.fao.geonet.ApplicationContextHolder;
import org.fao.geonet.SystemInfo;
import org.fao.geonet.analytics.WebAnalyticsConfiguration;
import org.fao.geonet.api.records.attachments.FilesystemStore;
import org.fao.geonet.api.records.attachments.FilesystemStoreResourceContainer;
import org.fao.geonet.api.records.attachments.Store;
Expand Down Expand Up @@ -1580,4 +1581,18 @@ private static List<Element> buildRecordLink(List<Hit> hits, String type) {
public static String escapeForJson(String value) {
return StringEscapeUtils.escapeJson(value);
}

public static String getWebAnalyticsService() {
ApplicationContext applicationContext = ApplicationContextHolder.get();
WebAnalyticsConfiguration webAnalyticsConfiguration = applicationContext.getBean(WebAnalyticsConfiguration.class);

return webAnalyticsConfiguration.getService();
}

public static String getWebAnalyticsJavascriptCode() {
ApplicationContext applicationContext = ApplicationContextHolder.get();
WebAnalyticsConfiguration webAnalyticsConfiguration = applicationContext.getBean(WebAnalyticsConfiguration.class);

return webAnalyticsConfiguration.getJavascriptCode();
}
}
5 changes: 5 additions & 0 deletions core/src/test/resources/core-repository-test-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,9 @@

<bean id="servletContext" class="org.fao.geonet.GeonetMockServletContext"/>
<bean id="threadPool" class="org.fao.geonet.TestThreadPool"/>

<bean class="org.fao.geonet.analytics.WebAnalyticsConfiguration" name="webAnalyticsConfiguration">
<property name="service" value="" />
<property name="javascriptCode" value="" />
</bean>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"$filter",
"gnExternalViewer",
"gnGlobalSettings",
"gnWebAnalyticsService",
function (
gnMap,
gnOwsCapabilities,
Expand All @@ -68,7 +69,8 @@
gnConfig,
$filter,
gnExternalViewer,
gnGlobalSettings
gnGlobalSettings,
gnWebAnalyticsService
) {
this.configure = function (options) {
angular.extend(this.map, options);
Expand Down Expand Up @@ -135,6 +137,7 @@

var addWFSToMap = function (link, md) {
var url = $filter("gnLocalized")(link.url) || link.url;
gnWebAnalyticsService.trackLink(url, link.protocol);

var isServiceLink =
gnSearchSettings.mapProtocols.services.indexOf(link.protocol) > -1;
Expand Down Expand Up @@ -211,6 +214,7 @@

function addMapToMap(record, md) {
var url = $filter("gnLocalized")(record.url) || record.url;
gnWebAnalyticsService.trackLink(url, record.protocol);
gnOwsContextService.loadContextFromUrl(url, gnSearchSettings.viewerMap);

gnSearchLocation.setMap("legend");
Expand Down Expand Up @@ -250,8 +254,12 @@
var openLink = function (record, link) {
var url = $filter("gnLocalized")(record.url) || record.url;
if (url && angular.isString(url) && url.match("^(http|ftp|sftp|\\\\|//)")) {
gnWebAnalyticsService.trackLink(url, record.protocol);

return window.open(url, "_blank");
} else if (url && url.indexOf("www.") == 0) {
gnWebAnalyticsService.trackLink("http://" + url, record.protocol);

return window.open("http://" + url, "_blank");
} else if (
record.title &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,24 @@
}
]);

/**
* Service to track links in the web analytics service configured in GeoNetwork.
*/
module.service("gnWebAnalyticsService", [
"gnGlobalSettings",
function (gnGlobalSettings) {
var analyticsService = gnGlobalSettings.webAnalyticsService;

this.trackLink = function (url, linkType) {
// Implement track link for the analytics
if (analyticsService === "matomo") {
_paq.push(["trackLink", url, linkType]);
_paq.push(["trackEvent", "catalogue-actions", linkType, url]);
}
};
}
]);

module.filter("sanitizeHtmlFilter", [
"$filter",
"$sanitize",
Expand Down
11 changes: 11 additions & 0 deletions web-ui/src/main/resources/catalog/views/default/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"gnFacetSorter",
"gnExternalViewer",
"gnUrlUtils",
"gnWebAnalyticsService",
"gnAlertService",
function (
$scope,
Expand All @@ -169,6 +170,7 @@
gnFacetSorter,
gnExternalViewer,
gnUrlUtils,
gnWebAnalyticsService,
gnAlertService
) {
var viewerMap = gnSearchSettings.viewerMap;
Expand Down Expand Up @@ -421,6 +423,10 @@

$scope.resultviewFns = {
addMdLayerToMap: function (link, md) {
var config = buildAddToMapConfig(link, md);

gnWebAnalyticsService.trackLink(config.url, link.protocol);

// This is probably only a service
// Open the add service layer tab
var config = buildAddToMapConfig(link, md);
Expand All @@ -442,6 +448,11 @@
if (config.length === 0) {
return;
}

config.forEach(function (c) {
gnWebAnalyticsService.trackLink(c.url, c.type);
});

$location.path("map").search({
add: encodeURIComponent(angular.toJson(config))
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,10 @@
The default publication options publishes the metadata to the ALL group and the INTRANET group.
-->
<bean class="org.fao.geonet.config.DefaultPublicationConfig" name="publicationConfig" />


<bean class="org.fao.geonet.analytics.WebAnalyticsConfiguration" name="webAnalyticsConfiguration">
<property name="service" value="\${analytics.web.service}" />
<property name="javascriptCode" value="\${analytics.web.jscode}" />
</bean>
</beans>
8 changes: 8 additions & 0 deletions web/src/main/webResources/WEB-INF/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ metadata.extentApi.disableFullUrlBackgroundMapServices=true

db.migration_onstartup=true

# Analytics service: (empty value: no analytics),matomo,google
analytics.web.service=
# Analytics javascript code to integrate with the analytics service (lines must be ended with \n\)
analytics.web.jscode=

#analytics.web.service=matomo
#analytics.web.jscode=var _paq = _paq || [];_paq.push(['trackPageView']);_paq.push(['enableLinkTracking']);(function() {var u="//localhost/";_paq.push(['setTrackerUrl', u+'piwik.php']);_paq.push(['setSiteId', '1']);var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);})();var currentUrl = location.href; window.addEventListener('hashchange', function() {_paq.push(['setReferrerUrl', currentUrl]);currentUrl = window.location.href;_paq.push(['setCustomUrl', currentUrl]);_paq.push(['setDocumentTitle', currentUrl]);_paq.push(['deleteCustomVariables', 'page']);_paq.push(['trackPageView']);var content = document.getElementsByTagName('body')[0];_paq.push(['MediaAnalytics::scanForMedia', content]);_paq.push(['FormAnalytics::scanForForms', content]);_paq.push(['trackContentImpressionsWithinNode', content]);_paq.push(['enableLinkTracking']);});


16 changes: 16 additions & 0 deletions web/src/main/webapp/xslt/base-layout-cssjs-loader.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@

<script type="text/javascript">
var module = angular.module('<xsl:value-of select="$angularApp"/>');

module.config(['gnGlobalSettings',
function(gnGlobalSettings) {
gnGlobalSettings.webAnalyticsService = '<xsl:value-of select="util:getWebAnalyticsService()"/>';
}]);
</script>

<xsl:if test="$angularApp = 'gn_search' or $angularApp = 'gn_login' or $angularApp = 'gn_admin'">
Expand Down Expand Up @@ -313,4 +318,15 @@
//jQuery.migrateEnablePatches( "self-closed-tags" );
</script>
</xsl:template>


<xsl:template name="webAnalytics">
<xsl:variable name="webAnalyticsService" select="util:getWebAnalyticsService()" />
<xsl:variable name="webAnalyticsCode" select="util:getWebAnalyticsJavascriptCode()" />
<xsl:if test="string($webAnalyticsService) and string($webAnalyticsCode)">
<script type="text/javascript">
<xsl:value-of select="$webAnalyticsCode" />
</script>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
2 changes: 2 additions & 0 deletions web/src/main/webapp/xslt/base-layout.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@
</xsl:if>
</xsl:otherwise>
</xsl:choose>

<xsl:call-template name="webAnalytics"/>
</body>
</html>
</xsl:template>
Expand Down
1 change: 1 addition & 0 deletions web/src/main/webapp/xslt/common/render-html.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<xsl:call-template name="footer"/>
</div>

<xsl:call-template name="webAnalytics"/>
</body>
</html>
</xsl:template>
Expand Down

0 comments on commit 0dec1ad

Please sign in to comment.