Add support for Google Analytics

If the `initializr.env.google-analytics-tracking-code` is specified with
the tracking code to use, the web UI automatically integrates with Google
Analytics.

Closes gh-180
This commit is contained in:
Stephane Nicoll
2016-01-14 18:09:16 +01:00
parent 9139d10669
commit 891650a836
8 changed files with 45 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -122,6 +122,11 @@ class InitializrConfiguration {
*/
String springBootMetadataUrl = 'https://spring.io/project_metadata/spring-boot'
/**
* Tracking code for Google Analytics. Only enabled if a value is explicitly provided.
*/
String googleAnalyticsTrackingCode
/**
* The application name to use if none could be generated.
*/
@@ -184,6 +189,7 @@ class InitializrConfiguration {
void merge(Env other) {
artifactRepository = other.artifactRepository
springBootMetadataUrl = other.springBootMetadataUrl
googleAnalyticsTrackingCode = other.googleAnalyticsTrackingCode
fallbackApplicationName = other.fallbackApplicationName
invalidApplicationNames = other.invalidApplicationNames
forceSsl = other.forceSsl

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -47,9 +47,11 @@ abstract class AbstractInitializrController {
* Render the home page with the specified template.
*/
protected String renderHome(String templatePath) {
def metadata = metadataProvider.get()
def model = [:]
model['serviceUrl'] = generateAppUrl()
metadataProvider.get().properties.each {
metadata.properties.each {
if (it.key.equals('types')) {
model['types'] = it.value.clone()
} else {
@@ -60,6 +62,9 @@ abstract class AbstractInitializrController {
// Only keep project type
model['types'].content.removeAll { t -> !'project'.equals(t.tags['format']) }
// Google analytics support
model['trackingCode'] = metadata.configuration.env.googleAnalyticsTrackingCode
template templatePath, model
}

View File

@@ -126,6 +126,12 @@
"sourceType": "io.spring.initializr.metadata.InitializrConfiguration$Env",
"defaultValue": true
},
{
"name": "initializr.env.google-analytics-tracking-code",
"type": "java.lang.String",
"description": "Tracking code for Google Analytics.",
"sourceType": "io.spring.initializr.metadata.InitializrConfiguration$Env"
},
{
"name": "initializr.env.invalid-application-names",
"type": "java.util.List<java.lang.String>",

View File

@@ -172,5 +172,14 @@
<script src="/js/typeahead.bundle.min.js"></script>
<script src="/js/mousetrap.min.js"></script>
<script src="/js/start.js"></script>
<% if (trackingCode) { %>
<script>
(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');
ga('create', '${trackingCode}', 'auto');
ga('send', 'pageview');
</script><% } %>
</body>
</html>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -59,4 +59,10 @@ class MainControllerEnvIntegrationTests extends AbstractInitializrControllerInte
.hasSpringBootStarterTest()
}
@Test
void googleAnalytics() {
def body = htmlHome()
assertTrue 'google analytics should be enabled', body.contains("ga('create', 'UA-1234567-89', 'auto');")
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -440,6 +440,12 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
assertNotNull(response.body)
}
@Test
void googleAnalyticsDisabledByDefault() {
def body = htmlHome()
assertFalse 'google analytics should be disabled', body.contains('GoogleAnalyticsObject')
}
private JSONObject getMetadataJson() {
getMetadataJson(null, null)
}

View File

@@ -1,6 +1,7 @@
initializr:
env:
artifactRepository: https://repo.spring.io/lib-release
google-analytics-tracking-code: UA-1234567-89
forceSsl: false
fallbackApplicationName: FooBarApplication
invalidApplicationNames:

View File

@@ -33,6 +33,7 @@
"artifactRepository": "https://repo.spring.io/release/",
"fallbackApplicationName": "Application",
"forceSsl": true,
"googleAnalyticsTrackingCode": null,
"invalidApplicationNames": [
"SpringApplication",
"SpringBootApplication"