mirror of
https://github.com/foomo/sesamy-cli.git
synced 2025-10-16 12:35:36 +00:00
feat(tagmanager): add mpv2 user data transformation
This commit is contained in:
parent
53e4b278dd
commit
6c47690f64
@ -7,6 +7,8 @@ const (
|
||||
NameGoogleAnalyticsGA4ClientTrigger = "Google Analytics GA4 Client"
|
||||
NameGoogleGTagClientTemplate = "Google gtag.js"
|
||||
NameGoogleGTagClient = "Google gtag.js"
|
||||
NameMPv2UserDataTransformation = "MPv2 User Data"
|
||||
NameJSONRequestValueTemplate = "JSON Request Value"
|
||||
NameMeasurementProtocolGA4Client = "Measurement Protocol GA4"
|
||||
NameMeasurementProtocolGA4ClientTrigger = "Measurement Protocol GA4 Client"
|
||||
)
|
||||
|
||||
@ -2,15 +2,18 @@ package googleanalytics
|
||||
|
||||
import (
|
||||
"github.com/foomo/sesamy-cli/pkg/config"
|
||||
client2 "github.com/foomo/sesamy-cli/pkg/provider/googleanalytics/server/client"
|
||||
googleanalyticsclient "github.com/foomo/sesamy-cli/pkg/provider/googleanalytics/server/client"
|
||||
containertag "github.com/foomo/sesamy-cli/pkg/provider/googleanalytics/server/tag"
|
||||
template2 "github.com/foomo/sesamy-cli/pkg/provider/googleanalytics/server/template"
|
||||
googleanalyticstemplate "github.com/foomo/sesamy-cli/pkg/provider/googleanalytics/server/template"
|
||||
"github.com/foomo/sesamy-cli/pkg/provider/googleanalytics/server/trigger"
|
||||
"github.com/foomo/sesamy-cli/pkg/provider/googleconsent"
|
||||
googleconsentvariable "github.com/foomo/sesamy-cli/pkg/provider/googleconsent/server/variable"
|
||||
"github.com/foomo/sesamy-cli/pkg/tagmanager"
|
||||
serverclient "github.com/foomo/sesamy-cli/pkg/tagmanager/server/client"
|
||||
servertemplate "github.com/foomo/sesamy-cli/pkg/tagmanager/server/template"
|
||||
servertransformation "github.com/foomo/sesamy-cli/pkg/tagmanager/server/transformation"
|
||||
servertrigger "github.com/foomo/sesamy-cli/pkg/tagmanager/server/trigger"
|
||||
servervariable "github.com/foomo/sesamy-cli/pkg/tagmanager/server/variable"
|
||||
"github.com/foomo/sesamy-cli/pkg/utils"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -43,15 +46,30 @@ func Server(tm *tagmanager.TagManager, cfg config.GoogleAnalytics, redactVisitor
|
||||
if _, err = tm.UpsertTrigger(servertrigger.NewClient(NameMeasurementProtocolGA4ClientTrigger, client)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.GoogleGTag.Enabled {
|
||||
template, err := tm.UpsertCustomTemplate(template2.NewGoogleGTagClient(NameGoogleGTagClientTemplate))
|
||||
userDataTemplate, err := tm.UpsertCustomTemplate(servertemplate.NewJSONRequestValue(NameJSONRequestValueTemplate))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tm.UpsertClient(client2.NewGoogleGTag(NameGoogleGTagClient, cfg.GoogleGTag, template))
|
||||
userDataVariable, err := tm.UpsertVariable(servervariable.NewMPv2Data("user_data", userDataTemplate))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tm.UpsertTransformation(servertransformation.NewMPv2UserData(NameMPv2UserDataTransformation, userDataVariable, client))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.GoogleGTag.Enabled {
|
||||
template, err := tm.UpsertCustomTemplate(googleanalyticstemplate.NewGoogleGTagClient(NameGoogleGTagClientTemplate))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tm.UpsertClient(googleanalyticsclient.NewGoogleGTag(NameGoogleGTagClient, cfg.GoogleGTag, template))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -10,13 +10,5 @@ func NewGoogleConsentModeCheck(name string) *tagmanager.CustomTemplate {
|
||||
return &tagmanager.CustomTemplate{
|
||||
Name: name,
|
||||
TemplateData: fmt.Sprintf(GoogleConsentModeCheckData, name),
|
||||
// oogleapi: Error 400: galleryReference: This field is invalid (or unsupported).
|
||||
// GalleryReference: &tagmanager.GalleryReference{
|
||||
// Host: "github.com",
|
||||
// Owner: "analytics-engineers",
|
||||
// Repository: "gtm-server-variable-google-consent-mode-check",
|
||||
// Signature: "8905ba41f72b510484a3ff9dc27dabaf09c029eb1228e2d1435b5cc2e837cc8d",
|
||||
// Version: "a31230ca43cdadea1b97ef7dcf76b8e9f8c04725",
|
||||
// },
|
||||
}
|
||||
}
|
||||
|
||||
14
pkg/tagmanager/server/template/jsonrequestvalue.go
Normal file
14
pkg/tagmanager/server/template/jsonrequestvalue.go
Normal file
@ -0,0 +1,14 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/api/tagmanager/v2"
|
||||
)
|
||||
|
||||
func NewJSONRequestValue(name string) *tagmanager.CustomTemplate {
|
||||
return &tagmanager.CustomTemplate{
|
||||
Name: name,
|
||||
TemplateData: fmt.Sprintf(JSONRequestValueData, name),
|
||||
}
|
||||
}
|
||||
126
pkg/tagmanager/server/template/jsonrequestvaluedata.go
Normal file
126
pkg/tagmanager/server/template/jsonrequestvaluedata.go
Normal file
File diff suppressed because one or more lines are too long
89
pkg/tagmanager/server/transformation/mpv2userdata.go
Normal file
89
pkg/tagmanager/server/transformation/mpv2userdata.go
Normal file
@ -0,0 +1,89 @@
|
||||
package transformation
|
||||
|
||||
import (
|
||||
"google.golang.org/api/tagmanager/v2"
|
||||
)
|
||||
|
||||
func NewMPv2UserData(name string, variable *tagmanager.Variable, client *tagmanager.Client) *tagmanager.Transformation {
|
||||
return &tagmanager.Transformation{
|
||||
Name: name,
|
||||
Parameter: []*tagmanager.Parameter{
|
||||
{
|
||||
Key: "matchingConditionsEnabled",
|
||||
Type: "boolean",
|
||||
Value: "true",
|
||||
},
|
||||
{
|
||||
Key: "allTagsExcept",
|
||||
Type: "boolean",
|
||||
Value: "true",
|
||||
},
|
||||
{
|
||||
Key: "booleanExpressionString",
|
||||
Type: "template",
|
||||
},
|
||||
{
|
||||
Key: "augmentEventTable",
|
||||
List: []*tagmanager.Parameter{
|
||||
{
|
||||
IsWeakReference: false,
|
||||
Map: []*tagmanager.Parameter{
|
||||
{
|
||||
Key: "paramName",
|
||||
Type: "template",
|
||||
Value: "user_data",
|
||||
},
|
||||
{
|
||||
Key: "paramValue",
|
||||
Type: "template",
|
||||
Value: "{{" + variable.Name + "}}",
|
||||
},
|
||||
},
|
||||
Type: "map",
|
||||
},
|
||||
},
|
||||
Type: "list",
|
||||
},
|
||||
{
|
||||
Key: "affectedTags",
|
||||
Type: "list",
|
||||
},
|
||||
{
|
||||
Key: "affectedTagTypes",
|
||||
Type: "list",
|
||||
},
|
||||
{
|
||||
Key: "matchingConditionsTable",
|
||||
List: []*tagmanager.Parameter{
|
||||
{
|
||||
Map: []*tagmanager.Parameter{
|
||||
{
|
||||
Key: "variableName",
|
||||
Type: "template",
|
||||
Value: "Client Name",
|
||||
},
|
||||
{
|
||||
Key: "variableReference",
|
||||
Type: "template",
|
||||
Value: "{{Client Name}}",
|
||||
},
|
||||
{
|
||||
Key: "expressionType",
|
||||
Type: "template",
|
||||
Value: "EQUALS",
|
||||
},
|
||||
{
|
||||
Key: "expressionValue",
|
||||
Type: "template",
|
||||
Value: client.Name,
|
||||
},
|
||||
},
|
||||
Type: "map",
|
||||
},
|
||||
},
|
||||
Type: "list",
|
||||
},
|
||||
},
|
||||
Type: "tf_augment_event",
|
||||
}
|
||||
}
|
||||
24
pkg/tagmanager/server/variable/mpv2data.go
Normal file
24
pkg/tagmanager/server/variable/mpv2data.go
Normal file
@ -0,0 +1,24 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"github.com/foomo/sesamy-cli/pkg/utils"
|
||||
"google.golang.org/api/tagmanager/v2"
|
||||
)
|
||||
|
||||
func MPv2DataName(v string) string {
|
||||
return "mpv2." + v
|
||||
}
|
||||
|
||||
func NewMPv2Data(name string, template *tagmanager.CustomTemplate) *tagmanager.Variable {
|
||||
return &tagmanager.Variable{
|
||||
Name: name,
|
||||
Parameter: []*tagmanager.Parameter{
|
||||
{
|
||||
Key: "key",
|
||||
Type: "template",
|
||||
Value: name,
|
||||
},
|
||||
},
|
||||
Type: utils.TemplateType(template),
|
||||
}
|
||||
}
|
||||
@ -31,6 +31,7 @@ type (
|
||||
triggers map[string]*tagmanager.Trigger
|
||||
tags map[string]*tagmanager.Tag
|
||||
customTemplates map[string]*tagmanager.CustomTemplate
|
||||
transformations map[string]*tagmanager.Transformation
|
||||
}
|
||||
Option func(*TagManager)
|
||||
)
|
||||
@ -304,6 +305,19 @@ func (t *TagManager) LookupTemplate(name string) (*tagmanager.CustomTemplate, er
|
||||
return elems[name], nil
|
||||
}
|
||||
|
||||
func (t *TagManager) LookupTransformation(name string) (*tagmanager.Transformation, error) {
|
||||
elems, err := t.LoadTransformations()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, ok := elems[name]; !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
return elems[name], nil
|
||||
}
|
||||
|
||||
func (t *TagManager) LoadTriggers() (map[string]*tagmanager.Trigger, error) {
|
||||
if t.triggers == nil {
|
||||
t.l.Info("🛄 Loading list", "type", "Trigger")
|
||||
@ -384,6 +398,24 @@ func (t *TagManager) LoadCustomTemplates() (map[string]*tagmanager.CustomTemplat
|
||||
return t.customTemplates, nil
|
||||
}
|
||||
|
||||
func (t *TagManager) LoadTransformations() (map[string]*tagmanager.Transformation, error) {
|
||||
if t.transformations == nil {
|
||||
t.l.Info("🛄 Loading list", "type", "Transformation")
|
||||
r, err := t.Service().Accounts.Containers.Workspaces.Transformations.List(t.WorkspacePath()).Do()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := map[string]*tagmanager.Transformation{}
|
||||
for _, value := range r.Transformation {
|
||||
res[value.Name] = value
|
||||
}
|
||||
t.transformations = res
|
||||
}
|
||||
|
||||
return t.transformations, nil
|
||||
}
|
||||
|
||||
func (t *TagManager) UpsertClient(item *tagmanager.Client) (*tagmanager.Client, error) {
|
||||
l := t.l.With("type", "Client", "name", item.Name)
|
||||
|
||||
@ -419,6 +451,41 @@ func (t *TagManager) UpsertClient(item *tagmanager.Client) (*tagmanager.Client,
|
||||
return t.LookupClient(item.Name)
|
||||
}
|
||||
|
||||
func (t *TagManager) UpsertTransformation(item *tagmanager.Transformation) (*tagmanager.Transformation, error) {
|
||||
l := t.l.With("type", "Transformation", "name", item.Name)
|
||||
|
||||
folder, err := t.LookupFolder(t.folderName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to retrieve folder")
|
||||
}
|
||||
item.ParentFolderId = folder.FolderId
|
||||
|
||||
item.Notes = t.Notes(item)
|
||||
item.AccountId = t.AccountID()
|
||||
item.ContainerId = t.ContainerID()
|
||||
item.WorkspaceId = t.WorkspaceID()
|
||||
|
||||
cache, err := t.LookupTransformation(item.Name)
|
||||
if err != nil && !errors.Is(err, ErrNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cache == nil {
|
||||
l.Info("🚀 New")
|
||||
t.transformations[item.Name], err = t.Service().Accounts.Containers.Workspaces.Transformations.Create(t.WorkspacePath(), item).Do()
|
||||
} else if item.Notes == cache.Notes {
|
||||
l.Info("✅ OK", "id", cache.TransformationId)
|
||||
} else {
|
||||
l.Info("🔄 Update", "id", cache.TransformationId)
|
||||
t.transformations[item.Name], err = t.Service().Accounts.Containers.Workspaces.Transformations.Update(t.WorkspacePath()+"/transformations/"+cache.TransformationId, item).Do()
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return t.LookupTransformation(item.Name)
|
||||
}
|
||||
|
||||
func (t *TagManager) UpsertFolder(name string) (*tagmanager.Folder, error) {
|
||||
l := t.l.With("type", "Folder", "name", name)
|
||||
|
||||
|
||||
@ -94,6 +94,15 @@ func TestNewClient_Server(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
{ // --- Transformations ---
|
||||
t.Run("list transformations", func(t *testing.T) {
|
||||
cmd := c.Service().Accounts.Containers.Workspaces.Transformations.List(c.WorkspacePath())
|
||||
if r, err := cmd.Do(); assert.NoError(t, err) {
|
||||
dump(t, r)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
{ // --- Tags ---
|
||||
t.Run("list tags", func(t *testing.T) {
|
||||
cmd := c.Service().Accounts.Containers.Workspaces.Tags.List(c.WorkspacePath())
|
||||
@ -113,7 +122,7 @@ func TestNewClient_Server(t *testing.T) {
|
||||
cmd := c.Service().Accounts.Containers.Workspaces.Templates.List(c.WorkspacePath())
|
||||
if r, err := cmd.Do(); assert.NoError(t, err) {
|
||||
dump(t, r)
|
||||
fmt.Println(r.Template[3].TemplateData)
|
||||
fmt.Println(r.Template[8].TemplateData)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user