-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add opt-out analytics to
diez
(#92)
- Add a `serverless` back end for pinging Amplitude with `diez` CLI activity events. - Activate opt-out analytics every time the `diez` CLI is bootstrapped. - Add a command, `diez analytics <on|off>`, for easily enabling/disabling analytics.
- Loading branch information
Showing
44 changed files
with
610 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
TODO | ||
Refer to Beta Software License Agreement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Serverless directories | ||
.serverless | ||
|
||
# golang output binary directory | ||
bin | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
build: | ||
env GOOS=linux go build -ldflags="-s -w" -o bin/ping ping/main.go | ||
|
||
clean: | ||
rm -rf ./bin | ||
|
||
deploy-dev: clean build | ||
sls deploy --verbose --aws-profile diez-serverless --stage dev | ||
|
||
deploy: clean build | ||
sls deploy --verbose --aws-profile diez-serverless --stage prod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# `@diez/analytics` | ||
|
||
A simple analytics back end for Diez built with [`serverless`](https://serverless.com). | ||
|
||
This function provides a simple anonymizing relay to Amplitude devoid of any PII. | ||
|
||
The following environment is required in order to deploy the analytics back end to AWS: | ||
- Environment variables: | ||
- `DIEZ_HOSTED_ZONE_ID`: a Route 53 Hosted Zone ID for `diez.org`. | ||
- `DIEZ_AMPLITUDE_API_KEY_DEV`: an Amplitude API key for the "dev" stage. | ||
- `DIEZ_AMPLITUDE_API_KEY_PROD`: an Amplitude API key for the "prod" stage. | ||
- AWS profiles: | ||
- `diez-serverless`: a profile with the requisite permissions for `serverless` ([see gist](https://gist.github.com/ServerlessBot/7618156b8671840a539f405dea2704c8)) and `serverless-domain-manager` ([see "Prerequisities"](https://github.com/amplify-education/serverless-domain-manager#prerequisites)). | ||
|
||
First-time activation of domains requires running `create_domain` from `serverless-domain-manager`, e.g.: | ||
|
||
``` | ||
$ sls create_domain --aws-profile diez-serverless --stage dev | ||
``` | ||
|
||
### Deploying | ||
|
||
- Run `make deploy-dev` to deploy the dev stage. | ||
- Run `make deploy` to deploy the prod stage. | ||
- See [`Makefile`](./Makefile) for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/diez/analytics | ||
|
||
require ( | ||
github.com/aws/aws-lambda-go v1.10.0 | ||
github.com/savaki/amplitude-go v0.0.0-20160610055645-f62e3b57c0e4 | ||
github.com/stretchr/testify v1.3.0 // indirect | ||
golang.org/x/net v0.0.0-20190514140710-3ec191127204 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
github.com/aws/aws-lambda-go v1.10.0 h1:uafgdfYGQD0UeT7d2uKdyWW8j/ZYRifRPIdmeqLzLCk= | ||
github.com/aws/aws-lambda-go v1.10.0/go.mod h1:zUsUQhAUjYzR8AuduJPCfhBuKWUaDbQiPOG+ouzmE1A= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/savaki/amplitude-go v0.0.0-20160610055645-f62e3b57c0e4 h1:97Sfylvx7jxNwMDa6m3wRYQ0sCwP8R7ZGneF57jTSP0= | ||
github.com/savaki/amplitude-go v0.0.0-20160610055645-f62e3b57c0e4/go.mod h1:+dADDNKPvI1c6TuMfh+UakKF37l2i4ok/+wpEVT2KII= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20190514140710-3ec191127204 h1:4yG6GqBtw9C+UrLp6s2wtSniayy/Vd/3F7ffLE427XI= | ||
golang.org/x/net v0.0.0-20190514140710-3ec191127204/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@diez/analytics", | ||
"private": true, | ||
"version": "10.0.0-alpha.0", | ||
"description": "An analytics back end for the Diez CLI.", | ||
"author": "The Haiku Team <contact@haiku.ai>", | ||
"license": "Proprietary", | ||
"devDependencies": { | ||
"serverless-domain-manager": "^3.2.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/aws/aws-lambda-go/lambda" | ||
"github.com/savaki/amplitude-go" | ||
|
||
"encoding/json" | ||
"os" | ||
) | ||
|
||
type response events.APIGatewayProxyResponse | ||
|
||
type ping struct { | ||
UUID string `json:"uuid"` | ||
EventType string `json:"eventType"` | ||
Properties map[string]interface{} `json:"properties"` | ||
} | ||
|
||
func handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | ||
event := new(ping) | ||
err := json.Unmarshal([]byte(req.Body), event) | ||
if err != nil { | ||
return events.APIGatewayProxyResponse{ | ||
StatusCode: 400, | ||
}, err | ||
} | ||
|
||
client := amplitude.New(os.Getenv("AMPLITUDE_API_KEY")) | ||
client.Publish(amplitude.Event{ | ||
DeviceId: event.UUID, | ||
EventType: event.EventType, | ||
EventProperties: event.Properties, | ||
}) | ||
client.Flush() | ||
client.Close() | ||
return events.APIGatewayProxyResponse{ | ||
StatusCode: 204, | ||
}, nil | ||
} | ||
|
||
func main() { | ||
lambda.Start(handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
service: analytics | ||
frameworkVersion: ">=1.28.0 <2.0.0" | ||
plugins: | ||
- serverless-domain-manager | ||
custom: | ||
domainNames: | ||
dev: analytics-dev.diez.org | ||
prod: analytics.diez.org | ||
amplitudeApiKeys: | ||
dev: ${env:DIEZ_AMPLITUDE_API_KEY_DEV} | ||
prod: ${env:DIEZ_AMPLITUDE_API_KEY_PROD} | ||
customDomain: | ||
hostedZoneId: ${env:DIEZ_HOSTED_ZONE_ID} | ||
domainName: ${self:custom.domainNames.${opt:stage}} | ||
certificateName: diez.org | ||
provider: | ||
name: aws | ||
runtime: go1.x | ||
stage: ${opt:stage, "dev"} | ||
region: us-east-1 | ||
environment: | ||
AMPLITUDE_API_KEY: ${self:custom.amplitudeApiKeys.${opt:stage}} | ||
package: | ||
exclude: | ||
- ./** | ||
include: | ||
- ./bin/** | ||
|
||
functions: | ||
ping: | ||
handler: bin/ping | ||
events: | ||
- http: | ||
path: ping | ||
method: post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"providers": { | ||
"commands": [ | ||
"./lib/commands/analytics" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {disableAnalytics, enableAnalytics} from '@diez/storage'; | ||
import {CliCommandProvider} from '../api'; | ||
import {info} from '../reporting'; | ||
|
||
const provider: CliCommandProvider = { | ||
name: 'analytics <on|off>', | ||
description: 'Turn Diez analytics on or off.', | ||
action: async (_, toggle: string) => { | ||
switch (toggle) { | ||
case 'on': | ||
await enableAnalytics(); | ||
info('Diez analytics are enabled. Learn more here: https://diez.org/analytics'); | ||
break; | ||
case 'off': | ||
await disableAnalytics(); | ||
info('Diez analytics are disabled. Learn more here: https://diez.org/analytics'); | ||
break; | ||
default: | ||
throw new Error(`Unknown state: "${toggle}". Please specify either "on" or "off".`); | ||
} | ||
}, | ||
}; | ||
|
||
export = provider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {assignMock} from '@diez/test-utils'; | ||
|
||
const mockEmitDiagnostics = jest.fn(); | ||
const mockEnableAnalytics = jest.fn(); | ||
const mockDisableAnalytics = jest.fn(); | ||
jest.doMock('@diez/storage', () => ({ | ||
Registry: { | ||
get () { | ||
return true; | ||
}, | ||
}, | ||
emitDiagnostics: mockEmitDiagnostics, | ||
enableAnalytics: mockEnableAnalytics, | ||
disableAnalytics: mockDisableAnalytics, | ||
})); | ||
|
||
import {run} from '../src/cli'; | ||
import {action as analyticsAction} from '../src/commands/analytics'; | ||
import {diezVersion} from '../src/utils'; | ||
|
||
beforeEach(() => { | ||
assignMock(process, 'exit'); | ||
}); | ||
|
||
jest.mock('package-json'); | ||
|
||
describe('cli.analytics', () => { | ||
test('analytics ping', async () => { | ||
await run(); | ||
expect(mockEmitDiagnostics).toHaveBeenCalledTimes(1); | ||
expect(mockEmitDiagnostics).toHaveBeenCalledWith('activity', diezVersion); | ||
}); | ||
|
||
test('analytics <on|off> command', async () => { | ||
await analyticsAction({}, 'on'); | ||
expect(mockEnableAnalytics).toHaveBeenCalledTimes(1); | ||
expect(mockDisableAnalytics).toHaveBeenCalledTimes(0); | ||
await analyticsAction({}, 'off'); | ||
expect(mockEnableAnalytics).toHaveBeenCalledTimes(1); | ||
expect(mockDisableAnalytics).toHaveBeenCalledTimes(1); | ||
await expect(analyticsAction({}, 'medium')).rejects.toThrow(); | ||
expect(mockEnableAnalytics).toHaveBeenCalledTimes(1); | ||
expect(mockDisableAnalytics).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.