itly-plugin-amplitude 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d14dad1deaee5525bf9d21dab3f2f661682b4d11f448859511756a2df96962b1
4
+ data.tar.gz: f8eb63bf513f4b5c9ab24973ff3d5ebea47a10b1dfef5c717152cfbac608d2a2
5
+ SHA512:
6
+ metadata.gz: 356c30953cc309af7d7ccdbaf43674a67632c2a4aab262ce91e875bf228903520535da0e6fc0c72cdcc710de3c4b98fe0b004fd46e4111ddd884dcaa3d2bf59d
7
+ data.tar.gz: 5e9e0f3682c07c384b7bc6422e263765d90ab0185263336a61ac84dddcc85883118df3c1f8ebd747d5a912241201e58360d81d88746887cade2c678900c8fd3a
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in itly-plugin-amplitude.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ if ENV['LOCAL_ITLY_GEM']
11
+ # TODO: before publication to RubyGems, switch to version 1
12
+ gem 'itly-sdk', '~> 0.1', path: '../sdk'
13
+ end
14
+
15
+ gem 'rbs', '~> 1.0'
16
+ gem 'rspec'
17
+ gem 'steep', '~> 0.41'
data/Steepfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ target :lib do
4
+ signature 'sig'
5
+
6
+ check 'lib'
7
+
8
+ library 'logger', 'set', 'itly-sdk'
9
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'itly/plugin-amplitude'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/rspec ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Configure and load RBS
5
+ unless ENV['DISABLE_TYPE_CHECKING']
6
+ ENV['RBS_TEST_TARGET'] = 'Itly::*'
7
+ ENV['RBS_TEST_LOGLEVEL'] = 'warn'
8
+ ENV['RBS_TEST_DOUBLE_SUITE'] = 'rspec'
9
+ ENV['RBS_TEST_OPT'] = '-I./sig -I../sdk/sig'
10
+
11
+ require 'rbs/test/setup'
12
+ end
13
+
14
+ # Start RSpec
15
+ require 'rspec/core'
16
+
17
+ ENV['RSPEC_RUN_FROM_SCRIPT'] = 'true'
18
+ RSpec::Core::Runner.invoke
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/itly/plugin/amplitude/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'itly-plugin-amplitude'
7
+ spec.version = Itly::Plugin::Amplitude::VERSION
8
+ spec.authors = ['Iteratively', 'Benjamin Bouchet', 'Justin Fiedler', 'Andrey Sokolov']
9
+ spec.email = ['support@iterative.ly']
10
+
11
+ spec.summary = 'Amplitude plugin for Iteratively SDK for Ruby'
12
+ spec.description = 'Track and validate analytics with a unified, extensible interface ' \
13
+ 'that works with all your 3rd party analytics providers.'
14
+ spec.homepage = 'https://github.com/iterativelyhq/itly-sdk-ruby'
15
+ spec.license = 'MIT'
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
17
+
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
19
+
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = 'https://github.com/iterativelyhq/itly-sdk-ruby/plugin-amplitude'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'amplitude-api', '~> 0.3'
31
+ spec.add_dependency 'itly-sdk', '~> 0.1'
32
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ if ENV['LOCAL_ITLY_GEM']
4
+ lib = File.expand_path('../../../sdk/lib', File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ end
7
+
8
+ require_relative 'plugin/amplitude/amplitude'
9
+ require_relative 'plugin/amplitude/options'
10
+ require_relative 'plugin/amplitude/call_options'
11
+ require_relative 'plugin/amplitude/version'
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'itly-sdk'
4
+ require 'amplitude-api'
5
+
6
+ class Itly
7
+ class Plugin
8
+ ##
9
+ # Amplitude plugin class for Itly SDK
10
+ #
11
+ class Amplitude < Plugin
12
+ attr_reader :logger, :disabled
13
+
14
+ ##
15
+ # Instantiate a new Plugin::Amplitude
16
+ #
17
+ # @param [String] api_key: specify the Amplitude api key
18
+ # @param [TrueClass/FalseClass] disabled: set to true to disable the plugin. Default to false
19
+ #
20
+ def initialize(api_key:, disabled: false)
21
+ super()
22
+ @disabled = disabled
23
+
24
+ ::AmplitudeAPI.config.api_key = api_key
25
+ end
26
+
27
+ ##
28
+ # Initialize AmplitudeApi client
29
+ #
30
+ # @param [Itly::PluginOptions] options: plugins options
31
+ #
32
+ def load(options:)
33
+ super
34
+ # Get options
35
+ @logger = options.logger
36
+
37
+ # Log
38
+ logger&.info "#{id}: load()"
39
+
40
+ logger&.info "#{id}: plugin is disabled!" if @disabled
41
+ end
42
+
43
+ ##
44
+ # Identify a user
45
+ #
46
+ # Raise an error if the response is not 200
47
+ #
48
+ # @param [String] user_id: the id of the user in your application
49
+ # @param [Hash] properties: the properties containing user's traits to pass to your application
50
+ # @param [Itly::Plugin::Amplitude::IdentifyOptions] options: the plugin specific options
51
+ #
52
+ def identify(user_id:, properties: nil, options: nil)
53
+ super
54
+ return unless enabled?
55
+
56
+ # Log
57
+ log = Itly::Loggers.vars_to_log user_id: user_id, properties: properties, options: options
58
+ logger&.info "#{id}: identify(#{log})"
59
+
60
+ # Send through the client
61
+ payload = {}
62
+ payload.merge! options.to_hash if options
63
+ payload.merge! properties if properties
64
+
65
+ call_end_point(options&.callback) do
66
+ ::AmplitudeAPI.send_identify user_id, nil, payload
67
+ end
68
+ end
69
+
70
+ ##
71
+ # Track an event
72
+ #
73
+ # Raise an error if the response is not 200
74
+ #
75
+ # @param [String] user_id: the id of the user in your application
76
+ # @param [Event] event: the Event object to pass to your application
77
+ # @param [Itly::Plugin::Amplitude::TrackOptions] options: the plugin specific options
78
+ #
79
+ def track(user_id:, event:, options: nil)
80
+ super
81
+ return unless enabled?
82
+
83
+ # Log
84
+ log = Itly::Loggers.vars_to_log(
85
+ user_id: user_id, event: event&.name, properties: event&.properties, options: options
86
+ )
87
+ logger&.info "#{id}: track(#{log})"
88
+
89
+ # Send through the client
90
+ call_end_point(options&.callback) do
91
+ ::AmplitudeAPI.track ::AmplitudeAPI::Event.new(
92
+ user_id: user_id,
93
+ event_type: event.name,
94
+ event_properties: event.properties,
95
+ **(options&.to_hash || {})
96
+ )
97
+ end
98
+ end
99
+
100
+ ##
101
+ # Get the plugin ID
102
+ #
103
+ # @return [String] plugin id
104
+ #
105
+ def id
106
+ 'amplitude'
107
+ end
108
+
109
+ private
110
+
111
+ def enabled?
112
+ !@disabled
113
+ end
114
+
115
+ def call_end_point(callback)
116
+ raise 'You need to give a block' unless block_given?
117
+
118
+ # Call remote endpoint (Note: the AmplitudeAPI is using Faraday)
119
+ response = yield
120
+
121
+ # yield to the callback passed in to options
122
+ callback&.call(response.status, response.body)
123
+
124
+ # Return in case of success
125
+ return if response.status >= 200 && response.status < 300
126
+
127
+ # Raise in case of error
128
+ message = "The remote end-point returned an error. Response status: #{response.status}. "\
129
+ "Raw body: #{response.body}"
130
+ raise Itly::RemoteError, message
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Itly
4
+ class Plugin
5
+ # Amplitude plugin class for Itly SDK
6
+ class Amplitude
7
+ ##
8
+ # Amplitude specific plugin options class
9
+ #
10
+ class CallOptions < Itly::PluginCallOptions
11
+ PROPS = %w[device_id time groups app_version platform os_name os_version device_brand device_manufacturer
12
+ device_model carrier country region city dma language price quantity revenue productId revenueType
13
+ location_lat location_lng ip idfa idfv adid android_id event_id session_id insert_id].freeze
14
+
15
+ attr_reader :callback, *PROPS
16
+
17
+ class_eval(
18
+ <<-EVAL, __FILE__, __LINE__ + 1
19
+ def initialize(callback: nil, #{PROPS.collect { |p| "#{p}: nil" }.join ', '}) # def initialize(callback: nil, device_id: nil, ...)
20
+ super() # super()
21
+ @callback = callback # @callback = callback
22
+ #{PROPS.collect { |p| "@#{p} = #{p}" }.join "\n"} # @device_id = device_id
23
+ end # end
24
+ EVAL
25
+ )
26
+
27
+ ##
28
+ # Return all properties to be passed to the client
29
+ # While excluding the `callback` property
30
+ #
31
+ # @return [Hash] properties
32
+ #
33
+ def to_hash
34
+ PROPS.each_with_object({}) { |prop, hash| hash[prop.to_sym] = send(prop) unless send(prop).nil? }
35
+ end
36
+
37
+ ##
38
+ # Get the plugin description, for logs
39
+ #
40
+ # @return [String] description
41
+ #
42
+ def to_s
43
+ class_name = self.class.name.split('::').last
44
+ props = PROPS.collect { |prop| " #{prop}: #{send prop}" unless send(prop).nil? }.compact
45
+ "#<Amplitude::#{class_name} callback: #{callback.nil? ? 'nil' : 'provided'}#{props.join}>"
46
+ end
47
+ end
48
+
49
+ ##
50
+ # Amplitude specific plugin options class for calls to plugin methods
51
+ #
52
+ %w[Identify Group Page Track Alias].each do |name|
53
+ class_eval(
54
+ <<-EVAL, __FILE__, __LINE__ + 1
55
+ class #{name}Options < CallOptions # class IdentifyOptions < CallOptions
56
+ end # end
57
+ EVAL
58
+ )
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'itly-sdk'
4
+
5
+ class Itly
6
+ class Plugin
7
+ class Amplitude
8
+ ##
9
+ # Options for the Amplitude plugin class
10
+ #
11
+ # rubocop:disable Lint/EmptyClass
12
+ class Options
13
+ end
14
+ # rubocop:enable Lint/EmptyClass
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Itly
4
+ class Plugin
5
+ class Amplitude < Plugin
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
data/sig/amplitude.rbs ADDED
@@ -0,0 +1,21 @@
1
+ class Itly
2
+ class Plugin
3
+ class Amplitude < Plugin
4
+ VERSION: String
5
+
6
+ attr_reader logger: Logger?
7
+ attr_reader disabled: bool
8
+
9
+ def load: (options: Itly::PluginOptions options) -> void
10
+ def identify: (user_id: String user_id, ?properties: propertiesHash?, ?options: Itly::Plugin::Amplitude::IdentifyOptions? options) -> void
11
+ def track: (user_id: String user_id, event: Itly::Event event, ?options: Itly::Plugin::Amplitude::TrackOptions? options) -> void
12
+ def id: () -> String
13
+
14
+ private
15
+
16
+ def initialize: (api_key: String api_key, ?disabled: bool disabled) -> void
17
+ def enabled?: () -> bool
18
+ def call_end_point: ((^(Integer?, String?) -> void)? callback) ?{ () -> Faraday::Response } -> void
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ class Itly
2
+ class Plugin
3
+ class Amplitude
4
+ class CallOptions < Itly::PluginCallOptions
5
+ attr_reader callback: (^(Integer?, String?) -> void)?
6
+
7
+ def to_hash: () -> Hash[Symbol, String | Integer | Float | untypedHash]
8
+ def to_s: () -> String
9
+
10
+ private
11
+
12
+ def initialize: (?callback: (^(Integer?, String?) -> void)? callback, ?device_id: String? device_id, ?time: Integer? time, ?groups: untypedHash? groups, ?app_version: String? app_version, ?platform: String? platform, ?os_name: String? os_name, ?os_version: String? os_version, ?device_brand: String? device_brand, ?device_manufacturer: String? device_manufacturer, ?device_model: String? device_model, ?carrier: String? carrier, ?country: String? country, ?region: String? region, ?city: String? city, ?dma: String? dma, ?language: String? language, ?price: Float? price, ?quantity: Integer? quantity, ?revenue: Float? revenue, ?productId: String? productId, ?revenueType: String? revenueType, ?location_lat: Float? location_lat, ?location_lng: Float? location_lng, ?ip: String? ip, ?idfa: String? idfa, ?idfv: String? idfv, ?adid: String? adid, ?android_id: String? android_id, ?event_id: Integer? event_id, ?session_id: Integer? session_id, ?insert_id: String? insert_id) -> void
13
+ end
14
+
15
+ class IdentifyOptions < CallOptions
16
+ end
17
+
18
+ class GroupOptions < CallOptions
19
+ end
20
+
21
+ class PageOptions < CallOptions
22
+ end
23
+
24
+ class TrackOptions < CallOptions
25
+ end
26
+
27
+ class AliasOptions < CallOptions
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ # Part of the gems
2
+ class Faraday
3
+ class Response
4
+ attr_reader status: Integer?
5
+ attr_reader body: String
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ # Part of the gems
2
+ class MonitorMixin
3
+ end
data/sig/options.rbs ADDED
@@ -0,0 +1,8 @@
1
+ class Itly
2
+ class Plugin
3
+ class Amplitude
4
+ class Options
5
+ end
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: itly-plugin-amplitude
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Iteratively
8
+ - Benjamin Bouchet
9
+ - Justin Fiedler
10
+ - Andrey Sokolov
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2021-06-15 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: amplitude-api
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '0.3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: itly-sdk
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: '0.1'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '0.1'
44
+ description: Track and validate analytics with a unified, extensible interface that
45
+ works with all your 3rd party analytics providers.
46
+ email:
47
+ - support@iterative.ly
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - ".rspec"
53
+ - Gemfile
54
+ - Steepfile
55
+ - bin/console
56
+ - bin/rspec
57
+ - bin/setup
58
+ - itly-plugin-amplitude.gemspec
59
+ - lib/itly/plugin-amplitude.rb
60
+ - lib/itly/plugin/amplitude/amplitude.rb
61
+ - lib/itly/plugin/amplitude/call_options.rb
62
+ - lib/itly/plugin/amplitude/options.rb
63
+ - lib/itly/plugin/amplitude/version.rb
64
+ - sig/amplitude.rbs
65
+ - sig/call_options.rbs
66
+ - sig/lib/faraday_response.rbs
67
+ - sig/lib/monitor_mixin.rbs
68
+ - sig/options.rbs
69
+ homepage: https://github.com/iterativelyhq/itly-sdk-ruby
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ allowed_push_host: https://rubygems.org/
74
+ homepage_uri: https://github.com/iterativelyhq/itly-sdk-ruby
75
+ source_code_uri: https://github.com/iterativelyhq/itly-sdk-ruby/plugin-amplitude
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 2.6.0
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.0.3.1
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Amplitude plugin for Iteratively SDK for Ruby
95
+ test_files: []