| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package analytics |
| 6 |
| 7 // analytics.go is contains public functions for getting |
| 8 // the Google Analytics ID and javascript snippets out from the admin settings. |
| 9 |
| 10 import ( |
| 11 "fmt" |
| 12 "html/template" |
| 13 |
| 14 "golang.org/x/net/context" |
| 15 |
| 16 "github.com/luci/luci-go/common/logging" |
| 17 ) |
| 18 |
| 19 // ID returns the Google Analytics ID if it's set, and "" otherwise. |
| 20 func ID(c context.Context) string { |
| 21 return fetchCachedSettings(c).AnalyticsID |
| 22 } |
| 23 |
| 24 // Snippet returns the html snippet for Google Analytics, including the |
| 25 // <script> tag and ID, if ID is set. |
| 26 func Snippet(c context.Context) template.HTML { |
| 27 id := ID(c) |
| 28 if id == "" { |
| 29 return "" |
| 30 } |
| 31 if !rAllowed.MatchString(id) { |
| 32 logging.Errorf(c, "Analytics ID %s does not match UA-\\d+-\\d+",
id) |
| 33 return "" |
| 34 } |
| 35 return template.HTML(fmt.Sprintf(` |
| 36 <script> |
| 37 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||functio
n(){ |
| 38 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createEleme
nt(o), |
| 39 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefo
re(a,m) |
| 40 })(window,document,'script','https://www.google-analytics.com/analytics.
js','ga'); |
| 41 |
| 42 ga('create', '%s', 'auto'); |
| 43 ga('send', 'pageview'); |
| 44 </script> |
| 45 `, id)) |
| 46 } |
| OLD | NEW |