diff --git a/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee b/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee index a57118723e..38029219ee 100644 --- a/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee +++ b/services/web/app/coffee/Features/Analytics/AnalyticsController.coffee @@ -1,15 +1,20 @@ AnalyticsManager = require "./AnalyticsManager" Errors = require "../Errors/Errors" AuthenticationController = require("../Authentication/AuthenticationController") +GeoIpLookup = require '../../infrastructure/GeoIpLookup' module.exports = AnalyticsController = updateEditingSession: (req, res, next) -> userId = AuthenticationController.getLoggedInUserId(req) projectId = req.params.projectId + countryCode = null if userId? - AnalyticsManager.updateEditingSession userId, projectId, {}, (error) -> - respondWith(error, res, next) + GeoIpLookup.getDetails req.ip, (err, geoDetails) -> + if geoDetails?.country_code? and geoDetails.country_code != "" + countryCode = geoDetails.country_code + AnalyticsManager.updateEditingSession userId, projectId, countryCode, (error) -> + respondWith(error, res, next) else res.send 204 diff --git a/services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee b/services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee index 3a3467b59e..469183d2fa 100644 --- a/services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee +++ b/services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee @@ -43,19 +43,19 @@ module.exports = opts.qs = {fromV2: 1} makeRequest opts, callback - updateEditingSession: (userId, projectId, segmentation = {}, callback = (error) ->) -> + updateEditingSession: (userId, projectId, countryCode, callback = (error) ->) -> if userId+"" == settings.smokeTest?.userId+"" return callback() + query = + userId: userId + projectId: projectId + if countryCode + query.countryCode = countryCode opts = - body: - segmentation: segmentation - json: true method: "PUT" timeout: 1000 url: "/editingSession" - qs: - userId: userId - projectId: projectId + qs: query maxAttempts: 20 retryDelay: 5000 if settings.overleaf? diff --git a/services/web/app/coffee/infrastructure/GeoIpLookup.coffee b/services/web/app/coffee/infrastructure/GeoIpLookup.coffee index 88f3ed4bfa..e2b36e94ab 100644 --- a/services/web/app/coffee/infrastructure/GeoIpLookup.coffee +++ b/services/web/app/coffee/infrastructure/GeoIpLookup.coffee @@ -49,4 +49,4 @@ module.exports = GeoIpLookup = countryCode = ipDetails?.country_code?.toUpperCase() currencyCode = currencyMappings[countryCode] || "USD" logger.log ip:ip, currencyCode:currencyCode, ipDetails:ipDetails, "got currencyCode for ip" - callback(err, currencyCode, countryCode) \ No newline at end of file + callback(err, currencyCode, countryCode) diff --git a/services/web/config/settings.defaults.coffee b/services/web/config/settings.defaults.coffee index 1a057d4be8..a28a1020d6 100644 --- a/services/web/config/settings.defaults.coffee +++ b/services/web/config/settings.defaults.coffee @@ -139,7 +139,7 @@ module.exports = settings = apiKey: "" subdomain: "" geoIpLookup: - url: "http://#{process.env['GEOIP_HOST'] or 'localhost'}:8080/json" + url: "http://#{process.env['GEOIP_HOST'] or 'localhost'}:8080/json/" realTime: url: "http://#{process.env['REALTIME_HOST'] or 'localhost'}:3026" contacts: diff --git a/services/web/test/unit/coffee/Analytics/AnalyticsControllerTests.coffee b/services/web/test/unit/coffee/Analytics/AnalyticsControllerTests.coffee index 8ac73c5af1..c3e3802f37 100644 --- a/services/web/test/unit/coffee/Analytics/AnalyticsControllerTests.coffee +++ b/services/web/test/unit/coffee/Analytics/AnalyticsControllerTests.coffee @@ -22,6 +22,8 @@ describe 'AnalyticsController', -> "../Authentication/AuthenticationController":@AuthenticationController "logger-sharelatex": log:-> + '../../infrastructure/GeoIpLookup': @GeoIpLookup = + getDetails: sinon.stub() @res = send:-> @@ -31,12 +33,18 @@ describe 'AnalyticsController', -> @req = params: projectId: "a project id" + @GeoIpLookup.getDetails = sinon.stub() + .callsArgWith(1, null, {country_code: 'XY'}) it "delegates to the AnalyticsManager", (done) -> @AuthenticationController.getLoggedInUserId.returns("1234") @controller.updateEditingSession @req, @res - @AnalyticsManager.updateEditingSession.calledWith("1234", "a project id", {}).should.equal true + @AnalyticsManager.updateEditingSession.calledWith( + "1234", + "a project id", + 'XY' + ).should.equal true done() describe "recordEvent", -> @@ -46,6 +54,7 @@ describe 'AnalyticsController', -> event:"i_did_something" body:"stuff" sessionID: "sessionIDHere" + session: {} it "should use the user_id", (done)-> @AuthenticationController.getLoggedInUserId.returns("1234")