1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# frozen_string_literal: true
require_relative './helpers'
module InternalEventsCli
class UsageViewer
include Helpers
IDENTIFIER_EXAMPLES = {
%w[namespace project user] => { "namespace" => "project.namespace" },
%w[namespace user] => { "namespace" => "group" }
}.freeze
attr_reader :cli, :event
def initialize(cli, event_path = nil, event = nil)
@cli = cli
@event = event
@selected_event_path = event_path
end
def run
prompt_for_eligible_event
prompt_for_usage_location
end
def prompt_for_eligible_event
return if event
event_details = events_by_filepath
@selected_event_path = cli.select(
"Show examples for which event?",
get_event_options(event_details),
**select_opts,
**filter_opts
)
@event = event_details[@selected_event_path]
end
def prompt_for_usage_location(default = 'ruby/rails')
choices = [
{ name: 'ruby/rails', value: :rails },
{ name: 'rspec', value: :rspec },
{ name: 'javascript (vue)', value: :vue },
{ name: 'javascript (plain)', value: :js },
{ name: 'vue template', value: :vue_template },
{ name: 'haml', value: :haml },
{ name: 'View examples for a different event', value: :other_event },
{ name: 'Exit', value: :exit }
]
usage_location = cli.select(
'Select a use-case to view examples for:',
choices,
**select_opts,
per_page: 10
) do |menu|
menu.enum '.'
menu.default default
end
case usage_location
when :rails
rails_examples
prompt_for_usage_location('ruby/rails')
when :rspec
rspec_examples
prompt_for_usage_location('rspec')
when :haml
haml_examples
prompt_for_usage_location('haml')
when :js
js_examples
prompt_for_usage_location('javascript (plain)')
when :vue
vue_examples
prompt_for_usage_location('javascript (vue)')
when :vue_template
vue_template_examples
prompt_for_usage_location('vue template')
when :other_event
self.class.new(cli).run
when :exit
cli.say(Text::FEEDBACK_NOTICE)
end
end
def rails_examples
args = Array(event['identifiers']).map do |identifier|
" #{identifier}: #{identifier_examples[identifier]}"
end
action = args.any? ? "\n '#{event['action']}',\n" : "'#{event['action']}'"
cli.say format_warning <<~TEXT
#{divider}
#{format_help('# RAILS')}
Gitlab::InternalEvents.track_event(#{action}#{args.join(",\n")}#{"\n" unless args.empty?})
#{divider}
TEXT
end
def rspec_examples
cli.say format_warning <<~TEXT
#{divider}
#{format_help('# RSPEC')}
it_behaves_like 'internal event tracking' do
let(:event) { '#{event['action']}' }
#{
Array(event['identifiers']).map do |identifier|
" let(:#{identifier}) { #{identifier_examples[identifier]} }\n"
end.join('')
}end
#{divider}
TEXT
end
def identifier_examples
event['identifiers']
.to_h { |identifier| [identifier, identifier] }
.merge(IDENTIFIER_EXAMPLES[event['identifiers'].sort] || {})
end
def haml_examples
cli.say <<~TEXT
#{divider}
#{format_help('# HAML -- ON-CLICK')}
.gl-display-inline-block{ #{format_warning("data: { event_tracking: '#{event['action']}' }")} }
= _('Important Text')
#{divider}
#{format_help('# HAML -- COMPONENT ON-CLICK')}
= render Pajamas::ButtonComponent.new(button_options: { #{format_warning("data: { event_tracking: '#{event['action']}' }")} })
#{divider}
#{format_help('# HAML -- COMPONENT ON-LOAD')}
= render Pajamas::ButtonComponent.new(button_options: { #{format_warning("data: { event_tracking_load: true, event_tracking: '#{event['action']}' }")} })
#{divider}
TEXT
cli.say("Want to see the implementation details? See app/assets/javascripts/tracking/internal_events.js\n\n")
end
def vue_template_examples
cli.say <<~TEXT
#{divider}
#{format_help('// VUE TEMPLATE -- ON-CLICK')}
<script>
import { GlButton } from '@gitlab/ui';
export default {
components: { GlButton }
};
</script>
<template>
<gl-button #{format_warning("data-event-tracking=\"#{event['action']}\"")}>
Click Me
</gl-button>
</template>
#{divider}
#{format_help('// VUE TEMPLATE -- ON-LOAD')}
<script>
import { GlButton } from '@gitlab/ui';
export default {
components: { GlButton }
};
</script>
<template>
<gl-button #{format_warning("data-event-tracking-load=\"#{event['action']}\"")}>
Click Me
</gl-button>
</template>
#{divider}
TEXT
cli.say("Want to see the implementation details? See app/assets/javascripts/tracking/internal_events.js\n\n")
end
def js_examples
cli.say <<~TEXT
#{divider}
#{format_help('// FRONTEND -- RAW JAVASCRIPT')}
#{format_warning("import { InternalEvents } from '~/tracking';")}
export const performAction = () => {
#{format_warning("InternalEvents.trackEvent('#{event['action']}');")}
return true;
};
#{divider}
TEXT
# https://docs.snowplow.io/docs/understanding-your-pipeline/schemas/
cli.say("Want to see the implementation details? See app/assets/javascripts/tracking/internal_events.js\n\n")
end
def vue_examples
cli.say <<~TEXT
#{divider}
#{format_help('// VUE')}
<script>
#{format_warning("import { InternalEvents } from '~/tracking';")}
import { GlButton } from '@gitlab/ui';
#{format_warning('const trackingMixin = InternalEvents.mixin();')}
export default {
#{format_warning('mixins: [trackingMixin]')},
components: { GlButton },
methods: {
performAction() {
#{format_warning("this.trackEvent('#{event['action']}');")}
},
},
};
</script>
<template>
<gl-button @click=performAction>Click Me</gl-button>
</template>
#{divider}
TEXT
cli.say("Want to see the implementation details? See app/assets/javascripts/tracking/internal_events.js\n\n")
end
end
end
|