Snippets

Variable, Inc. AWS Mobile Analytics Post Event (not working)

Created by Wade Gasior

File analytics-test.go Added

  • Ignore whitespace
  • Hide word diff
+package ccanalytics
+
+import (
+	"testing"
+)
+
+func TestPushEvent(t *testing.T) {
+	event := dummyEvent()
+
+	PostEvent(event)
+}

File analytics.go Added

  • Ignore whitespace
  • Hide word diff
+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 = &timestamp
+
+	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)
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.