Skip to content

Commit

Permalink
Init code
Browse files Browse the repository at this point in the history
  • Loading branch information
Guan Wei committed Apr 18, 2018
1 parent f0a0d1b commit 045cd1f
Show file tree
Hide file tree
Showing 27 changed files with 1,984 additions and 1 deletion.
13 changes: 13 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2015 SensorsData Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
# sa-sdk-go
# Sensors Analytics

This is the official Golang SDK for Sensors Analytics.

## Easy Installation

You can get Sensors Analytics SDK using go.

```
go get github.com/sensorsdata/sa-sdk-go
```
Or update sdk with
```
go get -u github.com/sensorsdata/sa-sdk-go
```

Once the SDK is successfully installed, use the Sensors Analytics SDK likes:

```golang
import sdk "github.com/sensorsdata/sa-sdk-go"

// Gets the url of Sensors Analytics in the home page.
SA_SERVER_URL = 'YOUR_SERVER_URL'

// Initialized the Sensors Analytics SDK with Default Consumer
consumer = sdk.InitDefaultConsumer(SA_SERVER_URL)
sa = sdk.InitSensorsAnalytics(consumer)

properties := map[string]interface{}{
"price": 12,
"name": "apple",
"somedata": []string{"a", "b"},
}

// Track the event 'ServerStart'
sa.track("ABCDEFG1234567", "ServerStart", properties, false)

sa.Close()
```

## More Examples
([Examples](_examples))

## To Learn More
See our [full manual](http://www.sensorsdata.cn/manual/golang_sdk.html)

14 changes: 14 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=====================
Sensors Analytics SDK
=====================

Sensors Analytics SDK
=====================

This is the official Golang SDK for Sensors Analytics.

To Learn More
-------------

See our `full manual <http://www.sensorsdata.cn/manual/golang_sdk.html>`_.

45 changes: 45 additions & 0 deletions _examples/example_batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main
import (
"fmt"
sdk "github.com/sensorsdata/sa-sdk-go"
)

func main() {
c, err := sdk.InitBatchConsumer("http://localhost:8106/sa?project=production", 3, 1000)
if err != nil {
fmt.Println(err)
return
}

sa := sdk.InitSensorsAnalytics(c, "default", false)
defer sa.Close()

distinctId := "ABCDEF123456"
event := "ViewProduct"
properties := map[string]interface{}{
"$ip": "2.2.2.2",
"ProductId": "123456",
"ProductCatalog": "Laptop Computer",
"IsAddedToFav": true,
}

err = sa.Track(distinctId, event, properties, true)
if err != nil {
fmt.Println("track failed", err)
return
}

err = sa.Track(distinctId, event, properties, true)
if err != nil {
fmt.Println("track failed", err)
return
}

err = sa.Track(distinctId, event, properties, true)
if err != nil {
fmt.Println("track failed", err)
return
}

fmt.Println("track done")
}
33 changes: 33 additions & 0 deletions _examples/example_concurrentlogging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main
import (
"fmt"
sdk "github.com/sensorsdata/sa-sdk-go"
)

func main() {
c, err := sdk.InitConcurrentLoggingConsumer("./log.data", false)
if err != nil {
fmt.Println(err)
return
}

sa := sdk.InitSensorsAnalytics(c, "default", false)
defer sa.Close()

distinctId := "ABCDEF123456"
event := "ViewProduct"
properties := map[string]interface{}{
"$ip": "2.2.2.2",
"ProductId": "123456",
"ProductCatalog": "Laptop Computer",
"IsAddedToFav": true,
}

err = sa.Track(distinctId, event, properties, true)
if err != nil {
fmt.Println("track failed", err)
return
}

fmt.Println("track done")
}
32 changes: 32 additions & 0 deletions _examples/example_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main
import (
"fmt"
sdk "github.com/sensorsdata/sa-sdk-go"
)

func main() {
c, err := sdk.InitDebugConsumer("http://localhost:8106/sa?project=production", false, 1000)
if err != nil {
fmt.Println(err)
return
}

sa := sdk.InitSensorsAnalytics(c, "default", false)

distinctId := "ABCDEF123456777"
event := "ViewInfo"
properties := map[string]interface{}{
"$ip": "2.2.2.2",
"ProductId": "123456",
"ProductCatalog": "Laptop Computer",
"IsAddedToFav": true,
}

err = sa.Track(distinctId, event, properties, false)
if err != nil {
fmt.Println("track failed", err)
return
}

fmt.Println("track done")
}
32 changes: 32 additions & 0 deletions _examples/example_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main
import (
"fmt"
sdk "github.com/sensorsdata/sa-sdk-go"
)

func main() {
c, err := sdk.InitDefaultConsumer("http://localhost:8106/sa", 1000)
if err != nil {
fmt.Println(err)
return
}

sa := sdk.InitSensorsAnalytics(c, "default", false)
defer sa.Close()

distinctId := "12345"
event := "ViewProduct"
properties := map[string]interface{}{
"price": 12,
"name": "apple",
"somedata": []string{"a", "b"},
}

err = sa.Track(distinctId, event, properties, true)
if err != nil {
fmt.Println("track failed", err)
return
}

fmt.Println("track done")
}
34 changes: 34 additions & 0 deletions _examples/example_logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
sdk "github.com/sensorsdata/sa-sdk-go"
)

func main() {
c, err := sdk.InitLoggingConsumer("./log.data", false)
if err != nil {
fmt.Println(err)
return
}

sa := sdk.InitSensorsAnalytics(c, "default", false)
defer sa.Close()

distinctId := "ABCDEF123456"
event := "ViewProduct"
properties := map[string]interface{}{
"$ip": "2.2.2.2",
"ProductId": "123456",
"ProductCatalog": "Laptop Computer",
"IsAddedToFav": true,
}

err = sa.Track(distinctId, event, properties, true)
if err != nil {
fmt.Println("track failed", err)
return
}

fmt.Println("track done")
}
56 changes: 56 additions & 0 deletions consumers/batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package consumers

import (
"time"
"encoding/json"

"github.com/sensorsdata/sa-sdk-go/structs"
)

const (
BATCH_DEFAULT_MAX = 50
)


type BatchConsumer struct {
Url string
Max int
buffer []structs.EventData
Timeout time.Duration
}

func InitBatchConsumer(url string, max, timeout int) (*BatchConsumer, error) {
if max > BATCH_DEFAULT_MAX {
max = BATCH_DEFAULT_MAX
}

c := &BatchConsumer{Url: url, Max: max, Timeout: time.Duration(timeout) * time.Millisecond}
c.buffer = make([]structs.EventData, 0, max)

return c, nil
}

func (c *BatchConsumer) Send(data structs.EventData) error {
c.buffer = append(c.buffer, data)
if len(c.buffer) >= c.Max {
return c.Flush()
}
return nil
}

func (c *BatchConsumer) Flush() error {
jdata, err := json.Marshal(c.buffer)
if err != nil {
return err
}

send(c.Url, string(jdata), c.Timeout, true)

c.buffer = c.buffer[:0]

return nil
}

func (c *BatchConsumer) Close() error {
return c.Flush()
}
Loading

0 comments on commit 045cd1f

Please sign in to comment.