Make some analytics call conditional#9061
Conversation
| this.sendRequest_(request, trigger); | ||
| return request; | ||
| }); | ||
| return this.checkTriggerEnabled_(trigger, event) |
There was a problem hiding this comment.
would it make more sense to check for enabled before we do the url expansion. That way we prevent some work that might not be used.
| } | ||
|
|
||
| /** | ||
| * Checks if request for a trigger is enabled. |
There was a problem hiding this comment.
s/sampling/if the trigger is enabled
| } | ||
|
|
||
| /** | ||
| * Checks result of 'enabled' spec evaluation. Returns false if spec is provided and value |
There was a problem hiding this comment.
it returns false for all falsey values. right? (eg: 0, null and so on)
There was a problem hiding this comment.
Method itself always returns Promise, so it's real true/false all the time.
In current implementation false is returned only if the result of a spec expansion is an empty string (assuming that url expansion always returns string, the only falsey value is '').
Should I change this and treat all falsey-looking strings ('0', 'false', 'undefined', 'null, 'NaN'') as false?
There was a problem hiding this comment.
I think we should go ahead and change this, yes. The only more desirable alternative is to change var/url-replacements expansion with a new mode that will take false-y values and instead of outputting them via String(x) would instead output them as an empty string. However, that'd be a lot of changes for a relatively small feature. So, in the meantime, using falsy strings is a good alternative, I believe.
| }; | ||
| } | ||
|
|
||
| it('allows a request through', () => { |
There was a problem hiding this comment.
allows a request through for undefined `enabled` property
| const analytics = getAnalyticsTag(config); | ||
|
|
||
| const urlReplacements = urlReplacementsForDoc(analytics.element); | ||
| sandbox.stub(urlReplacements.getVariableSource(), 'get').returns(0); |
There was a problem hiding this comment.
this is interesting. Does the 0 get converted to a string? how does the tigger get enabled for this case?
There was a problem hiding this comment.
The result of expandUrlAsync is always a string because it gets encoded every time, that's the reason 0 (that gets translated to '0') is treated as true
There was a problem hiding this comment.
Knowing the fact that URL replacements does conversion to strings, we still can fairly easily standardize on what strings lead to false, including 0 and false.
| }); | ||
| }); | ||
| }); | ||
|
|
There was a problem hiding this comment.
Can you add a test or two for the case where both tag level and trigger level properties are specified?
|
ptal |
| if (!enabled) { | ||
| return; | ||
| } | ||
| return this.expandExtraUrlParams_(trigger, event) |
There was a problem hiding this comment.
Could you move this into a separate method, e.g. expandAndSendRequest_?
| } | ||
|
|
||
| /** | ||
| * Checks result of 'enabled' spec evaluation. Returns false if spec is provided and value |
There was a problem hiding this comment.
I think we should go ahead and change this, yes. The only more desirable alternative is to change var/url-replacements expansion with a new mode that will take false-y values and instead of outputting them via String(x) would instead output them as an empty string. However, that'd be a lot of changes for a relatively small feature. So, in the meantime, using falsy strings is a good alternative, I believe.
|
@zikas Thanks a lot for the PR! This looks very good. I just added a couple of comments, most importantly around the point you've made on falsy strings. |
zikas
left a comment
There was a problem hiding this comment.
@dvoytenko No problem, all done. Thank you for the review!
| }); | ||
| }); | ||
| }); | ||
|
|
avimehta
left a comment
There was a problem hiding this comment.
just a few small changes. Mostly lgtm from my side.
| * @param {string} request The request to process. | ||
| * @param {!JSONType} trigger JSON config block that resulted in this event. | ||
| * @param {!Object} event Object with details about the event. | ||
| * @return {!Promise<string|undefined>} The request that was sent out. |
There was a problem hiding this comment.
I believe type is always !Promise<string> here. no?
| InstrumentationService, | ||
| instrumentationServicePromiseForDoc, | ||
| AnalyticsEventType, | ||
| InstrumentationService, |
There was a problem hiding this comment.
nit: were the indentation changes automatically generated?
There was a problem hiding this comment.
Yeah, I guess this is my ide. Lint accepts either way. I rolled back unrelated indentation changes.
| const expansionOptions = this.expansionOptions_({}, trigger); | ||
| return this.expandTemplateWithUrlParams_(sampleOn, expansionOptions) | ||
| .then(key => this.cryptoService_.uniform(key)) | ||
| .then(digest => digest * 100 < spec['threshold']); |
There was a problem hiding this comment.
Could you please change here spec['threshold'] to threshold from above const threshold?
dvoytenko
left a comment
There was a problem hiding this comment.
One small request and LGTM. Thanks!
|
Thanks again! Looks like everything is green. Merging... |
Added the ability to fire analytics triggers conditional based on
enabledproperty that can be specified in individual triggers and at the root level.FIXES #4752