feat: add optional mpv2 user data transformation

This commit is contained in:
Kevin Franklin Kim
2025-04-11 10:26:47 +02:00
parent 9dc885eeb7
commit 7acd33aa8c
9 changed files with 104 additions and 81 deletions
+2 -1
View File
@@ -15,6 +15,7 @@ import (
microsoftadsprovider "github.com/foomo/sesamy-cli/pkg/provider/microsoftads"
tracifyprovider "github.com/foomo/sesamy-cli/pkg/provider/tracify"
umamiprovider "github.com/foomo/sesamy-cli/pkg/provider/umami"
ptermx "github.com/foomo/sesamy-cli/pkg/pterm"
"github.com/foomo/sesamy-cli/pkg/tagmanager"
"github.com/foomo/sesamy-cli/pkg/utils"
"github.com/pkg/errors"
@@ -149,7 +150,7 @@ func NewServer(l *slog.Logger) *cobra.Command {
tree.Children = append(tree.Children, child)
}
if err := pterm.DefaultTree.WithRoot(tree).WithWriter(utils.NewPTermWriter(pterm.Warning)).Render(); err != nil {
if err := pterm.DefaultTree.WithRoot(tree).WithWriter(ptermx.NewWriter(pterm.Warning)).Render(); err != nil {
l.Warn("failed to render missed resources", "error", err)
}
}
+2 -1
View File
@@ -11,6 +11,7 @@ import (
googletagprovider "github.com/foomo/sesamy-cli/pkg/provider/googletag"
googletagmanagerprovider "github.com/foomo/sesamy-cli/pkg/provider/googletagmanager"
hotjarprovider "github.com/foomo/sesamy-cli/pkg/provider/hotjar"
ptermx "github.com/foomo/sesamy-cli/pkg/pterm"
"github.com/foomo/sesamy-cli/pkg/tagmanager"
"github.com/foomo/sesamy-cli/pkg/utils"
"github.com/pkg/errors"
@@ -117,7 +118,7 @@ func NewWeb(l *slog.Logger) *cobra.Command {
}
tree.Children = append(tree.Children, child)
}
if err := pterm.DefaultTree.WithRoot(tree).WithWriter(utils.NewPTermWriter(pterm.Warning)).Render(); err != nil {
if err := pterm.DefaultTree.WithRoot(tree).WithWriter(ptermx.NewWriter(pterm.Warning)).Render(); err != nil {
l.Warn("failed to render missed resources", "error", err)
}
}
-55
View File
@@ -1,40 +1,13 @@
package cmd
import (
"fmt"
"log/slog"
"os"
"runtime/debug"
"strings"
cowsay "github.com/Code-Hex/Neo-cowsay/v2"
cmdx "github.com/foomo/sesamy-cli/pkg/cmd"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
l *slog.Logger
root *cobra.Command
)
func init() {
l = cmdx.NewLogger()
root = NewRoot(l)
root.AddCommand(
NewConfig(l),
NewConfig(l),
NewList(l),
NewProvision(l),
NewTags(l),
NewTypeScript(l),
NewVersion(l),
)
}
// NewRoot represents the base command when called without any subcommands
func NewRoot(l *slog.Logger) *cobra.Command {
c := viper.New()
@@ -56,31 +29,3 @@ func NewRoot(l *slog.Logger) *cobra.Command {
return cmd
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
say := func(msg string) string {
if say, cerr := cowsay.Say(msg, cowsay.BallonWidth(80)); cerr == nil {
msg = say
}
return msg
}
code := 0
defer func() {
if r := recover(); r != nil {
pterm.Error.Println(say("It's time to panic"))
pterm.Error.Println(fmt.Sprintf("%v", r))
pterm.Error.Println(string(debug.Stack()))
code = 1
}
os.Exit(code)
}()
if err := root.Execute(); err != nil {
pterm.Error.Println(say(strings.Split(errors.Cause(err).Error(), ":")[0]))
pterm.Error.Println(err.Error())
code = 1
}
}
+47 -2
View File
@@ -1,7 +1,52 @@
package main
import "github.com/foomo/sesamy-cli/cmd"
import (
"fmt"
"os"
"runtime/debug"
"strings"
cowsay "github.com/Code-Hex/Neo-cowsay/v2"
"github.com/foomo/sesamy-cli/cmd"
cmdx "github.com/foomo/sesamy-cli/pkg/cmd"
"github.com/pkg/errors"
)
func main() {
cmd.Execute()
l := cmdx.NewLogger()
root := cmd.NewRoot(l)
root.AddCommand(
cmd.NewConfig(l),
cmd.NewConfig(l),
cmd.NewList(l),
cmd.NewProvision(l),
cmd.NewTags(l),
cmd.NewTypeScript(l),
cmd.NewVersion(l),
)
say := func(msg string) string {
if say, cerr := cowsay.Say(msg, cowsay.BallonWidth(80)); cerr == nil {
msg = say
}
return msg
}
code := 0
defer func() {
if r := recover(); r != nil {
l.Error(say("It's time to panic"))
l.Error(fmt.Sprintf("%v", r))
l.Error(string(debug.Stack()))
code = 1
}
os.Exit(code)
}()
if err := root.Execute(); err != nil {
l.Error(say(strings.Split(errors.Cause(err).Error(), ":")[0]))
l.Error(err.Error())
code = 1
}
}
+2
View File
@@ -11,6 +11,8 @@ type GoogleAnalytics struct {
GoogleConsent GoogleConsent `json:"googleConsent" yaml:"googleConsent"`
// GTag.js override configuration
GoogleGTagJSOverride GoogleAnalyticsGTagJSOverride `json:"googleGTagJSOverride" yaml:"googleGTagJSOverride"`
// Enable mpv2 user data transformation (experimental)
EnableMPv2UserDataTransformation bool `json:"enableMPv2UserDataTransformation" yaml:"enableMPv2UserDataTransformation"`
// Google Tag Manager web container settings
WebContainer contemplate.Config `json:"webContainer" yaml:"webContainer"`
// Google Tag Manager server container settings
+4 -4
View File
@@ -37,10 +37,10 @@ func Server(ctx context.Context, l *slog.Logger, tm *tagmanager.TagManager, cfg
}
template, err := tm.LookupTemplate(ctx, NameConversionsAPITagTemplate)
if err != nil {
if errors.Is(err, tagmanager.ErrNotFound) {
l.Warn("Please install the 'Conversion API' template manually first")
}
if errors.Is(err, tagmanager.ErrNotFound) {
l.Warn("Please install the 'Conversions API Tag' by 'facebookincubator' template manually first")
return err
} else if err != nil {
return err
}
+20 -18
View File
@@ -59,27 +59,29 @@ func Server(ctx context.Context, tm *tagmanager.TagManager, cfg config.GoogleAna
return err
}
userDataTemplate, err := tm.UpsertCustomTemplate(ctx, servertemplate.NewJSONRequestValue(NameJSONRequestValueTemplate))
if err != nil {
return err
}
if cfg.EnableMPv2UserDataTransformation {
userDataTemplate, err := tm.UpsertCustomTemplate(ctx, servertemplate.NewJSONRequestValue(NameJSONRequestValueTemplate))
if err != nil {
return err
}
userDataVariable, err := tm.UpsertVariable(ctx, folder, servervariable.NewMPv2Data("user_data", userDataTemplate))
if err != nil {
return err
}
userDataVariable, err := tm.UpsertVariable(ctx, folder, servervariable.NewMPv2Data("user_data", userDataTemplate))
if err != nil {
return err
}
debugModeVariable, err := tm.UpsertVariable(ctx, folder, servervariable.NewMPv2Data("debug_mode", userDataTemplate))
if err != nil {
return err
}
debugModeVariable, err := tm.UpsertVariable(ctx, folder, servervariable.NewMPv2Data("debug_mode", userDataTemplate))
if err != nil {
return err
}
_, err = tm.UpsertTransformation(ctx, folder, servertransformation.NewMPv2UserData(NameMPv2UserDataTransformation, map[string]*api.Variable{
"user_data": userDataVariable,
"debug_mode": debugModeVariable,
}, client))
if err != nil {
return err
_, err = tm.UpsertTransformation(ctx, folder, servertransformation.NewMPv2UserData(NameMPv2UserDataTransformation, map[string]*api.Variable{
"user_data": userDataVariable,
"debug_mode": debugModeVariable,
}, client))
if err != nil {
return err
}
}
}
+23
View File
@@ -149,41 +149,64 @@ func (t *TagManager) Missed() map[string][]string {
for _, i2 := range t.clients.Misssed() {
ret["Clients"] = append(ret["Clients"], i2.Name)
}
} else {
ret["Clients"] = append(ret["Clients"], "All")
}
if t.folders != nil {
for _, i2 := range t.folders.Misssed() {
ret["Folders"] = append(ret["Folders"], i2.Name)
}
} else {
ret["Folders"] = append(ret["Folders"], "All")
}
if t.variables != nil {
for _, i2 := range t.variables.Misssed() {
ret["Variables"] = append(ret["Variables"], i2.Name)
}
} else {
ret["Variables"] = append(ret["Variables"], "All")
}
if t.builtInVariables != nil {
for _, i2 := range t.builtInVariables.Misssed() {
ret["Built In Variables"] = append(ret["Built In Variables"], i2.Name)
}
} else {
ret["Built In Variables"] = append(ret["Built In Variables"], "All")
}
if t.triggers != nil {
for _, i2 := range t.triggers.Misssed() {
ret["Triggers"] = append(ret["Triggers"], i2.Name)
}
} else {
ret["Triggers"] = append(ret["Triggers"], "All")
}
if t.tags != nil {
for _, i2 := range t.tags.Misssed() {
ret["Tags"] = append(ret["Tags"], i2.Name)
}
} else {
ret["Tags"] = append(ret["Tags"], "All")
}
if t.customTemplates != nil {
for _, i2 := range t.customTemplates.Misssed() {
ret["Custom Templates"] = append(ret["Custom Templates"], i2.Name)
}
} else {
ret["Custom Templates"] = append(ret["Custom Templates"], "All")
}
if t.transformations != nil {
for _, i2 := range t.transformations.Misssed() {
ret["Transformations"] = append(ret["Transformations"], i2.Name)
}
} else {
ret["Transformations"] = append(ret["Transformations"], "All")
}
return ret
}
+4
View File
@@ -426,6 +426,10 @@
"$ref": "#/$defs/github.com.foomo.sesamy-cli.pkg.config.GoogleAnalyticsGTagJSOverride",
"description": "GTag.js override configuration"
},
"enableMPv2UserDataTransformation": {
"type": "boolean",
"description": "Enable mpv2 user data transformation (experimental)"
},
"webContainer": {
"$ref": "#/$defs/github.com.foomo.gocontemplate.pkg.contemplate.Config",
"description": "Google Tag Manager web container settings"