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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/signup/main.jsx
Expand Up @@ -217,13 +217,13 @@ class Signup extends React.Component {
const subId = parsedUrl.query.sid;

if ( affiliateId && ! isNaN( affiliateId ) ) {
this.props.trackAffiliateReferral( { affiliateId, campaignId, subId, urlPath } );
// Record the referral in Tracks
analytics.tracks.recordEvent( 'calypso_refer_visit', {
flow: this.props.flowName,
// The current page without any query params
page: `${ parsedUrl.host }${ parsedUrl.pathname }`,
} );
this.props.trackAffiliateReferral( { affiliateId, campaignId, subId, urlPath } );
}
}

Expand Down
59 changes: 48 additions & 11 deletions client/state/data-layer/third-party/refer/index.js
Expand Up @@ -3,17 +3,31 @@
/**
* External dependencies
*/
import { noop } from 'lodash';
import { pick } from 'lodash';

/**
* Internal dependencies
*/
import { registerHandlers } from 'state/data-layer/handler-registry';
import { dispatchRequestEx } from 'state/data-layer/wpcom-http/utils';
import { http } from 'state/http/actions';
import { AFFILIATE_REFERRAL } from 'state/action-types';
import { recordTracksEvent } from 'state/analytics/actions';
import { registerHandlers } from 'state/data-layer/handler-registry';
import { dispatchRequestEx } from 'state/data-layer/wpcom-http/utils';

const trackAffiliatePageLoad = action => {
const whitelistedEventProps = [
'status',
'success',
'duplicate',
'description',
'cookie_id',
'vendor_id',
'affiliate_id',
'campaign_id',
'sub_id',
'referrer',
];

const fetch = action => {
const { affiliateId, campaignId, subId, urlPath } = action;

if ( ! affiliateId || isNaN( affiliateId ) ) {
Expand All @@ -38,12 +52,35 @@ const trackAffiliatePageLoad = action => {
);
};

const onSuccess = ( action, eventProps ) => {
return recordTracksEvent( 'calypso_refer_visit_response', eventProps );
};

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

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

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

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

registerHandlers( 'state/data-layer/third-party/refer', {
[ AFFILIATE_REFERRAL ]: [
dispatchRequestEx( {
fetch: trackAffiliatePageLoad,
onSuccess: noop,
onError: noop,
} ),
],
[ AFFILIATE_REFERRAL ]: [ dispatchRequestEx( { fetch, fromApi, onSuccess, onError } ) ],
} );