Skip to content

Commit

Permalink
Merge pull request #125 from hmrc/PLATOPS-2060
Browse files Browse the repository at this point in the history
PLATOPS-2060: Include custom javascript snippet for GTM
  • Loading branch information
tomasz-rosiek authored Apr 15, 2019
2 parents f704603 + 0d938b4 commit 2f202c8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion project/PlayCrossCompilation.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uk.gov.hmrc.playcrosscompilation.AbstractPlayCrossCompilation
import uk.gov.hmrc.playcrosscompilation.PlayVersion.Play25
import uk.gov.hmrc.playcrosscompilation.PlayVersion._

object PlayCrossCompilation extends AbstractPlayCrossCompilation(defaultPlayVersion = Play25)
12 changes: 9 additions & 3 deletions src/main/play-25/uk/gov/hmrc/play/config/GTMConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ import play.api.Configuration

class GTMConfig @Inject()(configuration: Configuration) {
val url: Option[String] =
readConfig(container => s"gtm.$container.url")

val dataLayerUrl: Option[String] =
readConfig(_ => "gtm.data.layer.url")

def readConfig(configKey: String => String): Option[String] =
configuration.getString("gtm.container").map {
case container @ ("transitional" | "main") =>
case container@("transitional" | "main") =>
configuration
.getString(s"gtm.$container.url")
.getOrElse(throw new RuntimeException(s"Missing configuration gtm.$container.url"))
.getString(configKey(container))
.getOrElse(throw new RuntimeException(s"Missing configuration ${configKey(container)}"))
case _ => throw new IllegalArgumentException("gtm.container should be one of: { transitional, main }")
}
}
12 changes: 9 additions & 3 deletions src/main/play-26/uk/gov/hmrc/play/config/GTMConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ import play.api.Configuration

class GTMConfig @Inject()(configuration: Configuration) {
val url: Option[String] =
readConfig(container => s"gtm.$container.url")

val dataLayerUrl: Option[String] =
readConfig(_ => "gtm.data.layer.url")

def readConfig(configKey: String => String): Option[String] =
configuration.getString("gtm.container").map {
case container @ ("transitional" | "main") =>
case container@("transitional" | "main") =>
configuration
.getString(s"gtm.$container.url")
.getOrElse(throw new RuntimeException(s"Missing configuration gtm.$container.url"))
.getString(configKey(container))
.getOrElse(throw new RuntimeException(s"Missing configuration ${configKey(container)}"))
case _ => throw new IllegalArgumentException("gtm.container should be one of: { transitional, main }")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

@()

@gtmConfig.url.map { url =>
<script src='@url' type='text/javascript'></script>
@for(url <- gtmConfig.url; dataLayerUrl <- gtmConfig.dataLayerUrl) {
<script src='@dataLayerUrl' type='text/javascript'></script>
<script src='@url' type='text/javascript'></script>
}
21 changes: 15 additions & 6 deletions src/test/scala/uk/gov/hmrc/play/views/layouts/GTMSnippetSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class GTMSnippetSpec extends WordSpec with Matchers with PropertyChecks {
"include script tag with appropriate URL for container if gtm.container is defined" in {
val transitionalUrl = "http://s3bucket/transitional/gtm.js"
val mainUrl = "http://s3bucket/main/gtm.js"
val dataLayerUrl = "http://s3bucket/include/gtm_dl.js"
val containersAndUrls =
Table(
("container", "url"),
Expand All @@ -39,27 +40,35 @@ class GTMSnippetSpec extends WordSpec with Matchers with PropertyChecks {
)

forAll(containersAndUrls) { (container: String, url: String) =>
val snippet = createSnippet(Some(mainUrl), Some(transitionalUrl), Some(container))
val snippet = createSnippet(
container = Some(container),
mainUrl = Some(mainUrl),
transitionalUrl = Some(transitionalUrl),
dataLayerUrl = Some(dataLayerUrl))
script(snippet()) should contain(s"$url")
script(snippet()) should contain(s"$dataLayerUrl")
}
}

"not include script tag if gtm.container is not defined" in {
val snippet = createSnippet(None, None, None)
val snippet = createSnippet(None, None, None, None)
script(snippet()) shouldBe empty
}
}

private def createSnippet(
mainUrl: Option[String] = None,
transitionalUrl: Option[String] = None,
container: Option[String]): GTMSnippet = {
container: Option[String],
mainUrl: Option[String],
transitionalUrl: Option[String],
dataLayerUrl: Option[String]): GTMSnippet = {
val config = new GTMConfig(
Configuration(
Seq(
container.map("gtm.container" -> _),
mainUrl.map("gtm.main.url" -> _),
transitionalUrl.map("gtm.transitional.url" -> _),
container.map("gtm.container" -> _)).flatten: _*))
dataLayerUrl.map("gtm.data.layer.url" -> _)
).flatten: _*))
new GTMSnippet(config)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ object TestConfigs {
Configuration(
"gtm.container" -> "transitional",
"gtm.transitional.url" -> "some transitional url",
"gtm.main.url" -> "some main url"
"gtm.main.url" -> "some main url",
"gtm.data.layer.url" -> "some data layer url"
)
)

Expand Down

0 comments on commit 2f202c8

Please sign in to comment.