├── .gitignore ├── LICENSE ├── README.md └── keenio.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Bill Davis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## KeenIO middleware for Beego framework 2 | 3 | [![Codeship Status for pabdavis/beego_keenio](https://codeship.io/projects/88b34820-14f0-0132-a142-66789f1cb1b5/status?branch=master)](https://codeship.io/projects/33558) 4 | [![GoDoc](https://godoc.org/github.com/pabdavis/beego_keenio?status.svg)](https://godoc.org/github.com/pabdavis/beego_keenio) 5 | 6 | Keen IO middleware for Beego framework. The [Keen IO](https://keen.io/) API lets developers build analytics features directly into their apps. 7 | 8 | ### Installation 9 | 10 | Standard `go get`: 11 | 12 | ``` 13 | $ go get github.com/pabdavis/beego_keenio 14 | ``` 15 | 16 | #### Dependencies 17 | 18 | ``` 19 | go get github.com/astaxie/beego 20 | go get github.com/philpearl/keengo 21 | ``` 22 | 23 | beego v1.4.2+ 24 | 25 | ### Usage 26 | 27 | To use this beego middleware with the Keen IO API, you have to configure your Keen IO Project ID and its write access key (if you need an account, [sign up here](https://keen.io/) - it's free). 28 | 29 | This configuration information needs to be added to the conf/app.conf file in your Beego project 30 | 31 | ```ini 32 | KeenioProjectId = XXXXXXXXX 33 | KeenioWriteKey = YYYYYYYYY 34 | ``` 35 | 36 | Beware of whitespace and line breaks in the write key based on it's length. 37 | 38 | #### Configuring Beego Middleware 39 | 40 | Add the following lines into the ```routers/routers.go``` file which will initialize the filter to run on all requests (BeforeRouter and FinishRouter) 41 | 42 | 43 | ```go 44 | import "github.com/pabdavis/beego_keenio" 45 | 46 | func init() { 47 | beego_keenio.InitKeenioFilter() 48 | 49 | 50 | } 51 | ``` 52 | 53 | #### Queueing Events from controller 54 | 55 | Since Keen IO does not force specific tags to be included, this middleware attempts to provide a flexible way for you to format the data you want 56 | to send and it will handle it from there. 57 | 58 | The filter will provide a empty queue via GetData which allows for multiple keen events per controller method to different event collections. 59 | Use the Push method to identify the Keen IO event collection and the data to send to the collection. The data must be an interface that can 60 | be marshaled into JSON, sample uses simplejson. 61 | 62 | ** You must set the variable back into input context using the beego_keenio constant, if not, the events will not be sent to keen io. 63 | 64 | 65 | ```go 66 | 67 | func (this *Controller) SomeMethod() { 68 | 69 | apiData := map[string]interface{}{ 70 | "apikey": api.Key, 71 | "app_name": api.Application.Name, 72 | "username": api.User.Name, 73 | } 74 | dataSet1 := simplejson.New() 75 | dataSet1.Set("api_request", apiData) 76 | 77 | .... 78 | 79 | purchaseData := map[string]interface{}{ 80 | "item_id": item.Key 81 | "qty": 1 82 | "price": 5 83 | } 84 | dataSet2 := simplejson.New() 85 | dataSet2.Set("purchases", purchaseData) 86 | 87 | if keenQ, ok := this.Ctx.Input.GetData(beego_keenio.KEENIO_QUEUE_KEY).(beego_keenio.KeenioQueue); ok { 88 | keenQ.Push("collection1", dataSet1) 89 | keenQ.Push("collection2", dataSet2) 90 | this.Ctx.Input.SetData(beego_keenio.KEENIO_QUEUE_KEY, keenQ) //Must set this back into the defined key 91 | } 92 | 93 | .... 94 | } 95 | 96 | ``` 97 | 98 | That's it! After running your code, check your Keen IO Project to see the event/events has been added. 99 | -------------------------------------------------------------------------------- /keenio.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014 Bill Davis. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package beego_keenio implements a middleware to KeenIO from the beego framework. 6 | package beego_keenio 7 | 8 | import ( 9 | "strings" 10 | "sync" 11 | 12 | "github.com/astaxie/beego" 13 | "github.com/astaxie/beego/context" 14 | "github.com/philpearl/keengo" 15 | ) 16 | 17 | // KEENIO_QUEUE_KEY constant to identify the context key in the request 18 | const ( 19 | KEENIO_QUEUE_KEY = "keenio_queue" 20 | ) 21 | 22 | var sender *keengo.Sender 23 | 24 | // keenioQueue interface for queue 25 | type keenioQueue interface { 26 | Len() int 27 | Push() 28 | Pop() (string, interface{}) 29 | } 30 | 31 | type keenioEvent struct { 32 | collection string 33 | data interface{} 34 | next *keenioEvent 35 | } 36 | 37 | // KeenioQueue is FIFO data stucture 38 | type KeenioQueue struct { 39 | head *keenioEvent 40 | tail *keenioEvent 41 | count int 42 | lock *sync.Mutex 43 | } 44 | 45 | // Creates a new pointer to a new queue. 46 | func newKeenioQueue() *KeenioQueue { 47 | q := &KeenioQueue{} 48 | q.lock = &sync.Mutex{} 49 | return q 50 | } 51 | 52 | // Len returns the number of events in the queue 53 | func (q *KeenioQueue) Len() int { 54 | q.lock.Lock() 55 | defer q.lock.Unlock() 56 | return q.count 57 | } 58 | 59 | // Push adds event to the end of the queue 60 | func (q *KeenioQueue) Push(collection string, item interface{}) { 61 | q.lock.Lock() 62 | defer q.lock.Unlock() 63 | 64 | n := &keenioEvent{collection: collection, data: item} 65 | 66 | if q.tail == nil { 67 | q.tail = n 68 | q.head = n 69 | } else { 70 | q.tail.next = n 71 | q.tail = n 72 | } 73 | q.count++ 74 | } 75 | 76 | // Pop returns event from the top of the queue 77 | func (q *KeenioQueue) Pop() (string, interface{}) { 78 | q.lock.Lock() 79 | defer q.lock.Unlock() 80 | 81 | if q.head == nil { 82 | return "", nil 83 | } 84 | 85 | n := q.head 86 | q.head = n.next 87 | 88 | if q.head == nil { 89 | q.tail = nil 90 | } 91 | q.count-- 92 | 93 | return n.collection, n.data 94 | } 95 | 96 | // InitKeenioQueue initialize the queue structure for this request 97 | func InitKeenioQueue(ctx *context.Context) { 98 | q := newKeenioQueue() 99 | ctx.Input.SetData(KEENIO_QUEUE_KEY, *q) 100 | } 101 | 102 | // ProcessKeenioQueue iterates the queue structure for this request 103 | func ProcessKeenioQueue(ctx *context.Context) { 104 | 105 | if q, ok := ctx.Input.GetData(KEENIO_QUEUE_KEY).(KeenioQueue); ok { 106 | cnt := q.Len() 107 | for i := 0; i < cnt; i++ { 108 | coll, data := q.Pop() 109 | if coll != "" && data != nil { 110 | sender.Queue(coll, data) 111 | } 112 | } 113 | } 114 | } 115 | 116 | // InitKeenioFilter initializes the keengo sender in a go-routine 117 | func InitKeenioFilter() { 118 | 119 | // validate the necessary configuration 120 | projectId := beego.AppConfig.String("KeenioProjectId") 121 | if projectId == "" { 122 | beego.Warn("Please specify Keenio Project ID in the application config: KeenioProjectId=53dfa0000000000000000002") 123 | return 124 | } 125 | 126 | writeKey := beego.AppConfig.String("KeenioWriteKey") 127 | // easy to get whitespace in the write key based on length 128 | writeKey = strings.Replace(writeKey, " ", "", -1) 129 | 130 | if writeKey == "" { 131 | beego.Warn("Please specify Keenio Write Key in the application config: KeenioWriteKey=d21785d8ade08c6f5116b39eed701ff4dbe978688333sefd1a550788e09486c1a40cf1d48f56f1feee730ea4710a081f6631bc1b649847e8937d8be2953e1df9dc8a89c5a69f6d6ad18c6381739f3ab21bd90c376e07f0bf0fdcb6e9cbb702db1ace3c9a 60d3530fffa18d84c65cb3ee") 132 | return 133 | } 134 | 135 | sender = keengo.NewSender(projectId, writeKey) 136 | 137 | beego.InsertFilter("*", beego.BeforeRouter, InitKeenioQueue) 138 | beego.InsertFilter("*", beego.FinishRouter, ProcessKeenioQueue, false) 139 | 140 | beego.Info("Keenio filter initialized") 141 | } 142 | --------------------------------------------------------------------------------