Added segment events for new maps / datasets

pull/5936/head
Javier Martin 9 years ago
parent 4f0f6087ad
commit fdbad96a17

@ -106,6 +106,9 @@ group :development, :test do
gem 'thin', require: false
end
# segment metrics
gem 'analytics-ruby', '~> 2.0.0', :require => 'segment/analytics'
# Load optional engines
# TODO activate when CartoDB plugins are finally included
# Dir['engines' + '/*/*.gemspec'].each do |gemspec_file|

@ -52,6 +52,7 @@ GEM
multi_json (~> 1.0)
addressable (2.3.2)
aequitas (0.0.2)
analytics-ruby (2.0.13)
arel (3.0.3)
autoparse (0.3.3)
addressable (>= 2.3.1)
@ -337,6 +338,7 @@ DEPENDENCIES
activerecord-postgresql-adapter
addressable (= 2.3.2)
aequitas (= 0.0.2)
analytics-ruby (~> 2.0.0)
aws-sdk (= 1.8.5)
bartt-ssl_requirement (~> 1.4.0)
capybara (= 1.1.2)
@ -397,3 +399,6 @@ DEPENDENCIES
virtus (= 1.0.0.beta3)
vizzuality-sequel-rails (= 0.3.7)!
webrick (= 1.3.1)
BUNDLED WITH
1.10.6

@ -1,4 +1,6 @@
# coding: UTF-8
#require 'cartodb/event_tracker'
require_relative '../../../models/visualization/presenter'
require_relative '../../../helpers/bounding_box_helper'
require_relative '../../../../services/named-maps-api-wrapper/lib/named-maps-wrapper/exceptions'
@ -37,6 +39,9 @@ class Api::Json::TablesController < Api::ApplicationController
@table.valid? && @table.save
end
custom_properties = {'privacy' => @table.table_visualization.privacy, 'type' => @table.table_visualization.type, 'vis_id' => @table.table_visualization.id, 'origin' => 'blank'}
Cartodb::EventTracker.new.send_event(current_user, 'Created dataset', custom_properties)
if save_status
render_jsonp(@table.public_values({request:request}), 200, { location: "/tables/#{@table.id}" })
else
@ -122,9 +127,15 @@ class Api::Json::TablesController < Api::ApplicationController
@stats_aggregator.timing('ownership-check') do
return head(403) unless @table.table_visualization.is_owner?(current_user)
end
custom_properties = {'privacy' => @table.table_visualization.privacy, 'type' => @table.table_visualization.type, 'vis_id' => @table.table_visualization.id}
@stats_aggregator.timing('delete') do
@table.destroy
end
Cartodb::EventTracker.new.send_event(current_user, 'Deleted dataset', custom_properties)
head :no_content
rescue CartoDB::NamedMapsWrapper::HTTPResponseError => exception
CartoDB::Logger.info "Communication error with tiler API. HTTP Code: #{exception.message}", exception.template_data

@ -1,5 +1,6 @@
# encoding: utf-8
require 'json'
require 'cartodb/event_tracker'
require_relative '../../../models/visualization/member'
require_relative '../../../models/visualization/collection'
require_relative '../../../models/visualization/presenter'
@ -17,7 +18,7 @@ class Api::Json::VisualizationsController < Api::ApplicationController
ssl_allowed :notify_watching, :list_watching, :add_like, :remove_like
ssl_required :create, :update, :destroy, :set_next_id unless Rails.env.development? || Rails.env.test?
skip_before_filter :api_authorization_required, only: [:add_like, :remove_like]
before_filter :optional_api_authorization, only: [:add_like, :remove_like]
before_filter :table_and_schema_from_params, only: [:update, :destroy, :stats,
:notify_watching, :list_watching,
@ -56,6 +57,9 @@ class Api::Json::VisualizationsController < Api::ApplicationController
vis = set_visualization_prev_next(vis, prev_id, next_id)
custom_properties = {'privacy' => vis.privacy, 'type' => vis.type, 'vis_id' => vis.id}
Cartodb::EventTracker.new.send_event(current_user, 'Created map', custom_properties)
render_jsonp(vis)
rescue CartoDB::InvalidMember
render_jsonp({ errors: vis.full_errors }, 400)
@ -67,7 +71,6 @@ class Api::Json::VisualizationsController < Api::ApplicationController
rescue CartoDB::NamedMapsWrapper::NamedMapsDataError => exception
render_jsonp({ errors: { named_maps: exception } }, 400)
end
end
end
@ -144,10 +147,15 @@ class Api::Json::VisualizationsController < Api::ApplicationController
return(head 404) unless vis
return(head 403) unless vis.is_owner?(current_user)
custom_properties = {'privacy' => vis.privacy, 'type' => vis.type, 'vis_id' => vis.id}
event_type = vis.type == Visualization::Member::TYPE_DERIVED ? 'map' : 'dataset'
@stats_aggregator.timing('delete') do
vis.delete
end
Cartodb::EventTracker.new.send_event(current_user, "Deleted #{event_type}", custom_properties)
return head 204
rescue KeyError
head(404)

@ -2,6 +2,7 @@
require 'sequel'
require 'fileutils'
require 'uuidtools'
require 'cartodb/event_tracker'
require_relative './user'
require_relative './table'
require_relative './log'
@ -271,6 +272,7 @@ class DataImport < Sequel::Model
log.append "Import finished\n"
log.store
save
begin
CartoDB::PlatformLimits::Importer::UserConcurrentImportsAmount.new({
user: current_user,
@ -284,6 +286,22 @@ class DataImport < Sequel::Model
"#{exception.message} #{exception.backtrace.inspect}")
end
notify(results)
track_new_datasets(results)
#results.each do |result|
# if result.success?
# if result.name != nil
# map_id = UserTable.where(data_import_id: self.id, name: result.name).first.map_id
# else
# map_id = UserTable.where(data_import_id: self.id).first.map_id
# end
# vis = Carto::Visualization.where(map_id: map_id).first
#
# custom_properties = {'privacy' => vis.privacy, 'type' => vis.type, 'vis_id' => vis.id, 'origin' => 'import'}
# Cartodb::EventTracker.new.send_event(current_user, 'Created dataset', custom_properties)
# end
#end
self
end
@ -879,4 +897,22 @@ class DataImport < Sequel::Model
end
end
def track_new_datasets(results)
results.each do |result|
if result.success?
if result.name != nil
map_id = UserTable.where(data_import_id: self.id, name: result.name).first.map_id
origin = self.from_common_data? ? 'common-data' : 'import'
else
map_id = UserTable.where(data_import_id: self.id).first.map_id
origin = 'copy'
end
vis = Carto::Visualization.where(map_id: map_id).first
custom_properties = {'privacy' => vis.privacy, 'type' => vis.type, 'vis_id' => vis.id, 'origin' => origin}
Cartodb::EventTracker.new.send_event(current_user, 'Created dataset', custom_properties)
end
end
end
end

@ -92,6 +92,8 @@ defaults: &defaults
wizard: ''
filter: ''
query: ''
segment:
api_key:
common_data:
protocol: 'https'
username: 'common-data'
@ -453,6 +455,8 @@ development:
retries: 5
timeout: 5
enforce_non_empty_layer_css: false
segment:
api_key:
test:
<<: *defaults
@ -464,9 +468,13 @@ test:
api_requests_es_service:
url: "http://api-calls-service.localhost.lan/search"
body: ""
segment:
api_key:
staging:
<<: *defaults
segment:
api_key:
production:
<<: *defaults

@ -0,0 +1,35 @@
module Cartodb
class EventTracker
def send_event(user, event_name, custom_properties = {})
return unless is_tracking_active?
# Some events register custom properties
# Monitary values associated with the event should use 'revenue' reserved key
properties = generate_event_properties(user).merge(custom_properties)
Resque.enqueue(Resque::EventDeliveryJobs::TrackEvent, user.id, event_name, properties)
end
def test(name)
Resque.enqueue(Resque::EventDeliveryJobs::Test, name)
end
private
def generate_event_properties(user)
{
'username' => user.username,
'email' => user.email,
'plan' => user.account_type,
'organization' => user.organization_user? ? user.organization.name: nil,
'event_origin' => 'Cartodb'
}
end
def is_tracking_active?
!Cartodb.config[:segment]['api_key'].nil?
end
end
end

@ -0,0 +1,41 @@
require 'segment/analytics'
module Cartodb
# Development info: https://segment.com/docs/libraries/ruby/quickstart
class SegmentTracker
def initialize
@api_key = Cartodb.config[:segment]['api_key']
@analytics = Segment::Analytics.new({
write_key: @api_key
})
end
def enabled?
!@api_key.nil?
end
def track_event(user_id, event, properties)
return unless enabled?
begin
@analytics.track(
user_id: user_id,
event: event,
properties: properties
)
rescue Exception => e
Rollbar.report_message('Segment error tracking event', 'error', { user: user_id, event: event })
end
end
def flush
begin
@analytics.flush
rescue Exception => e
Rollbar.report_message('Segment error flush', 'error')
end
end
end
end

@ -0,0 +1,29 @@
require_relative '../cartodb/segment_tracker'
module Resque
module EventDeliveryJobs
module Test
@queue = :tracker
def self.perform(name)
File.open("/home/jmartin/#{name}.txt", 'w') do |f2|
f2.puts "yeaaaaaaah"
end
end
end
module TrackEvent
@queue = :tracker
def self.perform(user_id, event_name, properties)
tracker = Cartodb::SegmentTracker.new
tracker.track_event(user_id,
event_name,
properties)
tracker.flush
end
end
end
end

@ -1,2 +1,2 @@
#!/bin/sh
VVERBOSE=true QUEUE=imports,users,geocodings,synchronizations rake environment resque:work
VVERBOSE=true QUEUE=imports,users,geocodings,synchronizations,tracker rake environment resque:work

Loading…
Cancel
Save