Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | 51x 312x 312x 104x 208x 208x 6x 312x 110x 202x 202x 116x 86x 258x 258x 131x 86x 312x 312x 312x 312x 312x 312x 26x 312x 312x 785x 312x 298x 312x 204x 312x 11x 312x 312x 312x 312x 3x 3x 3x 312x 312x 312x | /**
* External dependencies
*/
import {
Button,
Dialog,
InputControl,
Text,
ValidatedInputControl,
} from '@jetpack-premium-analytics/externals';
import { useCopyToClipboard } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
/**
* Internal dependencies
*/
import styles from './utm-url-builder-dialog.module.scss';
const COPIED_LABEL_DURATION = 4000;
type UtmValues = {
destination: string;
campaign: string;
source: string;
medium: string;
};
function parseDestination( destination: string ): URL | null {
const address = destination.trim();
if ( ! /^https?:\/\//i.test( address ) ) {
return null;
}
try {
return new URL( address );
} catch {
return null;
}
}
function buildTrackedUrl(
url: URL | null,
{ campaign, source, medium }: Omit< UtmValues, 'destination' >
): string | null {
if ( ! url ) {
return null;
}
const parameters = {
utm_campaign: campaign.trim(),
utm_source: source.trim(),
utm_medium: medium.trim(),
};
if ( ! Object.values( parameters ).some( Boolean ) ) {
return null;
}
for ( const [ key, value ] of Object.entries( parameters ) ) {
url.searchParams.delete( key );
if ( value ) {
url.searchParams.set( key, value );
}
}
return url.toString();
}
export function UtmUrlBuilderDialog( { onClose }: { onClose: () => void } ) {
const [ values, setValues ] = useState< UtmValues >( {
destination: '',
campaign: '',
source: '',
medium: '',
} );
const [ copiedUrl, setCopiedUrl ] = useState< string | null >( null );
const copiedTimeout = useRef< ReturnType< typeof setTimeout > >( undefined );
const destinationUrl = parseDestination( values.destination );
const isDestinationInvalid = values.destination.trim() !== '' && ! destinationUrl;
const destinationValidity = useMemo(
() =>
isDestinationInvalid
? {
type: 'invalid' as const,
message: __(
'Enter a full URL that starts with https:// or http://.',
'jetpack-premium-analytics-pkg'
),
}
: undefined,
[ isDestinationInvalid ]
);
const trackedUrl = buildTrackedUrl( destinationUrl, values );
const hasParameters = [ values.campaign, values.source, values.medium ].some( value =>
value.trim()
);
const setField = useCallback( ( field: keyof UtmValues, value: string ) => {
setValues( current => ( { ...current, [ field ]: value } ) );
}, [] );
const setDestination = useCallback(
( value: string ) => setField( 'destination', value ),
[ setField ]
);
const setCampaign = useCallback(
( value: string ) => setField( 'campaign', value ),
[ setField ]
);
const setSource = useCallback( ( value: string ) => setField( 'source', value ), [ setField ] );
const setMedium = useCallback( ( value: string ) => setField( 'medium', value ), [ setField ] );
const handleOpenChange = useCallback(
( open: boolean ) => {
if ( ! open ) {
onClose();
}
},
[ onClose ]
);
const copyRef = useCopyToClipboard< HTMLButtonElement >( trackedUrl ?? '', () => {
setCopiedUrl( trackedUrl );
clearTimeout( copiedTimeout.current );
copiedTimeout.current = setTimeout( () => setCopiedUrl( null ), COPIED_LABEL_DURATION );
} );
useEffect( () => () => clearTimeout( copiedTimeout.current ), [] );
// Compared with the current URL, so an edit after a copy ends the confirmation.
const isCopied = copiedUrl !== null && copiedUrl === trackedUrl;
return (
<Dialog.Root open onOpenChange={ handleOpenChange }>
<Dialog.Popup size="large">
<Dialog.Header>
<Dialog.Title>{ __( 'Build URL', 'jetpack-premium-analytics-pkg' ) }</Dialog.Title>
<Dialog.CloseIconButton />
</Dialog.Header>
<Dialog.Content>
<div className={ styles.layout }>
<div className={ styles.form }>
<Text render={ <p /> }>
{ __(
'Generate URLs to share and track UTM parameters.',
'jetpack-premium-analytics-pkg'
) }
</Text>
<ValidatedInputControl
label={ __( 'Destination URL', 'jetpack-premium-analytics-pkg' ) }
type="url"
value={ values.destination }
onValueChange={ setDestination }
customValidity={ destinationValidity }
/>
<InputControl
label={ __( 'UTM campaign', 'jetpack-premium-analytics-pkg' ) }
value={ values.campaign }
onValueChange={ setCampaign }
placeholder={ __( 'e.g. promotion', 'jetpack-premium-analytics-pkg' ) }
/>
<InputControl
label={ __( 'UTM source', 'jetpack-premium-analytics-pkg' ) }
value={ values.source }
onValueChange={ setSource }
placeholder={ __( 'e.g. newsletter', 'jetpack-premium-analytics-pkg' ) }
/>
<InputControl
label={ __( 'UTM medium', 'jetpack-premium-analytics-pkg' ) }
value={ values.medium }
onValueChange={ setMedium }
placeholder={ __( 'e.g. email, social', 'jetpack-premium-analytics-pkg' ) }
/>
<div className={ styles.result }>
<Text variant="heading-md" render={ <h3 /> }>
{ __( 'Your URL to share', 'jetpack-premium-analytics-pkg' ) }
</Text>
<div className={ styles.resultValue }>
{ trackedUrl ??
( hasParameters
? __(
'Enter a destination URL to see the URL',
'jetpack-premium-analytics-pkg'
)
: __(
'Fill out campaign parameters to see the URL',
'jetpack-premium-analytics-pkg'
) ) }
</div>
</div>
</div>
<aside className={ styles.help }>
<Text variant="heading-md" render={ <h3 /> }>
{ __( 'Why should I use this?', 'jetpack-premium-analytics-pkg' ) }
</Text>
<Text render={ <p /> }>
{ __(
'UTM codes help track where your traffic comes from. Adding them to your URLs gives you insights into what works and where to optimize.',
'jetpack-premium-analytics-pkg'
) }
</Text>
<dl>
<dt>
<code>utm_campaign</code>
</dt>
<dd>
{ __( 'Name your campaign', 'jetpack-premium-analytics-pkg' ) }
<em>
{ __( 'Example: christmas, flash-sale', 'jetpack-premium-analytics-pkg' ) }
</em>
</dd>
<dt>
<code>utm_source</code>
</dt>
<dd>
{ __( 'Define where traffic originates', 'jetpack-premium-analytics-pkg' ) }
<em>
{ __(
'Example: newsletter, facebook, google',
'jetpack-premium-analytics-pkg'
) }
</em>
</dd>
<dt>
<code>utm_medium</code>
</dt>
<dd>
{ __( 'Define the channel type', 'jetpack-premium-analytics-pkg' ) }
<em>{ __( 'Example: email, social, cpc', 'jetpack-premium-analytics-pkg' ) }</em>
</dd>
</dl>
<Text render={ <p /> }>
{ __(
'Use your generated URLs in social posts, emails, or ads. Jetpack Stats will track UTM codes, giving you accurate insights into your traffic.',
'jetpack-premium-analytics-pkg'
) }
</Text>
</aside>
</div>
</Dialog.Content>
<Dialog.Footer>
{ /* A disabled Button stays clickable (aria-disabled), so the copy listener is attached only with a URL. */ }
<Button
ref={ trackedUrl ? copyRef : undefined }
variant="solid"
size="compact"
disabled={ ! trackedUrl }
>
{ isCopied
? __( 'Copied!', 'jetpack-premium-analytics-pkg' )
: __( 'Copy to clipboard', 'jetpack-premium-analytics-pkg' ) }
</Button>
</Dialog.Footer>
</Dialog.Popup>
</Dialog.Root>
);
}
|