Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tracks event: calypso_refer_visit_response #27888

Merged
merged 3 commits into from Oct 31, 2018

Conversation

jaswrks
Copy link
Contributor

@jaswrks jaswrks commented Oct 16, 2018

Changes proposed in this Pull Request

  • Listens for a response from the Refer affiliate platform and triggers a new Tracks event (calypso_refer_visit_response) to record each response for further analysis.

Testing instructions

Open this Calypso.live URL.

In your browser Console, enable debugging by entering:

localStorage.setItem( 'debug', 'calypso:analytics:*' );

Open this Calypso.live URL (or just reload the page).

Filter Console by refer and confirm the following log entries exist.

2018-10-16_11-24-22

You can also test using all possible affiliate tracking params:
https://hash-6a33e29e2244a758f9fa26e5b2317e991100a877.calypso.live/start?aff=3343&cid=697355&sid=foo&flags=analytics


Now load this Calypso.live URL containing an invalid affiliate ID.

Filter Console by refer and confirm the following log entries exist.

2018-10-16_11-25-01


Aside from the expected HTTP response failure (due to invalid affiliate ID), please confirm there were no JavaScript errors in your browser console while running any of these tests.

@matticbot
Copy link
Contributor

@jaswrks jaswrks added [Status] In Progress [Feature] Tracks Metrics & Monitoring Capturing analytics about user behavior on WordPress.com. [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] In Progress labels Oct 16, 2018
eventProps.success = responseBody.success || '';
eventProps.description = responseBody.message || 'error';

analytics.tracks.recordEvent( 'calypso_refer_visit_response', eventProps );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK we don't want to use these methods directly as here, but more like here. The latter is in a React component within a call to connect though, so I'm not sure how that would translate to this data layer module. 🤔 Just thinking out loud here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsnajdr or @dmsnell Can you help us answer this question? I want to record a Tracks event on HTTP success or failure. Have I done it properly here in the data-layer? Or is there a better way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both handlers (success and error) should return a Redux action to be dispatched as a reaction to the response. And we have a dedicated Redux analytics library:

import { recordTracksEvent } from 'state/analytics/actions';

function onTrackAffiliatePageLoadError( ... ) {
  /* ... */
  return recordTracksEvent( 'calypso_refer_visit_response', eventProps );
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Thank you so much!

@lsinger lsinger added [Status] Ready to Merge and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Oct 19, 2018
Copy link
Contributor

@lsinger lsinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Works as expected. Left a non-blocking code comment.


const response = error.response;
let responseBody = JSON.parse( response.text );
responseBody = 'object' === typeof responseBody ? responseBody : {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this parsing feels a bit hand-wavey. why is the response coming back as JSON data in the text value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. This API we're calling responds with JSON even on error, but its treated as a failure and the body is not parsed, instead being passed to onError where we get hand-wavey.

Instead of onSuccess and onError it would be nice to put http() into a mode whereby all responses are handled by an onComplete handler. The onComplete handler could look at the response status and response data in greater detail than is typically necessary -- just for cases like this.

) {
debug( 'Unexpected referral response: ', response );
return; // Not possible.
}
Copy link
Contributor

@dmsnell dmsnell Oct 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an appropriate place to use a fromApi parser. it will let us simplify the success handler and the data layer will switch to onError if the parse throws any exception.

const onSuccess = ( action, eventProps ) => recordEvent( 'calypso_refer_visit_response', eventProps );

const fromApi = ( { body: { data: { message, status, success, ...responseData } ) => ( {
	...pick( responseData, whitelistedEventProps ),
	status: status || '',
	success: success || '',
	descriptions: message || 'success',
} );

const onError = ( action, error ) => {
	if ( ! ( error && error.response && 'string' === typeof error.response.text ) ) {
		return;
	}

	const data = JSON.parse( error.response.text );
	if ( 'object' !== typeof data ) {
		return;
	}

	return recordEvent( 'calypso_refer_visit_response', {
		...pick( data, whitelistedEventProps ),
		status: data.status || '',
		success: data.success || '',
		description: data.message || 'success',
	} );
};

…dispatchRequestEx( { fetch, onSuccess, onError, fromApi } )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really appreciate you explaining fromApi. What you suggest is much more elegant and I see the try/catch block switches back to onError as you noted. Very nice.

@jaswrks jaswrks force-pushed the update/affiliate-tracking branch 2 times, most recently from f06290d to 018a8ea Compare October 25, 2018 06:12
@jaswrks jaswrks added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] Ready to Merge labels Oct 25, 2018
@jaswrks
Copy link
Contributor Author

jaswrks commented Oct 25, 2018

Suggested changes implemented. This could use a follow-up review now.

Copy link
Contributor

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the updates. please do share your thoughts if you want to on the changes you made with fromApi; I'd be curious to hear.

I did not test these changes; this approval is based on code auditing of only part of the problem so please rely on others for approval 😄

@lsinger
Copy link
Contributor

lsinger commented Oct 26, 2018

🤔 success and status are just empty strings for me:

screen shot 2018-10-26 at 4 55 53 pm

@jaswrks
Copy link
Contributor Author

jaswrks commented Oct 26, 2018

@lsinger Great catch. Thanks! I've updated this PR to resolve that problem.

@lsinger
Copy link
Contributor

lsinger commented Oct 30, 2018

Thanks for the update @jaswrks! This seem to work well now. I did notice an extra duplicate field for the first test that I'm not sure is supposed to be there. It's in the "whitelisted" list but I didn't see a mention of it anywhere else:

screen shot 2018-10-30 at 12 19 47 pm

If this is intended — :shipit:!

@lsinger lsinger added [Status] Ready to Merge and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Oct 30, 2018
@lsinger lsinger self-requested a review October 30, 2018 11:24
@jaswrks
Copy link
Contributor Author

jaswrks commented Oct 30, 2018

@lsinger Thanks! Yep, the duplicate flag is new on the Refer platform side. It works as an indication that you've already been counted by the affiliate platform. That flag is one reason to get this in; i.e., so we can see all of this data in Tracks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Tracks Metrics & Monitoring Capturing analytics about user behavior on WordPress.com.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants