beego_keenio

package module
v0.0.0-...-63d379f Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2014 License: MIT Imports: 5 Imported by: 0

README

KeenIO middleware for Beego framework

Codeship Status for pabdavis/beego_keenio GoDoc

Keen IO middleware for Beego framework. The Keen IO API lets developers build analytics features directly into their apps.

Installation

Standard go get:

$ go get github.com/pabdavis/beego_keenio
Dependencies
  go get github.com/astaxie/beego 
  go get github.com/philpearl/keengo

beego v1.4.2+

Usage

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 - it's free).

This configuration information needs to be added to the conf/app.conf file in your Beego project

 KeenioProjectId = XXXXXXXXX
 KeenioWriteKey =  YYYYYYYYY

Beware of whitespace and line breaks in the write key based on it's length.

Configuring Beego Middleware

Add the following lines into the routers/routers.go file which will initialize the filter to run on all requests (BeforeRouter and FinishRouter)

 import "github.com/pabdavis/beego_keenio"
 
 func init() {
    beego_keenio.InitKeenioFilter()

  
 }
Queueing Events from controller

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 to send and it will handle it from there.

The filter will provide a empty queue via GetData which allows for multiple keen events per controller method to different event collections. 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 be marshaled into JSON, sample uses simplejson.

** 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.


    func (this *Controller) SomeMethod() {
       
        apiData := map[string]interface{}{
            "apikey":   api.Key, 
            "app_name": api.Application.Name,
            "username": api.User.Name,
        }
        dataSet1 := simplejson.New()
        dataSet1.Set("api_request", apiData)
    
        ....

        purchaseData := map[string]interface{}{
            "item_id": item.Key
            "qty": 1
            "price": 5
        }
        dataSet2 := simplejson.New()
        dataSet2.Set("purchases", purchaseData)

        if keenQ, ok := this.Ctx.Input.GetData(beego_keenio.KEENIO_QUEUE_KEY).(beego_keenio.KeenioQueue); ok {
            keenQ.Push("collection1", dataSet1)
            keenQ.Push("collection2", dataSet2)
            this.Ctx.Input.SetData(beego_keenio.KEENIO_QUEUE_KEY, keenQ) //Must set this back into the defined key
        }

        ....
    }

That's it! After running your code, check your Keen IO Project to see the event/events has been added.

Documentation

Overview

Package beego_keenio implements a middleware to KeenIO from the beego framework.

Index

Constants

View Source
const (
	KEENIO_QUEUE_KEY = "keenio_queue"
)

KEENIO_QUEUE_KEY constant to identify the context key in the request

Variables

This section is empty.

Functions

func InitKeenioFilter

func InitKeenioFilter()

InitKeenioFilter initializes the keengo sender in a go-routine

func InitKeenioQueue

func InitKeenioQueue(ctx *context.Context)

InitKeenioQueue initialize the queue structure for this request

func ProcessKeenioQueue

func ProcessKeenioQueue(ctx *context.Context)

ProcessKeenioQueue iterates the queue structure for this request

Types

type KeenioQueue

type KeenioQueue struct {
	// contains filtered or unexported fields
}

KeenioQueue is FIFO data stucture

func (*KeenioQueue) Len

func (q *KeenioQueue) Len() int

Len returns the number of events in the queue

func (*KeenioQueue) Pop

func (q *KeenioQueue) Pop() (string, interface{})

Pop returns event from the top of the queue

func (*KeenioQueue) Push

func (q *KeenioQueue) Push(collection string, item interface{})

Push adds event to the end of the queue

Jump to

Keyboard shortcuts

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