Skip to content

Commit

Permalink
Merge pull request #69 from foomo/microsoft
Browse files Browse the repository at this point in the history
fix(provider/microsoftads): add provider
  • Loading branch information
franklinkim authored Nov 26, 2024
2 parents c75e99a + dddb3b9 commit fd02274
Show file tree
Hide file tree
Showing 13 changed files with 1,208 additions and 202 deletions.
8 changes: 8 additions & 0 deletions cmd/tagmanager/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
googleadsprovider "github.com/foomo/sesamy-cli/pkg/provider/googleads"
googleanalyticsprovider "github.com/foomo/sesamy-cli/pkg/provider/googleanalytics"
googletagmanagerprovider "github.com/foomo/sesamy-cli/pkg/provider/googletagmanager"
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"
"github.com/foomo/sesamy-cli/pkg/tagmanager"
Expand Down Expand Up @@ -109,6 +110,13 @@ func NewServer(root *cobra.Command) {
}
}

if cfg.MicrosoftAds.Enabled && pkgcmd.Tag(microsoftadsprovider.Tag, tags) {
l.Info("🅿️ Running provider", "name", microsoftadsprovider.Name, "tag", microsoftadsprovider.Tag)
if err := microsoftadsprovider.Server(l, tm, cfg.MicrosoftAds); err != nil {
return errors.Wrap(err, "failed to provision microsoftads")
}
}

return nil
},
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Config struct {
ConversionLinker ConversionLinker `json:"conversionLinker" yaml:"conversionLinker"`
// Facebook provider settings
Facebook Facebook `json:"facebook" yaml:"facebook"`
// MicrosoftAds provider settings
MicrosoftAds MicrosoftAds `json:"microsoftAds" yaml:"microsoftAds"`
// Emarsys provider settings
Emarsys Emarsys `json:"emarsys" yaml:"emarsys"`
// Hotjar provider settings
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/microsoftads.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

type MicrosoftAds struct {
// Enable provider
Enabled bool `json:"enabled" yaml:"enabled"`
// Google Consent settings
GoogleConsent GoogleConsent `json:"googleConsent" yaml:"googleConsent"`
// Microsoft Ads UET Tag ID
TagID string `json:"tagId" yaml:"tagId"`
// Microsoft Ads Conversion settings
Conversion MicrosoftAdsConversion `json:"conversion" yaml:"conversion"`
}
29 changes: 29 additions & 0 deletions pkg/config/microsoftadsconversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

import (
"github.com/foomo/gocontemplate/pkg/contemplate"
)

type MicrosoftAdsConversion struct {
// Enable provider
Enabled bool `json:"enabled" yaml:"enabled"`
// Google Tag Manager server container settings
ServerContainer MicrosoftAdsServerContainer `json:"serverContainer" yaml:"serverContainer"`
}

type MicrosoftAdsServerContainer struct {
contemplate.Config `json:",inline" yaml:",squash"`
Settings map[string]MicrosoftAdsConversionTag `json:"settings" yaml:"settings"`
}

type MicrosoftAdsConversionTag struct {
PageType string `json:"pageType" yaml:"pageType"`
EventType string `json:"eventType" yaml:"eventType"`
}

func (s *MicrosoftAdsServerContainer) Setting(eventName string) MicrosoftAdsConversionTag {
if value, ok := s.Settings[eventName]; ok {
return value
}
return MicrosoftAdsConversionTag{}
}
200 changes: 0 additions & 200 deletions pkg/provider/microsoft/server/template/data/conversion/code.js

This file was deleted.

8 changes: 8 additions & 0 deletions pkg/provider/microsoftads/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package facebook

const (
Tag = "mads"
Name = "Microsoft Ads"
NameTagIDConstant = "Microsoft UET Tag ID"
NameConversionsTagTemplate = "Microsoft Ads Conversions"
)
70 changes: 70 additions & 0 deletions pkg/provider/microsoftads/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package facebook

import (
"log/slog"

"github.com/foomo/sesamy-cli/pkg/config"
"github.com/foomo/sesamy-cli/pkg/provider/googleconsent"
googleconsentvariable "github.com/foomo/sesamy-cli/pkg/provider/googleconsent/server/variable"
servertagx "github.com/foomo/sesamy-cli/pkg/provider/microsoftads/server/tag"
"github.com/foomo/sesamy-cli/pkg/provider/microsoftads/server/template"
"github.com/foomo/sesamy-cli/pkg/provider/microsoftads/server/trigger"
"github.com/foomo/sesamy-cli/pkg/tagmanager"
commonvariable "github.com/foomo/sesamy-cli/pkg/tagmanager/common/variable"
"github.com/foomo/sesamy-cli/pkg/utils"
"github.com/pkg/errors"
)

func Server(l *slog.Logger, tm *tagmanager.TagManager, cfg config.MicrosoftAds) error {
{ // create folder
if folder, err := tm.UpsertFolder("Sesamy - " + Name); err != nil {
return err
} else {
tm.SetFolderName(folder.Name)
}
}

tagID, err := tm.UpsertVariable(commonvariable.NewConstant(NameTagIDConstant, cfg.TagID))
if err != nil {
return err
}

if cfg.Conversion.Enabled {
tagTemplate, err := tm.UpsertCustomTemplate(template.NewConversionTag(NameConversionsTagTemplate))
if err != nil {
return err
}

{ // create tags
eventParameters, err := utils.LoadEventParams(cfg.Conversion.ServerContainer.Config)
if err != nil {
return err
}

for event := range eventParameters {
var eventTriggerOpts []trigger.ConversionEventOption
if cfg.GoogleConsent.Enabled {
if err := googleconsent.ServerEnsure(tm); err != nil {
return err
}
consentVariable, err := tm.LookupVariable(googleconsentvariable.GoogleConsentModeName(cfg.GoogleConsent.Mode))
if err != nil {
return err
}
eventTriggerOpts = append(eventTriggerOpts, trigger.ConversionEventWithConsentMode(consentVariable))
}

eventTrigger, err := tm.UpsertTrigger(trigger.NewConversionEvent(event, eventTriggerOpts...))
if err != nil {
return errors.Wrap(err, "failed to upsert event trigger: "+event)
}

if _, err := tm.UpsertTag(servertagx.NewConversion(event, tagID, tagTemplate, cfg.Conversion.ServerContainer.Setting(event), eventTrigger)); err != nil {
return err
}
}
}
}

return nil
}
Loading

0 comments on commit fd02274

Please sign in to comment.