add pocketbase
This commit is contained in:
56
backend/custom/gtm/gtm.go
Normal file
56
backend/custom/gtm/gtm.go
Normal file
@ -0,0 +1,56 @@
|
||||
package gtm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"text/template"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
func ServeTagMangerJS(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
return se.Router.GET("/api/gtm/script.js", func(e *core.RequestEvent) error {
|
||||
|
||||
settings, err := GetSettings(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := JS.ReadFile("gtm.js")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
templ, err := template.New("test").Parse(string(file))
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
templ.Execute(&buf, map[string]interface{}{
|
||||
"GtagID": settings.GtagID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e.Response.Header().Set("Content-Type", "application/javascript")
|
||||
return e.String(http.StatusOK, buf.String())
|
||||
})
|
||||
}
|
||||
|
||||
type GTagSettings struct {
|
||||
GtagID string `json:"gtagID"`
|
||||
}
|
||||
|
||||
func GetSettings(app *pocketbase.PocketBase) (*GTagSettings, error) {
|
||||
record, err := app.FindFirstRecordByFilter("settings", "key='gtagID'", nil)
|
||||
|
||||
settings := GTagSettings{}
|
||||
json.Unmarshal([]byte(record.GetString("value")), &settings)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &settings, nil
|
||||
}
|
29
backend/custom/gtm/gtm.js
Normal file
29
backend/custom/gtm/gtm.js
Normal file
@ -0,0 +1,29 @@
|
||||
export class GTM {
|
||||
gtagID = "{{.GtagID}}";
|
||||
|
||||
constructor() {
|
||||
this.insertScript(window, document, 'script', 'dataLayer', this.gtagID);
|
||||
this.insertNoScript();
|
||||
}
|
||||
|
||||
|
||||
insertScript(w, d, s, l, i) {
|
||||
w[l] = w[l] || []; w[l].push({
|
||||
'gtm.start':
|
||||
new Date().getTime(), event: 'gtm.js'
|
||||
}); var f = d.getElementsByTagName(s)[0],
|
||||
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
|
||||
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
|
||||
}
|
||||
|
||||
insertNoScript() {
|
||||
const noscript = document.createElement("noscript");
|
||||
const iframe = document.createElement("iframe");
|
||||
iframe.src = "https://www.googletagmanager.com/ns.html?id=" + this.gtagID;
|
||||
iframe.height = 0;
|
||||
iframe.width = 0;
|
||||
iframe.style = "display:none;visibility:hidden";
|
||||
noscript.appendChild(iframe);
|
||||
document.body.appendChild(noscript);
|
||||
}
|
||||
}
|
8
backend/custom/gtm/js.go
Normal file
8
backend/custom/gtm/js.go
Normal file
@ -0,0 +1,8 @@
|
||||
package gtm
|
||||
|
||||
import (
|
||||
"embed"
|
||||
)
|
||||
|
||||
//go:embed gtm.js
|
||||
var JS embed.FS
|
Reference in New Issue
Block a user