Skip to content

Commit

Permalink
Settings: Add new dropdown for choosing analytics behavior (#13567)
Browse files Browse the repository at this point in the history
Co-authored-by: Swanand Mathekar <[email protected]>
Co-authored-by: Swanand01 <[email protected]>
  • Loading branch information
3 people authored Feb 16, 2024
1 parent 041cea6 commit 4353ae9
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 101 deletions.
26 changes: 24 additions & 2 deletions includes/Integrations/Site_Kit.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Google\Web_Stories\Analytics;
use Google\Web_Stories\Context;
use Google\Web_Stories\Service_Base;
use Google\Web_Stories\Settings;

/**
* Class Site_Kit.
Expand Down Expand Up @@ -64,17 +65,26 @@ class Site_Kit extends Service_Base {
*/
private Plugin_Status $plugin_status;

/**
* Settings instance.
*
* @var Settings Settings instance.
*/
private Settings $settings;

/**
* Constructor.
*
* @param Analytics $analytics Analytics instance.
* @param Context $context Context instance.
* @param Plugin_Status $plugin_status Plugin_Status instance.
* @param Settings $settings Settings instance.
*/
public function __construct( Analytics $analytics, Context $context, Plugin_Status $plugin_status ) {
public function __construct( Analytics $analytics, Context $context, Plugin_Status $plugin_status, Settings $settings ) {
$this->analytics = $analytics;
$this->context = $context;
$this->plugin_status = $plugin_status;
$this->settings = $settings;
}

/**
Expand All @@ -85,7 +95,19 @@ public function __construct( Analytics $analytics, Context $context, Plugin_Stat
public function register(): void {
add_filter( 'googlesitekit_amp_gtag_opt', [ $this, 'filter_site_kit_gtag_opt' ] );

if ( $this->is_analytics_module_active() ) {
$handler = $this->settings->get_setting( $this->settings::SETTING_NAME_TRACKING_HANDLER );

if ( 'web-stories' === $handler ) {
add_filter(
'googlesitekit_analytics-4_tag_amp_blocked',
function ( $blocked ) {
if ( $this->context->is_web_story() ) {
return true;
}
return $blocked;
}
);
} elseif ( 'site-kit' === $handler && $this->is_analytics_module_active() ) {
remove_action( 'web_stories_print_analytics', [ $this->analytics, 'print_analytics_tag' ] );
}
}
Expand Down
22 changes: 22 additions & 0 deletions includes/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ class Settings implements Service, Registerable, PluginUninstallAware {
*/
public const SETTING_NAME_DEFAULT_PAGE_DURATION = 'web_stories_default_page_duration';

/**
* GA Tracking Handler.
*/
public const SETTING_NAME_TRACKING_HANDLER = 'web_stories_ga_tracking_handler';

/**
* Shopping_Vendors instance.
*
Expand Down Expand Up @@ -403,6 +408,22 @@ public function register(): void {
'show_in_rest' => true,
]
);

register_setting(
self::SETTING_GROUP,
self::SETTING_NAME_TRACKING_HANDLER,
[
'description' => __( 'Tracking Handler', 'web-stories' ),
'type' => 'string',
'default' => 'site-kit',
'show_in_rest' => [
'schema' => [
'type' => 'string',
'enum' => [ 'site-kit', 'web-stories', 'both' ],
],
],
]
);
}

/**
Expand Down Expand Up @@ -495,6 +516,7 @@ public function on_plugin_uninstall(): void {
delete_option( self::SETTING_NAME_SHOPIFY_ACCESS_TOKEN );
delete_option( self::SETTING_NAME_DEFAULT_PAGE_DURATION );
delete_option( self::SETTING_NAME_AUTO_ADVANCE );
delete_option( self::SETTING_NAME_TRACKING_HANDLER );
}

/**
Expand Down
12 changes: 1 addition & 11 deletions packages/e2e-tests/src/specs/integrations/sitekit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ describe('Site Kit plugin integration', () => {
describe('Google Analytics', () => {
withPlugin('e2e-tests-site-kit-analytics-mock');

describe('Dashboard', () => {
it('should see Site Kit specific message', async () => {
await visitSettings();

await expect(page).toMatchTextContent(
'Site Kit by Google has already enabled Google Analytics for your Web Stories'
);
});
});

describe('Editor', () => {
afterAll(async () => {
await trashAllPosts('web-story');
Expand All @@ -69,7 +59,7 @@ describe('Site Kit plugin integration', () => {
withPlugin('e2e-tests-site-kit-adsense-mock');

describe('Dashboard', () => {
it('should see Site Kit specific message', async () => {
it('should see Google AdSense specific message', async () => {
await visitSettings();

await expect(page).toMatchTextContent(
Expand Down
3 changes: 3 additions & 0 deletions packages/wp-dashboard/src/api/reducers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
AD_NETWORK_TYPE,
ARCHIVE_TYPE,
SHOPPING_PROVIDER_TYPE,
GOOGLE_ANALYTICS_HANDLER_TYPE,
} from '../../constants';

export const ACTION_TYPES = {
Expand Down Expand Up @@ -49,6 +50,7 @@ export const defaultSettingsState = {
shopifyAccessToken: '',
autoAdvance: true,
defaultPageDuration: 7,
googleAnalyticsHandler: GOOGLE_ANALYTICS_HANDLER_TYPE.SITE_KIT,
};

function settingsReducer(state, action) {
Expand Down Expand Up @@ -89,6 +91,7 @@ function settingsReducer(state, action) {
shopifyAccessToken: action.payload.shopifyAccessToken,
autoAdvance: action.payload.autoAdvance,
defaultPageDuration: action.payload.defaultPageDuration,
googleAnalyticsHandler: action.payload.googleAnalyticsHandler,
};
}

Expand Down
6 changes: 6 additions & 0 deletions packages/wp-dashboard/src/api/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const transformSettingResponse = (response) => ({
shopifyAccessToken: response.web_stories_shopify_access_token,
autoAdvance: Boolean(response.web_stories_auto_advance),
defaultPageDuration: response.web_stories_default_page_duration,
googleAnalyticsHandler: response.web_stories_ga_tracking_handler,
});

/**
Expand Down Expand Up @@ -86,6 +87,7 @@ export function updateSettings(apiPath, queryParams) {
shopifyAccessToken,
autoAdvance,
defaultPageDuration,
googleAnalyticsHandler,
} = queryParams;

const query = {};
Expand Down Expand Up @@ -154,6 +156,10 @@ export function updateSettings(apiPath, queryParams) {
query.web_stories_default_page_duration = defaultPageDuration;
}

if (googleAnalyticsHandler !== undefined) {
query.web_stories_ga_tracking_handler = googleAnalyticsHandler;
}

const path = addQueryArgs(apiPath, query);

return apiFetch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function EditorSettings() {
shopifyAccessToken,
autoAdvance,
defaultPageDuration,
googleAnalyticsHandler,
} = useEditorSettings(
({
actions: {
Expand Down Expand Up @@ -117,6 +118,7 @@ function EditorSettings() {
shopifyAccessToken,
autoAdvance,
defaultPageDuration,
googleAnalyticsHandler,
},
media: { isLoading: isMediaLoading, newlyCreatedMediaIds },
publisherLogos: { publisherLogos },
Expand Down Expand Up @@ -155,6 +157,7 @@ function EditorSettings() {
shopifyAccessToken,
autoAdvance,
defaultPageDuration,
googleAnalyticsHandler,
})
);

Expand Down Expand Up @@ -329,6 +332,12 @@ function EditorSettings() {
[setPublisherLogoAsDefault]
);

const handleUpdateGoogleAnalyticsHandler = useCallback(
(newGoogleAnalyticsHandler) =>
updateSettings({ googleAnalyticsHandler: newGoogleAnalyticsHandler }),
[updateSettings]
);

return (
<Layout.Provider>
<Wrapper data-testid="editor-settings">
Expand All @@ -346,6 +355,10 @@ function EditorSettings() {
usingLegacyAnalytics={usingLegacyAnalytics}
handleMigrateLegacyAnalytics={handleMigrateLegacyAnalytics}
siteKitStatus={siteKit}
googleAnalyticsHandler={googleAnalyticsHandler}
handleUpdateGoogleAnalyticsHandler={
handleUpdateGoogleAnalyticsHandler
}
/>
<PublisherLogoSettings
onAddLogos={handleAddLogos}
Expand Down
Loading

0 comments on commit 4353ae9

Please sign in to comment.