Skip to content

Commit

Permalink
Merge pull request #7657 from google/enhance/7460-ga-create-account-e…
Browse files Browse the repository at this point in the history
…m-switch

Add enhanced measurement switch to AccountCreate.
  • Loading branch information
tofumatt authored Oct 3, 2023
2 parents 049e6c3 + e99a379 commit 91ce4cd
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,20 @@ export default function EnhancedMeasurementSwitch( {
onClick,
disabled = false,
loading = false,
formName = ENHANCED_MEASUREMENT_FORM,
} ) {
const isEnhancedMeasurementEnabled = useSelect( ( select ) =>
select( CORE_FORMS ).getValue(
ENHANCED_MEASUREMENT_FORM,
ENHANCED_MEASUREMENT_ENABLED
)
select( CORE_FORMS ).getValue( formName, ENHANCED_MEASUREMENT_ENABLED )
);

const { setValues } = useDispatch( CORE_FORMS );

const handleClick = useCallback( () => {
setValues( ENHANCED_MEASUREMENT_FORM, {
setValues( formName, {
[ ENHANCED_MEASUREMENT_ENABLED ]: ! isEnhancedMeasurementEnabled,
} );
onClick?.();
}, [ isEnhancedMeasurementEnabled, onClick, setValues ] );
}, [ formName, isEnhancedMeasurementEnabled, onClick, setValues ] );

return (
<div
Expand Down
6 changes: 5 additions & 1 deletion assets/js/modules/analytics-4/datastore/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { isPlainObject } from 'lodash';
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import { CORE_FORMS } from '../../../googlesitekit/datastore/forms/constants';
import { MODULES_ANALYTICS_4 } from './constants';
import { ENHANCED_MEASUREMENT_ENABLED, MODULES_ANALYTICS_4 } from './constants';
import {
MODULES_ANALYTICS,
FORM_ACCOUNT_CREATE,
Expand Down Expand Up @@ -105,6 +105,10 @@ const baseActions = {
dataStreamName: getValue( FORM_ACCOUNT_CREATE, 'dataStreamName' ),
timezone: getValue( FORM_ACCOUNT_CREATE, 'timezone' ),
regionCode: getValue( FORM_ACCOUNT_CREATE, 'countryCode' ),
enhancedMeasurementStreamEnabled: getValue(
FORM_ACCOUNT_CREATE,
ENHANCED_MEASUREMENT_ENABLED
),
};

yield clearError( 'createAccount', [] );
Expand Down
3 changes: 3 additions & 0 deletions assets/js/modules/analytics-4/utils/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ import { isURL } from '@wordpress/url';
* Internal dependencies
*/
import { countryCodesByTimezone } from '../../analytics/util/countries-timezones';
import { ENHANCED_MEASUREMENT_ENABLED } from '../datastore/constants';

/**
* Gets default values for a new account.
*
* @since 1.98.0
* @since n.e.x.t Added enhanced measurement enabled state to return value.
*
* @param {Object} args Site information.
* @param {string} args.siteName Site name.
Expand Down Expand Up @@ -62,5 +64,6 @@ export function getAccountDefaults(
timezone: countryCodesByTimezone[ timezone ]
? timezone
: _fallbackTimezone,
[ ENHANCED_MEASUREMENT_ENABLED ]: true,
};
}
9 changes: 9 additions & 0 deletions assets/js/modules/analytics-4/utils/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/**
* Internal dependencies
*/
import { ENHANCED_MEASUREMENT_ENABLED } from '../datastore/constants';
import * as accountUtils from './account';

describe( 'getAccountDefaults', () => {
Expand Down Expand Up @@ -109,4 +110,12 @@ describe( 'getAccountDefaults', () => {
).toBe( fallbackTimezone );
} );
} );

describe( 'enhanced measurement enabled', () => {
it( 'defaults to true', () => {
expect(
getAccountDefaults( { siteName, siteURL, timezone } )
).toHaveProperty( ENHANCED_MEASUREMENT_ENABLED, true );
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ import CountrySelect from './CountrySelect';
import WebDataStreamField from './WebDataStreamField';
import useViewContext from '../../../../../hooks/useViewContext';
import { useFeature } from '../../../../../hooks/useFeature';
import EnhancedMeasurementSwitch from '../../../../analytics-4/components/common/EnhancedMeasurementSwitch';
const { useDispatch, useSelect } = Data;

export default function AccountCreate() {
const ga4ReportingEnabled = useFeature( 'ga4Reporting' );
const enhancedMeasurementEnabled = useFeature( 'enhancedMeasurement' );

const [ isNavigating, setIsNavigating ] = useState( false );
const accounts = useSelect( ( select ) =>
Expand Down Expand Up @@ -300,6 +302,14 @@ export default function AccountCreate() {
<TimezoneSelect />
</div>

{ enhancedMeasurementEnabled && (
<div className="googlesitekit-setup-module__inputs">
<EnhancedMeasurementSwitch
formName={ FORM_ACCOUNT_CREATE }
/>
</div>
) }

<p>
{ hasRequiredScope && (
<span>
Expand Down
30 changes: 30 additions & 0 deletions includes/Modules/Analytics/Account_Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ class Account_Ticket {
*/
protected $timezone;

/**
* Whether or not enhanced measurement should be enabled.
*
* @since n.e.x.t
* @var boolean
*/
protected $enhanced_measurement_stream_enabled;

/**
* Constructor.
*
Expand Down Expand Up @@ -157,6 +165,28 @@ public function set_timezone( $timezone ) {
$this->timezone = (string) $timezone;
}

/**
* Gets the enabled state of enhanced measurement for the data stream.
*
* @since n.e.x.t
*
* @return bool $enabled Enabled state.
*/
public function get_enhanced_measurement_stream_enabled() {
return $this->enhanced_measurement_stream_enabled;
}

/**
* Sets the enabled state of enhanced measurement for the data stream.
*
* @since n.e.x.t
*
* @param bool $enabled Enabled state.
*/
public function set_enhanced_measurement_stream_enabled( $enabled ) {
$this->enhanced_measurement_stream_enabled = (bool) $enabled;
}

/**
* Gets the array representation of the instance values.
*
Expand Down
15 changes: 15 additions & 0 deletions includes/Modules/Analytics_4.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,20 @@ private function handle_provisioning_callback( $account_id, $account_ticket ) {
)
);

if ( Feature_Flags::enabled( 'enhancedMeasurement' ) && $account_ticket->get_enhanced_measurement_stream_enabled() ) {
$this->set_data(
'enhanced-measurement-settings',
array(
'propertyID' => $property->_id,
'webDataStreamID' => $web_datastream->_id,
'enhancedMeasurementSettings' => array(
// We can hardcode this to `true` here due to the conditional invocation.
'streamEnabled' => true,
),
)
);
}

$this->sync_google_tag_settings();
}

Expand Down Expand Up @@ -988,6 +1002,7 @@ function( $property ) {
$account_ticket->set_property_name( $data['propertyName'] );
$account_ticket->set_data_stream_name( $data['dataStreamName'] );
$account_ticket->set_timezone( $data['timezone'] );
$account_ticket->set_enhanced_measurement_stream_enabled( ! empty( $data['enhancedMeasurementStreamEnabled'] ) );
// Cache the create ticket id long enough to verify it upon completion of the terms of service.
set_transient(
Analytics::PROVISION_ACCOUNT_TICKET_ID . '::' . get_current_user_id(),
Expand Down
Loading

0 comments on commit 91ce4cd

Please sign in to comment.