diff --git a/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee b/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee index 1f2466674c..b96084c288 100644 --- a/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee +++ b/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee @@ -4,7 +4,8 @@ Errors = require "../Errors/Errors" module.exports = AnalyticsController = recordEvent: (req, res, next) -> - AnalyticsManager.recordEvent req.session?.user?._id, req.params.event, req.body, (error) -> + user_id = AuthenticationController.getLoggedInUserId(req) or req.sessionID + AnalyticsManager.recordEvent user_id, req.params.event, req.body, (error) -> if error? if error instanceof Errors.ServiceNotConfiguredError # ignore, no-op diff --git a/services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee b/services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee index 66d2818032..12e5b68c84 100644 --- a/services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee +++ b/services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee @@ -16,6 +16,15 @@ makeRequest = (opts, callback)-> module.exports = + identifyUser: (user_id, old_user_id, callback = (error)->)-> + opts = + body: + old_user_id:old_user_id + json:true + method:"POST" + timeout:1000 + url: "/user/#{user_id}/identify" + makeRequest opts, callback recordEvent: (user_id, event, segmentation = {}, callback = (error) ->) -> if user_id+"" == settings.smokeTest?.userId+"" diff --git a/services/web/app/coffee/Features/Authentication/AuthenticationController.coffee b/services/web/app/coffee/Features/Authentication/AuthenticationController.coffee index 485b046a85..3c43323887 100644 --- a/services/web/app/coffee/Features/Authentication/AuthenticationController.coffee +++ b/services/web/app/coffee/Features/Authentication/AuthenticationController.coffee @@ -87,6 +87,7 @@ module.exports = AuthenticationController = LoginRateLimiter.recordSuccessfulLogin(email) AuthenticationController._recordSuccessfulLogin(user._id) Analytics.recordEvent(user._id, "user-logged-in", {ip:req.ip}) + Analytics.identifyUser(user._id, req.sessionID) logger.log email: email, user_id: user._id.toString(), "successful log in" req.session.justLoggedIn = true # capture the request ip for use when creating the session diff --git a/services/web/app/coffee/Features/StaticPages/StaticPagesRouter.coffee b/services/web/app/coffee/Features/StaticPages/StaticPagesRouter.coffee index f543d20c2e..07923d48d0 100644 --- a/services/web/app/coffee/Features/StaticPages/StaticPagesRouter.coffee +++ b/services/web/app/coffee/Features/StaticPages/StaticPagesRouter.coffee @@ -9,6 +9,7 @@ module.exports = webRouter.get '/tos', HomeController.externalPage("tos", "Terms of Service") webRouter.get '/about', HomeController.externalPage("about", "About Us") + webRouter.get '/security', HomeController.externalPage("security", "Security") webRouter.get '/privacy_policy', HomeController.externalPage("privacy", "Privacy Policy") webRouter.get '/planned_maintenance', HomeController.externalPage("planned_maintenance", "Planned Maintenance") @@ -20,4 +21,4 @@ module.exports = webRouter.get '/dropbox', HomeController.externalPage("dropbox", "Dropbox and ShareLaTeX") webRouter.get '/university', UniversityController.getIndexPage - webRouter.get '/university/*', UniversityController.getPage \ No newline at end of file + webRouter.get '/university/*', UniversityController.getPage diff --git a/services/web/test/UnitTests/coffee/Analytics/AnalyticsControllerTests.coffee b/services/web/test/UnitTests/coffee/Analytics/AnalyticsControllerTests.coffee new file mode 100644 index 0000000000..2c52b8b5f0 --- /dev/null +++ b/services/web/test/UnitTests/coffee/Analytics/AnalyticsControllerTests.coffee @@ -0,0 +1,44 @@ +should = require('chai').should() +SandboxedModule = require('sandboxed-module') +assert = require('assert') +path = require('path') +modulePath = path.join __dirname, '../../../../app/js/Features/Analytics/AnalyticsController' +sinon = require("sinon") +expect = require("chai").expect + + +describe 'AnalyticsController', -> + + beforeEach -> + @AuthenticationController = + getLoggedInUserId: sinon.stub() + + @AnalyticsManager = + recordEvent: sinon.stub().callsArgWith(3) + + @req = + params: + event:"i_did_something" + body:"stuff" + sessionID: "sessionIDHere" + + @res = + send:-> + @controller = SandboxedModule.require modulePath, requires: + "./AnalyticsManager":@AnalyticsManager + "../Authentication/AuthenticationController":@AuthenticationController + "logger-sharelatex": + log:-> + + describe "recordEvent", -> + + it "should use the user_id", (done)-> + @AuthenticationController.getLoggedInUserId.returns("1234") + @controller.recordEvent @req, @res + @AnalyticsManager.recordEvent.calledWith("1234", @req.params["event"], @req.body).should.equal true + done() + + it "should use the session id", (done)-> + @controller.recordEvent @req, @res + @AnalyticsManager.recordEvent.calledWith(@req.sessionID, @req.params["event"], @req.body).should.equal true + done() diff --git a/services/web/test/UnitTests/coffee/Authentication/AuthenticationControllerTests.coffee b/services/web/test/UnitTests/coffee/Authentication/AuthenticationControllerTests.coffee index 94e930c7b1..f44968a0e5 100644 --- a/services/web/test/UnitTests/coffee/Authentication/AuthenticationControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Authentication/AuthenticationControllerTests.coffee @@ -254,6 +254,8 @@ describe "AuthenticationController", -> @cb = sinon.stub() @LoginRateLimiter.processLoginRequest.callsArgWith(1, null, true) @AuthenticationManager.authenticate = sinon.stub().callsArgWith(2, null, @user) + @req.sessionID = Math.random() + @AnalyticsManager.identifyUser = sinon.stub() @AuthenticationController.doPassportLogin(@req, @req.body.email, @req.body.password, @cb) it "should attempt to authorise the user", -> @@ -261,6 +263,9 @@ describe "AuthenticationController", -> .calledWith(email: @email.toLowerCase(), @password) .should.equal true + it "should call identifyUser", -> + @AnalyticsManager.identifyUser.calledWith(@user._id, @req.sessionID).should.equal true + it "should setup the user data in the background", -> @UserHandler.setupLoginData.calledWith(@user).should.equal true