Snippets
Created by
Wade Gasior
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | package ccanalytics
import (
"encoding/json"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/mobileanalytics"
)
type q map[string]interface{}
type AnalyticsConfig struct {
Region string
AccessKey string
AccessSecret string
AppID string
}
var ccAnalyticsConfig = AnalyticsConfig{
Region: "us-east-1",
AccessKey: "...",
AccessSecret: "...",
AppID: "...",
}
func client() (*mobileanalytics.MobileAnalytics, error) {
creds := credentials.NewStaticCredentials(ccAnalyticsConfig.AccessKey,
ccAnalyticsConfig.AccessSecret, "")
_, err := creds.Get()
if err != nil {
return nil, err
}
cfg := aws.NewConfig().WithRegion(ccAnalyticsConfig.Region).WithCredentials(creds)
client := mobileanalytics.New(session.New(), cfg)
return client, nil
}
func dummyEvent() *mobileanalytics.Event {
event := mobileanalytics.Event{}
eventType := "DUMMY_EVENT"
eventKey1 := "key_1"
eventVal1 := "val_1"
timestamp := time.Now().UTC().Format(time.RFC3339)
event.EventType = &eventType
event.Attributes = map[string]*string{
eventKey1: &eventVal1,
}
event.Timestamp = ×tamp
return &event
}
func PostEvent(e *mobileanalytics.Event) {
fmt.Printf("posting event %+v\n", e)
c, err := client()
if err != nil {
fmt.Println("err in creating client")
fmt.Println(err)
return
}
pei := mobileanalytics.PutEventsInput{}
pei.Events = []*mobileanalytics.Event{
e,
}
// The client context including the client ID, app title, app version and package
ctx := q{
"client": q{
"client_id": "color-cloud-go",
"app_title": "color-cloud-go",
"app_version_name": "v3.0.1",
"app_version_code": "301",
"app_package_name": "com.variableinc.colorcloudgo",
},
"env": q{
"platform": "go",
"model": "server",
"make": "go-server",
"platform_version": "1.9",
"locale": "en-us",
},
"services": q{
"mobile_analytics": q{
"app_id": ccAnalyticsConfig.AppID,
},
},
}
ctxBytes, _ := json.Marshal(ctx)
ctxString := string(ctxBytes)
// fmt.Println("using JSON:")
// fmt.Println(ctxString)
pei.ClientContext = &ctxString
out, err := c.PutEvents(&pei)
if err != nil {
fmt.Println("err in PutEvents")
fmt.Println(err)
return
}
fmt.Println("Post even success")
fmt.Printf("%+v\n", out)
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.