Skip to content

Commit

Permalink
Add/track for product activations from my jetpack (#36745)
Browse files Browse the repository at this point in the history
* Add track for activations made in my jetpack

* changelog

* Fix conditional that would fire incorrectly

* Manually update window state when product is successfully activated
  • Loading branch information
CodeyGuyDylan committed Apr 8, 2024
1 parent 0d8ac7d commit b9c9fad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
40 changes: 39 additions & 1 deletion projects/packages/my-jetpack/_inc/data/products/use-activate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import { __, sprintf } from '@wordpress/i18n';
import useAnalytics from '../../hooks/use-analytics';
import { REST_API_SITE_PRODUCTS_ENDPOINT } from '../constants';
import { QUERY_ACTIVATE_PRODUCT_KEY } from '../constants';
import useSimpleMutation from '../use-simple-mutation';
import { getMyJetpackWindowInitialState } from '../utils/get-my-jetpack-window-state';
import useProduct from './use-product';
import type { ProductCamelCase } from '../types';

const setPluginActiveState = ( productId: string ) => {
const { items } = getMyJetpackWindowInitialState( 'products' );
if ( items[ productId ]?.standalone_plugin_info.has_standalone_plugin ) {
window.myJetpackInitialState.products.items[
productId
].standalone_plugin_info.is_standalone_active = true;
window.myJetpackInitialState.products.items[
productId
].standalone_plugin_info.is_standalone_installed = true;
}
};

const getIsPluginAlreadyActive = ( detail: ProductCamelCase ) => {
const { standalonePluginInfo, isPluginActive } = detail;

if ( standalonePluginInfo?.hasStandalonePlugin ) {
return standalonePluginInfo?.isStandaloneActive;
}

return isPluginActive;
};

const useActivate = ( productId: string ) => {
const { detail, refetch } = useProduct( productId );
const { recordEvent } = useAnalytics();

const { mutate: activate, isPending } = useSimpleMutation( {
name: QUERY_ACTIVATE_PRODUCT_KEY,
Expand All @@ -14,7 +40,19 @@ const useActivate = ( productId: string ) => {
method: 'POST',
},
options: {
onSuccess: refetch,
onSuccess: () => {
if ( ! getIsPluginAlreadyActive( detail ) ) {
recordEvent( 'jetpack_myjetpack_product_activated', {
product: productId,
} );

// This is to handle an edge case where a user is redirected somewhere after activation
// and goes back in the browser and "activates" the product again. This will manually update
// the window state so that the tracking is not recorded twice for one activation.
setPluginActiveState( productId );
}
refetch();
},
},
errorMessage: sprintf(
// translators: %$1s: Jetpack Product name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Add new tracking event for product activations made through My Jetpack

0 comments on commit b9c9fad

Please sign in to comment.