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

Remove Universal Analytics #322

Merged
merged 139 commits into from
Mar 4, 2024
Merged
Changes from 1 commit
Commits
Show all changes
139 commits
Select commit Hold shift + click to select a range
8ebae1d
Add settings field
Oct 3, 2023
26d7c1d
Add option defaults
Oct 3, 2023
1a99cc6
Restructure get_product_identifier
Oct 3, 2023
f68cb04
Add unit tests
Oct 3, 2023
0ddd6f8
Add default option value
martynmjones Oct 3, 2023
09fb50c
Set default options for new installations
martynmjones Oct 3, 2023
08e0268
Fix CS
martynmjones Oct 3, 2023
daf592b
Fix CS
Oct 4, 2023
eea89a6
Update product object structure
Oct 4, 2023
ed8af32
Update view_item_list event
Oct 4, 2023
6641bf6
Update add_to_cart event
Oct 4, 2023
a663b12
Update remove_from_cart event
Oct 4, 2023
80dde61
Add begin_checkout event
Oct 5, 2023
92bafdd
Remove email, phone, and billing address events
Oct 5, 2023
bd25a7e
Add add_shipping_info event
Oct 5, 2023
b6ec761
Remove trackCheckoutStep
Oct 5, 2023
bd73c66
Track add_shipping_info on checkout submit
Oct 5, 2023
1dabbe1
Remove trackCheckoutOption
Oct 5, 2023
0f614c9
Remove add_payment_info event
Oct 5, 2023
9aef6f5
Update select_content event
Oct 5, 2023
65fcc7a
Remove @wordpress/i18n import
Oct 5, 2023
662a963
Remove currentStep
Oct 5, 2023
848c446
Export getProductId
Oct 5, 2023
4371430
Update comment
Oct 9, 2023
158583c
Merge pull request #309 from woocommerce/add/295-product-identifier-s…
martynmjones Oct 9, 2023
9840d7f
Check data exists before tracking view_item_list
Oct 25, 2023
e615217
Check product for quantity
Oct 26, 2023
c566ea7
Remove tracking from class-wc-google-analytics.php
Oct 27, 2023
be97de4
Remove UA settings
Oct 27, 2023
f1e830d
Remove class-wc-google-analytics-js.php
Oct 27, 2023
036f1eb
Remove admin-ga-settings.js
Oct 27, 2023
3188a55
Remove unused alias
Oct 27, 2023
1c54142
Remove UA tracking from abstract class
Oct 27, 2023
2ee2fad
Move tracking to class-wc-google-gtag-js.php
Oct 27, 2023
2ce30f6
Fix unit tests
Oct 27, 2023
acbe1a4
Add notice if property ID starts with "UA"
Oct 27, 2023
6c818bc
CS Fixes
Oct 30, 2023
fe0ced4
Fix event name
Oct 30, 2023
2b48a17
Re-order settings fields
Oct 30, 2023
a7e9ac4
Remove event_tracking_code
Oct 30, 2023
8082ac2
Fix alignment
Oct 30, 2023
a3d1ae9
Merge pull request #320 from woocommerce/remove/ua-code
martynmjones Oct 30, 2023
1f0b6fc
Escape notice text and update external URL
martynmjones Oct 31, 2023
27ba6d9
Merge pull request #321 from woocommerce/add/ua-update-notice
martynmjones Oct 31, 2023
4a06f52
Update formatPrice to return a number
Nov 1, 2023
f6041b9
Conditionally include coupon
Nov 1, 2023
813b7ee
Add cart coupon utility
martynmjones Nov 1, 2023
45f2b5a
Merge pull request #311 from woocommerce/update/268-tracking-code-for…
martynmjones Nov 2, 2023
0e945df
Remove inline JS tracking
Nov 8, 2023
5eee69e
Remove abstract methods
Nov 8, 2023
273ef92
Add inline script data
Nov 8, 2023
01facf0
Format and attach event data in abstract class
Nov 9, 2023
18f6ba1
Map classic hooks to Blocks and inline event data
Nov 10, 2023
4d33305
Update wcgaiData single product name
Nov 17, 2023
6a563e2
Remove key for wcgaiData products
Nov 17, 2023
3506c88
Fix array keys
Nov 17, 2023
738f3ec
Update event map and script data
Nov 17, 2023
d5a659e
Add a tracker utility
Nov 17, 2023
977f49a
Update utils
Nov 17, 2023
e24a184
Move actions to data formatting functions
Nov 17, 2023
bb6ecc7
Add tracker utility
Nov 17, 2023
bd5f4cf
Add blocks and classic integrations
Nov 17, 2023
e14d27f
Add config file
Nov 17, 2023
3a36b96
Add new entry point
Nov 17, 2023
a2349aa
Remove enqueue_standard_tracking_code
Nov 20, 2023
82d2ce2
Update method signature
Nov 20, 2023
5ebea9c
Remove event map
Dec 7, 2023
d135e41
Rename data formatters
Dec 7, 2023
1fb3336
Restructure Tracker
Dec 7, 2023
d114b28
Update tracking
Dec 7, 2023
d7d9add
Reduce categories data to name only
Dec 7, 2023
4d65dfc
Update event => WC hook map
Dec 7, 2023
08ce30e
Update Tracker
Dec 10, 2023
326c59e
Add utility to find a single product by its ID
Dec 10, 2023
58a6fc2
Allow for single entry for data type in script data
Dec 10, 2023
89900a1
Track add to cart events
Dec 10, 2023
cabeb45
Fix JS formatting issues
martynmjones Jan 18, 2024
87d82f6
Remove unused PHP tests
martynmjones Jan 18, 2024
e4dd675
Update composer lock file
martynmjones Jan 18, 2024
6301d5e
Update composer.json
martynmjones Jan 18, 2024
17abd40
Remove calls to deleted functions
martynmjones Jan 19, 2024
d9c754b
Update cart formatting
martynmjones Jan 23, 2024
8616b72
Add remove_from_cart tracking
martynmjones Jan 23, 2024
67bfd6b
Add begin_checkout tracking
martynmjones Jan 24, 2024
3c4f670
Add select_content tracking
martynmjones Jan 24, 2024
af0b665
Add purchase tracking
martynmjones Jan 24, 2024
3a24994
Fix param description
martynmjones Jan 24, 2024
e12c442
Fix price formatting
martynmjones Jan 25, 2024
f99c620
Add check for enabled events
martynmjones Jan 25, 2024
5ef1fb6
Use correct product identifier
martynmjones Jan 25, 2024
2646ad9
Add product identifier to cart item
martynmjones Jan 25, 2024
48ee481
Add unit tests
martynmjones Jan 25, 2024
dc7fb24
Refactor test_script_data to support previous versions of PHP
martynmjones Jan 25, 2024
66142e2
Refactor getProductId util
martynmjones Jan 25, 2024
9104cb9
Add support for product variations
martynmjones Jan 26, 2024
9ba0a7f
Update plugin name to Google Analytics for WooCommerce
martynmjones Jan 31, 2024
2ea428c
Additional name updates
martynmjones Feb 1, 2024
155b32f
Merge pull request #353 from woocommerce/update/branding-to-google-an…
martynmjones Feb 1, 2024
5395991
Simplify tracker event handler API,
tomalec Feb 8, 2024
8fa9baf
Merge pull request #357 from woocommerce/update/tracking-for-classic-…
martynmjones Feb 9, 2024
c1bb15a
Fix price formatting
martynmjones Feb 9, 2024
b1a0de1
Remove unnecessary check
martynmjones Feb 9, 2024
bed5877
Remove demo comment
martynmjones Feb 9, 2024
7fcab41
Assign formatter to constant inside eventHandler
martynmjones Feb 9, 2024
c4931e3
Simplify set_script_data
martynmjones Feb 9, 2024
46dd222
Rename tracker_var to tracker_function_name
martynmjones Feb 9, 2024
930cf21
Update unit tests
martynmjones Feb 9, 2024
db37466
Update config export
martynmjones Feb 9, 2024
d625984
Fix addedToCart declaration
martynmjones Feb 9, 2024
9f60881
Remove spread operator
martynmjones Feb 19, 2024
9a1e058
Update classic select_content tracking
martynmjones Feb 26, 2024
234fe84
Update remove_from_cart tracking and add mini-cart support
martynmjones Feb 26, 2024
7ff8829
Update select_content selector
martynmjones Feb 26, 2024
8272538
Add comment describing why some actions are removed
martynmjones Feb 26, 2024
81f719e
Fix linting errors
martynmjones Feb 26, 2024
0bc1a5b
Remove options remapping, use settings directly.
tomalec Feb 26, 2024
899c0d3
Remove redundant WC_Google_Analytics properties
tomalec Feb 26, 2024
1bd5af3
Fix ID mismatch when removing a variation from cart
martynmjones Feb 26, 2024
c979cf0
Adjust append_script_data
martynmjones Feb 26, 2024
9fea878
Rename options to settings where applicable.
tomalec Feb 26, 2024
96603e2
Merge branch 'update/tracking-for-classic-pages' into dev/refactor-op…
tomalec Feb 26, 2024
e893d8c
Avoid overwriting third-party event handlers
martynmjones Feb 27, 2024
94cc036
Adjust select_content conditions and update comment
martynmjones Feb 27, 2024
e27406d
Fix spacing
martynmjones Feb 27, 2024
3dca31f
Merge pull request #328 from woocommerce/update/tracking-for-classic-…
martynmjones Feb 27, 2024
4cf040d
Implement MVP for consent mode support.
tomalec Feb 27, 2024
6d1a82c
Update custom consent snippet
tomalec Feb 27, 2024
352fa29
Set `wp_enqueue_scripts` priority
tomalec Feb 28, 2024
8a7eb7b
Dequeue the WC Blocks GA integration srcipt
tomalec Feb 28, 2024
afa53c9
Process tracker config separately from feeding data to classic tracking
tomalec Feb 28, 2024
c4e9ebc
Merge pull request #363 from woocommerce/fix/split-config
tomalec Feb 29, 2024
1bdf0b6
Merge branch 'remove-universal-analytics' into add/consent-mode-v0
tomalec Feb 29, 2024
372dc75
Update README
tomalec Feb 29, 2024
d8c750c
Fix the consent region code from `UK` to `GB`
tomalec Mar 1, 2024
f92124b
Merge pull request #360 from woocommerce/dev/refactor-options
tomalec Mar 1, 2024
d221ed3
Merge pull request #361 from woocommerce/add/consent-mode-v0
tomalec Mar 1, 2024
f0cf45b
Resolve merge conflicts
martynmjones Mar 4, 2024
b2def8a
PHPCS
martynmjones Mar 4, 2024
b47bf99
Fix JSDoc spacing
martynmjones Mar 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Restructure Tracker
Martyn Jones committed Dec 7, 2023
commit 1fb333604193a0416eab0f087af5f7670b5962c9
166 changes: 72 additions & 94 deletions assets/js/src/tracker/index.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,82 @@
import { config } from '../config';
import * as formatters from './data-formatting';

let instance;

/**
* A tracking utility for initializing a GA4, managing accepted
* events, attaching data to those events, and tracking them.
*
* @param {Object} config Configuration settings for the tracker.
* A tracking utility for initializing a GA4 and tracking accepted events.
*
* @return {Object} An object containing methods: init, setupEvents, event{ attach, get, track }.
* @class
*/
export const tracker = ( () => {
const events = [];

return {
/**
* Initializes the tracker and dataLayer if not already done.
*/
init: () => {
if ( window[ config.tracker_var ] ) {
// Tracker already initialized. Do nothing.
return;
}
class Tracker {
/**
* Constructs a new instance of the Tracker class.
*
* @throws {Error} If an instance of the Tracker already exists.
*/
constructor() {
if( instance ) {
throw new Error( 'Cannot instantiate more than one Tracker' );
}
instance = this;
instance.init();
}

window.dataLayer = window.dataLayer || [];
window[ config.tracker_var ] = function () {
window.dataLayer.push( arguments );
};
window[ config.tracker_var ]( 'js', new Date() );
window[ config.tracker_var ](
'set',
`developer_id.${ config.developer_id }`,
true
);
window[ config.tracker_var ]( 'config', config.gtag_id, {
allow_google_signals: config.allow_google_signals,
link_attribution: config.link_attribution,
anonymize_ip: config.anonymize_ip,
logged_in: config.logged_in,
linker: config.linker,
custom_map: config.custom_map,
} );
},
/**
* Each event in the array is added to a global `events` object using its `name` as the key.
*
* @param {Object[]} eventsArray An array of event objects.
*/
setupEvents: ( eventsArray ) => {
eventsArray.forEach(
( event ) => ( events[ event.name ] = event )
);
},
/**
* An object to control events which exist in the global `events` object.
*
* @param {string} event - The name of the event.
*
* @return {Object} An object containing methods to attach, get, and track the event.
*/
event: ( event ) => {
return {
/**
* Attach data to an event and track it.
*
* @param {*} data - Data to be used for the event.
*/
attach: ( data ) => {
const eventObject = tracker.event( event ).get();
const eventData = eventObject.callback( data );
/**
* Initializes the tracker and dataLayer if not already done.
*/
init() {
if ( window[ config.tracker_var ] ) {
// Tracker already initialized. Do nothing.
return;
}

// The experimental hooks from Blocks are sometimes triggered
// without data so we'll only track events with data.
if ( eventData ) {
tracker.event( eventObject.name ).track( eventData );
}
},
window.dataLayer = window.dataLayer || [];

function gtag() {
window.dataLayer.push( arguments );
}

window[ config.tracker_var ] = gtag;

/**
* Retrieves the event object, or returns false if not found.
*
* @return {Object|boolean} The event object or false if the event does not exist.
*/
get: () => {
return events[ event ] ?? false;
},
gtag( 'js', new Date() );
gtag(
'set',
`developer_id.${ config.developer_id }`,
true
);
gtag( 'config', config.gtag_id, {
allow_google_signals: config.allow_google_signals,
link_attribution: config.link_attribution,
anonymize_ip: config.anonymize_ip,
logged_in: config.logged_in,
linker: config.linker,
custom_map: config.custom_map,
} );
}

/**
* Tracks the event with associated data.
*
* @param {*} data - Data to be tracked with the event.
*/
track: ( data ) => {
window[ config.tracker_var ](
'event',
tracker.event( event ).get().name,
data
);
},
};
},
};
} )();
/**
* Creates and returns an event handler object for a specified event name.
*
* @param {string} name The name of the event.
* @returns {{handler: function(*): void}} An object with a `handler` method for processing and tracking the event.
* @throws {Error} If the event name is not supported.
*/
event( name ) {
if( ! formatters[ name ] ) {
throw new Error( `Event ${ name } is not supported.` );
}

return {
handler: ( data ) => {
window[ config.tracker_var ](
'event',
name,
formatters[ name ]( data )
);
},
};
}
}

tracker.init();
export const tracker = Object.freeze( new Tracker() );