gtm

package
v0.0.0-...-21e1e0c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package gtm provides a way to generate Google Tag Manager snippets.

Usage as a server module:

func main() {
  modules := []module.Module{
    gtm.NewModuleFromFlags(),
  }
  server.Main(nil, modules, func(srv *server.Server) error {
    srv.Routes.GET("/", ..., func(c *router.Context) {
      templates.MustRender(c.Context, c.Writer, "page.html", templates.Args{
        "GTMJSSnippet": gtm.JSSnippet(c.Context),
        "GTMNoScriptSnippet": gtm.NoScriptSnippet(c.Context),
        ...
      })
    return nil
  })
  ...
}

templates/page.html:

<html>
  <head>
    {{.GTMJSSnippet}}
    ...
  </head>
  <body>
    {{.GTMNoScriptSnippet}}
    ...
  </body>
</html>

Index

Constants

This section is empty.

Variables

View Source
var ModuleName = module.RegisterName("go.chromium.org/luci/server/gtm")

ModuleName can be used to refer to this module when declaring dependencies.

Functions

func JSSnippet

func JSSnippet(ctx context.Context) template.HTML

JSSnippet returns the GTM snippet to be inserted into the top of the page's HEAD as is.

If the ContainerID isn't configured, this will return an empty template so that it's safe to insert its return value into a page's source regardless.

func NewModuleFromFlags

func NewModuleFromFlags() module.Module

NewModuleFromFlags initializes options through command line flags.

Calling this function registers flags in flag.CommandLine. They are usually parsed in server.Main(...).

func NoScriptSnippet

func NoScriptSnippet(ctx context.Context) template.HTML

NoScriptSnippet returns the GTM snippet that works in a no-script environment to be inserted into the top of the page's BODY as is.

If the ContainerID isn't configured, this will return an empty template so that it's safe to insert its return value into a page's source regardless.

Types

type ModuleOptions

type ModuleOptions struct {
	// ContainerID is the Google Tag Manager container ID to use like
	// "GTM-XXXXXX".
	//
	// Default is empty, meaning Google Tag Manager integration is disabled.
	ContainerID string
}

ModuleOptions contain configuration of the analytics server module.

func (*ModuleOptions) Register

func (o *ModuleOptions) Register(f *flag.FlagSet)

Register registers the command line flags.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL