Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Add specs and use node to send data
Browse files Browse the repository at this point in the history
  • Loading branch information
mcolyer committed Sep 6, 2013
1 parent 34f0e17 commit 1b65d0d
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
16 changes: 9 additions & 7 deletions lib/collector.coffee
Expand Up @@ -5,24 +5,26 @@ module.exports =
constructor: ->
@sessionId = new Date().getTime()

# Public: Logs out the data that will be sent.
log: ->
console.debug JSON.stringify(@getData())

# Private
getPath: ->
global.project.getPath()

# Public: Returns the name of the currently logged in user.
# Private
getUser: ->
process.env.USER

# Private
getSessionId: ->
@sessionId

# Public: Returns an object containing all data collected.
getData: (additionalData) ->
data =
windowPath: @getPath()
sessionId: @getSessionId()
window_path: @getPath()
session_id: @getSessionId()
actor_login: @getUser()
user_agent: 'Atom v25.0.0'
screen_resolution: '2560x1440'
pixel_ratio: 1
browser_resolution: '1360x923'
_.extend(data, additionalData)
10 changes: 5 additions & 5 deletions lib/metrics.coffee
Expand Up @@ -2,11 +2,11 @@ Collector = require './collector'
Reporter = require './reporter'

module.exports =
collector: new Collector()
reporter: new Reporter()

activate: (state) ->
@collector = new Collector()
# FIXME: you have to use 'recordPageView'
new Reporter('recordPageView', @collector.getUser(), @collector.getData())
@reporter.send(@collector.getData(action: 'activate'))

deactivate: ->
# FIXME: you have to use 'recordPageView'
new Reporter('recordPageView', @collector.getUser(), @collector.getData())
@reporter.send(@collector.getData(action: 'deactivate'))
18 changes: 10 additions & 8 deletions lib/reporter.coffee
@@ -1,13 +1,15 @@
require './octolytics'
request = require 'request'

module.exports =
class Reporter
constructor: (event, user, data) ->
window._octo.setApp('atom')
window._octo.setHost('collector-staging.githubapp.com')
constructor: ->
@request = request

window._octo.setActor
login: user
send: (data) ->
params = timestamp: new Date().getTime()
for key, value of data
params["dimensions[#{key}]"] = value

window._octo.setDimensions(data)
window._octo.push([event])
@request
url: "https://collector-staging.githubapp.com/atom/page_view"
qs: params
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -13,6 +13,9 @@
"engines": {
"atom": ">25.0"
},
"dependencies": {
"request": "2.27.0"
},
"publishConfig": {
"registry": "https://atom.iriscouch.com/registry/_design/app/_rewrite"
}
Expand Down
20 changes: 20 additions & 0 deletions spec/collector-spec.coffee
@@ -0,0 +1,20 @@
_ = require 'underscore'
Collector = require '../lib/collector'

fdescribe "Collector", ->
subject = null
beforeEach ->
subject = new Collector

describe "getData", ->
beforeEach ->

it "creates a request with the proper options", ->
keys = _.keys(subject.getData())
expect(keys).toContain 'user_agent'
expect(keys).toContain 'screen_resolution'
expect(keys).toContain 'pixel_ratio'
expect(keys).toContain 'browser_resolution'
expect(keys).toContain 'window_path'
expect(keys).toContain 'session_id'
expect(keys).toContain 'actor_login'
23 changes: 19 additions & 4 deletions spec/metrics-spec.coffee
@@ -1,5 +1,20 @@
Metrics = require '../lib/metrics'
metrics = require '../lib/metrics'

describe "Metrics", ->
it "has one valid test", ->
expect("life").toBe "easy"
fdescribe "Metrics", ->
describe "upon loading", ->
beforeEach ->
spyOn(metrics.reporter, 'send')
metrics.activate()

it "reports a page view", ->
expect(metrics.reporter.send).toHaveBeenCalled()
expect(metrics.reporter.send.calls[0].args[0]).toEqual metrics.collector.getData(action: 'activate')

describe "upon unloading", ->
beforeEach ->
spyOn(metrics.reporter, 'send')
metrics.deactivate()

it "reports a page view", ->
expect(metrics.reporter.send).toHaveBeenCalled()
expect(metrics.reporter.send.calls[0].args[0]).toEqual metrics.collector.getData(action: 'deactivate')
17 changes: 17 additions & 0 deletions spec/reporter-spec.coffee
@@ -0,0 +1,17 @@
Reporter = require '../lib/reporter'

fdescribe "Reporter", ->
subject = null
beforeEach ->
subject = new Reporter

describe "send", ->
beforeEach ->
spyOn(subject, 'request')
subject.send(key: 'value')

it "creates a request with the proper options", ->
expect(subject.request).toHaveBeenCalled()
expect(subject.request.calls[0].args[0].url).toBe 'https://collector-staging.githubapp.com/atom/page_view'
expect(subject.request.calls[0].args[0].qs['dimensions[key]']).toBe 'value'
expect(subject.request.calls[0].args[0].qs['dimensions[timestamp]']).not.toBeNull()

0 comments on commit 1b65d0d

Please sign in to comment.