Skip to content

Commit

Permalink
Merge pull request #843 from googlecodelabs/Add-GA4-1
Browse files Browse the repository at this point in the history
Add GA4 analytics support to claat
  • Loading branch information
JasonCent authored Jun 22, 2023
2 parents 57b79d1 + b293135 commit 4c98edf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
28 changes: 16 additions & 12 deletions claat/parser/md/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ import (

// Metadata constants for the YAML header
const (
MetaAuthors = "authors"
MetaSummary = "summary"
MetaID = "id"
MetaCategories = "categories"
MetaEnvironments = "environments"
MetaStatus = "status"
MetaFeedbackLink = "feedback_link"
MetaAnalyticsAccount = "analytics_account"
MetaTags = "tags"
MetaSource = "source"
MetaDuration = "duration"
MetaAuthors = "authors"
MetaSummary = "summary"
MetaID = "id"
MetaCategories = "categories"
MetaEnvironments = "environments"
MetaStatus = "status"
MetaFeedbackLink = "feedback_link"
MetaAnalyticsAccount = "analytics_account"
MetaAnalyticsGa4Account = "analytics_ga4_account"
MetaTags = "tags"
MetaSource = "source"
MetaDuration = "duration"
)

const (
Expand Down Expand Up @@ -182,7 +183,7 @@ type docState struct {
survey int // last used survey ID
step *types.Step // current codelab step
lastNode nodes.Node // last appended node
env []string // current enviornment
env []string // current environment
cur *html.Node // current HTML node
stack []*stackItem // cur and flags stack
}
Expand Down Expand Up @@ -439,6 +440,9 @@ func addMetadataToCodelab(m map[string]string, c *types.Codelab, opts parser.Opt
case MetaAnalyticsAccount:
// Directly assign the GA id to the codelab field.
c.GA = v
case MetaAnalyticsGa4Account:
// Directly assign the GA id to the codelab field.
c.GA4 = v
case MetaTags:
// Standardize the tags and append to the codelab field.
c.Tags = append(c.Tags, util.NormalizedSplit(v)...)
Expand Down
5 changes: 5 additions & 0 deletions claat/parser/md/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func TestParseMetadata(t *testing.T) {
Tags: []string{"kiosk", "web"},
Feedback: "https://www.google.com",
GA: "12345",
GA4: "54321",
Extra: map[string]string{},
}

Expand All @@ -181,6 +182,7 @@ summary: abcdefghij
categories: not, really
environments: kiosk, web
analytics_account: 12345
analytics_ga4_account: 54321
feedback_link: https://www.google.com
---
Expand All @@ -204,6 +206,7 @@ func TestParseMetadataPassMetadata(t *testing.T) {
Tags: []string{"kiosk", "web"},
Feedback: "https://www.google.com",
GA: "12345",
GA4: "54321",
Extra: map[string]string{
"extra_field_two": "bbbbb",
},
Expand All @@ -216,6 +219,7 @@ summary: abcdefghij
categories: not, really
environments: kiosk, web
analytics_account: 12345
analytics_ga4_account: 54321
feedback_link: https://www.google.com
extra_field_one: aaaaa
extra_field_two: bbbbb
Expand Down Expand Up @@ -278,6 +282,7 @@ summary: abcdefghij
categories: not, really
environments: kiosk, web
analytics_account: 12345
analytics_ga4_account: 54321
feedback_link: https://www.google.com
extrafieldone: aaaaa
extrafieldtwo: bbbbb
Expand Down
18 changes: 10 additions & 8 deletions claat/render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ import (

// Context is a template context during execution.
type Context struct {
Env string
Prefix string
GlobalGA string
Format string
Meta *types.Meta
Steps []*types.Step
Updated string
Extra map[string]string // Extra variables passed from the command line.
Env string
Prefix string
GlobalGA string
GlobalGA4 string
Format string
Meta *types.Meta
Steps []*types.Step
Updated string
Extra map[string]string // Extra variables passed from the command line.
}

// Execute renders a template of the fmt format into w.
Expand Down Expand Up @@ -104,6 +105,7 @@ var funcMap = map[string]interface{}{
res += kvLine(mdParse.MetaTags, strings.Join(meta.Tags, ","))
res += kvLine(mdParse.MetaFeedbackLink, meta.Feedback)
res += kvLine(mdParse.MetaAnalyticsAccount, meta.GA)
res += kvLine(mdParse.MetaAnalyticsGa4Account, meta.GA4)
res += kvLine(mdParse.MetaSource, meta.Source)
res += kvLine(mdParse.MetaDuration, strconv.Itoa(meta.Duration))

Expand Down
3 changes: 2 additions & 1 deletion claat/render/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
</style>
</head>
<body>
<google-codelab-analytics gaid="{{.GlobalGA}}"></google-codelab-analytics>
<google-codelab-analytics gaid="{{.GlobalGA}}" ga4id="{{.GlobalGA4}}"></google-codelab-analytics>
<google-codelab codelab-gaid="{{.Meta.GA}}"
codelab-ga4id="{{.Meta.GA4}}"
id="{{.Meta.ID}}"
title="{{.Meta.Title}}"
environment="{{index .Env}}"
Expand Down
1 change: 1 addition & 0 deletions claat/types/codelab.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Meta struct {
Tags []string `json:"tags"` // All environments supported by the codelab
Feedback string `json:"feedback,omitempty"` // Issues and bugs are sent here
GA string `json:"ga,omitempty"` // Codelab-specific GA tracking ID
GA4 string `json:"ga4,omitempty"` // Codelab-specific GA4 tracking ID
Extra map[string]string `json:"extra,omitempty"` // Extra metadata specified in pass_metadata

URL string `json:"url"` // Legacy ID; TODO: remove
Expand Down

0 comments on commit 4c98edf

Please sign in to comment.