File tree Expand file tree Collapse file tree 5 files changed +103
-0
lines changed
main/java/edu/harvard/iq/dataverse/api
test/java/edu/harvard/iq/dataverse/api Expand file tree Collapse file tree 5 files changed +103
-0
lines changed Original file line number Diff line number Diff line change 1+ ### Feature Request: API endpoint for analytics.html
2+
3+ New API to get the analytics.html from settings for SPA
4+
5+ See also [ the guides] ( https://dataverse-guide--11359.org.readthedocs.build/en/11359/installation/config.html#web-analytics-code ) , #11448 .
Original file line number Diff line number Diff line change @@ -7609,3 +7609,20 @@ Parameters:
76097609
76107610``per_page`` Number of results returned per page.
76117611
7612+
7613+ Customization: Web Analytics Code
7614+ ---------------------------------
7615+
7616+ The Customization API is used to retrieve the analytics-code.html. See also :ref:`web-analytics-code` in the Configuration section of the Installation Guide.
7617+
7618+ The content is returned in type="text/html; charset=UTF-8"
7619+
7620+ A curl example getting the analytics-code
7621+
7622+ .. code-block:: bash
7623+
7624+ export SERVER_URL=https://demo.dataverse.org
7625+
7626+ curl "$SERVER_URL/api/customization/analytics"
7627+
7628+ .. _customization-analytics:
Original file line number Diff line number Diff line change 1+ package edu .harvard .iq .dataverse .api ;
2+
3+ import edu .harvard .iq .dataverse .settings .SettingsServiceBean ;
4+ import jakarta .ejb .EJB ;
5+ import jakarta .ws .rs .*;
6+ import jakarta .ws .rs .core .Response ;
7+
8+ @ Path ("customization" )
9+ public class CustomizationApi extends AbstractApiBean {
10+
11+ @ EJB
12+ SettingsServiceBean settingsService ;
13+
14+ @ GET
15+ @ Path ("analytics" )
16+ @ Produces ({"text/html; charset=UTF-8" })
17+ public Response getAnalyticsHTML () {
18+ return getFromSettings (SettingsServiceBean .Key .WebAnalyticsCode , "text/html; charset=UTF-8" );
19+ }
20+
21+ private Response getFromSettings (SettingsServiceBean .Key key , String type ) {
22+ String value = settingsService .getValueForKey (key );
23+ if (value != null ) {
24+ return Response .ok (value ).type (type ).build ();
25+ } else {
26+ return Response .status (Response .Status .NOT_FOUND ).build ();
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package edu .harvard .iq .dataverse .api ;
2+
3+ import edu .harvard .iq .dataverse .settings .SettingsServiceBean ;
4+ import io .restassured .response .Response ;
5+ import org .junit .jupiter .api .AfterEach ;
6+ import org .junit .jupiter .api .Test ;
7+
8+ import static org .hamcrest .CoreMatchers .equalTo ;
9+
10+ public class CustomizationIT {
11+
12+ @ AfterEach
13+ public void after () {
14+ UtilIT .deleteSetting (SettingsServiceBean .Key .WebAnalyticsCode );
15+ }
16+ @ Test
17+ public void testGetCustomAnalytics () {
18+ String html = """
19+ <!-- Global Site Tag (gtag.js) - Google Analytics -->
20+ <script async="async" src="https://www.googletagmanager.com/gtag/js?id=YOUR-ACCOUNT-CODE"></script>
21+ <script>
22+ //<![CDATA[
23+ window.dataLayer = window.dataLayer || [];
24+ function gtag(){dataLayer.push(arguments);}
25+ gtag('js', new Date());
26+
27+ gtag('config', 'YOUR-ACCOUNT-CODE');
28+ //]]>
29+ </script>""" ;
30+
31+ UtilIT .setSetting (SettingsServiceBean .Key .WebAnalyticsCode , html ).prettyPrint ();
32+
33+ Response getResponse = UtilIT .getCustomAnalyticsHTML ();
34+ getResponse .prettyPrint ();
35+ getResponse .then ().assertThat ()
36+ .statusCode (200 )
37+ .body (equalTo (html ));
38+
39+ UtilIT .deleteSetting (SettingsServiceBean .Key .WebAnalyticsCode ).prettyPrint ();
40+
41+ getResponse = UtilIT .getCustomAnalyticsHTML ();
42+ getResponse .prettyPrint ();
43+ getResponse .then ().assertThat ()
44+ .statusCode (404 );
45+ }
46+ }
Original file line number Diff line number Diff line change @@ -4737,4 +4737,10 @@ public static Response updateDatasetFilesMetadata(String datasetIdOrPersistentId
47374737 return given ().header (API_TOKEN_HTTP_HEADER , apiToken ).contentType (ContentType .JSON ).body (jsonArray .toString ())
47384738 .post ("/api/datasets/" + idInPath + "/files/metadata" + optionalQueryParam );
47394739 }
4740+
4741+ public static Response getCustomAnalyticsHTML () {
4742+ return given ()
4743+ .contentType ("text/html; charset=UTF-8" )
4744+ .get ("/api/customization/analytics" );
4745+ }
47404746}
You can’t perform that action at this time.
0 commit comments