Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Google Analytics v4 (GA4) properties #788

Merged
merged 6 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion microsite/docs/docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,18 @@ micrositeGitHostingService := GitLab
micrositeGitHostingUrl := "https://gitlab.com/gitlab-org/gitlab-ce"
```

- `micrositeAnalyticsToken`: Property id of Google Analytics. This is empty by default.
- `micrositeAnalyticsToken`: Property id of Google's Universal Analytics token ("UA-...-1"). This is empty by default.

```scala
micrositeAnalyticsToken := 'UA-XXXXX-Y'
```

- `micrositeGoogleAnalytics4Token`: Property id of Google Analytics 4 (GA4). This is empty by default.

```scala
micrositeGoogleAnalytics4Token := 'G-XXXXXXXXXX'
```

- `micrositeGithubLinks`: This setting defines whether to show/hide GitHub links for stars and forks in docs layout. By default, it is enabled.

```scala
Expand Down
9 changes: 7 additions & 2 deletions src/main/scala/microsites/MicrositeKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ trait MicrositeKeys {

val micrositeAnalyticsToken: SettingKey[String] =
settingKey[String](
"Optional. Add your property id of Google Analytics to add a Google Analytics tracker"
"Optional. Add your legacy Universal Access (UA-...) property id of Google Analytics to add a Google Analytics tracker"
)
val micrositeGoogleAnalytics4Token: SettingKey[String] =
settingKey[String](
"Optional. Add your Google Analytics 4 (GA4, G-...) property id of Google Analytics to add a Google Analytics tracker"
)
val micrositeGitterChannel: SettingKey[Boolean] = settingKey[Boolean](
"Optional. Includes Gitter sidecar Chat functionality. Enabled by default."
Expand Down Expand Up @@ -338,7 +342,8 @@ trait MicrositeAutoImportSettings extends MicrositeKeys {
organizationHomepage = micrositeOrganizationHomepage.value,
twitter = micrositeTwitter.value,
twitterCreator = micrositeTwitterCreator.value,
analytics = micrositeAnalyticsToken.value
analytics = micrositeAnalyticsToken.value,
googleAnalytics4 = micrositeGoogleAnalytics4Token.value
),
visualSettings = MicrositeVisualSettings(
highlightTheme = micrositeHighlightTheme.value,
Expand Down
33 changes: 17 additions & 16 deletions src/main/scala/microsites/MicrositesPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,23 @@ object MicrositesPlugin extends AutoPlugin {
"white-color" -> "#FFFFFF"
)
},
micrositeFavicons := Seq(),
micrositeVersionList := Seq(),
micrositeGithubOwner := gitRemoteInfo._1,
micrositeGithubRepo := gitRemoteInfo._2,
micrositeGithubToken := None,
micrositeGitHostingService := GitHub,
micrositeGitHostingUrl := "",
micrositePushSiteWith := GHPagesPlugin,
micrositeAnalyticsToken := "",
micrositeGitterChannel := true,
micrositeGitterChannelUrl := s"${micrositeGithubOwner.value}/${micrositeGithubRepo.value}",
micrositeFooterText := Some(layouts.Layout.footer.toString),
micrositeEditButton := None,
micrositeGithubLinks := true,
micrositeSearchEnabled := true,
micrositeHomeButtonTarget := "repo",
micrositeFavicons := Seq(),
micrositeVersionList := Seq(),
micrositeGithubOwner := gitRemoteInfo._1,
micrositeGithubRepo := gitRemoteInfo._2,
micrositeGithubToken := None,
micrositeGitHostingService := GitHub,
micrositeGitHostingUrl := "",
micrositePushSiteWith := GHPagesPlugin,
micrositeAnalyticsToken := "",
micrositeGoogleAnalytics4Token := "",
micrositeGitterChannel := true,
micrositeGitterChannelUrl := s"${micrositeGithubOwner.value}/${micrositeGithubRepo.value}",
micrositeFooterText := Some(layouts.Layout.footer.toString),
micrositeEditButton := None,
micrositeGithubLinks := true,
micrositeSearchEnabled := true,
micrositeHomeButtonTarget := "repo",
makeSite / includeFilter := "*.html" | "*.css" | "*.png" | "*.jpg" | "*.jpeg" | "*.gif" | "*.js" | "*.swf" | "*.md" | "*.webm" | "*.ico" | "CNAME" | "*.yml" | "*.svg" | "*.json",
Jekyll / includeFilter := (makeSite / includeFilter).value || "LICENSE",
commands ++= Seq(publishMicrositeCommand),
Expand Down
33 changes: 26 additions & 7 deletions src/main/scala/microsites/layouts/Layout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ abstract class Layout(config: MicrositeSettings) {

def render: TypedTag[String]

def commonHead: TypedTag[String] = {
head(
metas,
favicons,
styles
)
}
def commonHead: List[TypedTag[String]] =
List(
head(
metas,
favicons,
styles
)
) ++ ganalytics4

val ganalytics: Option[TypedTag[String]] =
if (config.identity.analytics.nonEmpty)
Expand Down Expand Up @@ -207,6 +208,24 @@ abstract class Layout(config: MicrositeSettings) {
cssStyles ++ customCssList ++ customScssList ++ customCDNList ++ ganalytics.toList
}

val ganalytics4: List[TypedTag[String]] =
if (config.identity.googleAnalytics4.nonEmpty)
List(
script(
attr("async") := "async",
attr(
"src"
) := s"https://www.googletagmanager.com/gtag/js?id=${config.identity.googleAnalytics4}"
),
script(s"""
|window.dataLayer = window.dataLayer || [];
|function gtag(){dataLayer.push(arguments);}
|gtag('js', new Date());
|gtag('config', '${config.identity.googleAnalytics4}');
""".stripMargin)
)
else Nil

def scripts: List[TypedTag[String]] = {

val customJsList =
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/microsites/microsites.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ case class MicrositeIdentitySettings(
organizationHomepage: String,
twitter: String,
twitterCreator: String,
analytics: String
analytics: String,
googleAnalytics4: String
)

case class MicrositeFileLocations(
Expand Down
4 changes: 3 additions & 1 deletion src/test/scala/microsites/util/Arbitraries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ trait Arbitraries {
twitter <- Arbitrary.arbitrary[String]
twitterCreator <- Arbitrary.arbitrary[String]
analytics <- Arbitrary.arbitrary[String]
googleAnalytics4 <- Arbitrary.arbitrary[String]
highlightTheme <- Arbitrary.arbitrary[String]
highlightLanguages <- Arbitrary.arbitrary[Seq[String]]
theme <- Arbitrary.arbitrary[String]
Expand Down Expand Up @@ -166,7 +167,8 @@ trait Arbitraries {
organizationHomepage,
twitter,
twitterCreator,
analytics
analytics,
googleAnalytics4
),
MicrositeVisualSettings(
highlightTheme,
Expand Down
Loading