analytics.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Package analytics provides functions to send analytics to analytics stores
  2. // such as Google Analytics.
  3. package analytics
  4. import (
  5. "fmt"
  6. "vultras.su/core/pwa/app"
  7. )
  8. // Backend is the interface that describes an analytics backend that sends
  9. // analytics for a defined provider.
  10. type Backend interface {
  11. // Links your users, and their actions, to a recognizable userID and traits.
  12. Identify(userID string, traits map[string]interface{})
  13. // Record actions your users perform.
  14. Track(event string, properties map[string]interface{})
  15. // Records page views on your website, along with optional extra information
  16. // about the page viewed by the user.
  17. Page(name string, properties map[string]interface{})
  18. }
  19. // Identify links your users, and their actions, to a recognizable userID and
  20. // traits.
  21. func Identify(userID string, traits map[string]interface{}) {
  22. if app.IsServer || userID == "" {
  23. return
  24. }
  25. traits = sanitizeValue(traits).(map[string]interface{})
  26. for _, b := range backends {
  27. b.Identify(userID, traits)
  28. }
  29. }
  30. // Track record actions your users perform.
  31. func Track(event string, properties map[string]interface{}) {
  32. if app.IsServer || event == "" {
  33. return
  34. }
  35. properties = sanitizeValue(properties).(map[string]interface{})
  36. for _, b := range backends {
  37. b.Track(event, properties)
  38. }
  39. }
  40. // Page records page views on your website, along with optional extra
  41. // information about the page viewed by the user.
  42. //
  43. // The following properties are automatically set: path, referrer, search, title
  44. // and url.
  45. func Page(name string, properties map[string]interface{}) {
  46. if app.IsServer {
  47. return
  48. }
  49. if properties == nil {
  50. properties = make(map[string]interface{}, 5)
  51. }
  52. properties["path"] = app.Window().Get("location").Get("pathname").String()
  53. properties["referrer"] = app.Window().Get("document").Get("referrer").String()
  54. properties["search"] = app.Window().Get("location").Get("search").String()
  55. properties["title"] = app.Window().Get("document").Get("title").String()
  56. properties["url"] = app.Window().Get("location").Get("href").String()
  57. if name == "" {
  58. name = app.Window().Get("document").Get("title").String()
  59. }
  60. properties = sanitizeValue(properties).(map[string]interface{})
  61. for _, b := range backends {
  62. b.Page(name, properties)
  63. }
  64. }
  65. // Add adds the given backend to the backends used to send analytics.
  66. func Add(b Backend) {
  67. backends = append(backends, b)
  68. }
  69. var (
  70. backends []Backend
  71. )
  72. func sanitizeValue(v interface{}) interface{} {
  73. switch v := v.(type) {
  74. case app.Value,
  75. app.Func,
  76. nil,
  77. string,
  78. bool,
  79. int,
  80. int8,
  81. int16,
  82. int32,
  83. int64,
  84. uint,
  85. uint8,
  86. uint16,
  87. uint32,
  88. uint64,
  89. float32,
  90. float64:
  91. return v
  92. case []interface{}:
  93. for i, item := range v {
  94. v[i] = sanitizeValue(item)
  95. }
  96. return v
  97. case map[string]interface{}:
  98. for k, val := range v {
  99. v[k] = sanitizeValue(val)
  100. }
  101. return v
  102. default:
  103. return fmt.Sprintf("%v", v)
  104. }
  105. }