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

Restructure gtag and event data config loading #362

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
14 changes: 5 additions & 9 deletions assets/js/src/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/* global wcgaiData */
/* eslint-disable camelcase */
export const {
config,
events,
cart,
products,
product,
added_to_cart: addedToCart,
order,
} = wcgaiData;
export const eventData = function() {

Check failure on line 3 in assets/js/src/config.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

[prettier/prettier] Insert `·`
return wcgaiData;
martynmjones marked this conversation as resolved.
Show resolved Hide resolved
};

export const trackerData = wcgaiTracker;

Check failure on line 7 in assets/js/src/config.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

[no-undef] 'wcgaiTracker' is not defined.

Check failure on line 7 in assets/js/src/config.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

[prettier/prettier] Insert `⏎`
10 changes: 8 additions & 2 deletions assets/js/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { eventData } from './config.js';
import { trackClassicIntegration } from './integrations/classic'

Check failure on line 2 in assets/js/src/index.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

[prettier/prettier] Insert `;`

// Initialize tracking for classic WooCommerce pages
import { trackClassicIntegration } from './integrations/classic';
trackClassicIntegration();
document.addEventListener( 'DOMContentLoaded', () => {
martynmjones marked this conversation as resolved.
Show resolved Hide resolved
trackClassicIntegration({

Check failure on line 6 in assets/js/src/index.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

[prettier/prettier] Replace `····trackClassicIntegration(` with `↹trackClassicIntegration(·`
...eventData()

Check failure on line 7 in assets/js/src/index.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

[prettier/prettier] Replace `········...eventData()` with `↹↹...eventData(),`
});

Check failure on line 8 in assets/js/src/index.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

[prettier/prettier] Replace `····}` with `↹}·`
});

Check failure on line 9 in assets/js/src/index.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

[prettier/prettier] Insert `·`

// Initialize tracking for Block based WooCommerce pages
import './integrations/blocks';
17 changes: 8 additions & 9 deletions assets/js/src/integrations/classic.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { tracker } from '../tracker';
import { getProductFromID } from '../utils';
import {
events,
cart,
products,
product,
addedToCart,
order,
} from '../config.js';

/**
* The Google Analytics integration for classic WooCommerce pages
Expand All @@ -18,7 +10,14 @@ import {
* 3. Listen for various actions (i.e clicks) on specific elements.
*/

export const trackClassicIntegration = () => {
export const trackClassicIntegration = ( {
events,
cart,
products,
product,
added_to_cart: addedToCart,
order,
} ) => {
const eventData = {
storeCart: cart,
products,
Expand Down
2 changes: 1 addition & 1 deletion assets/js/src/tracker/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { config } from '../config';
import { trackerData as config } from '../config.js';
import * as formatters from './data-formatting';

let instance;
Expand Down
6 changes: 6 additions & 0 deletions includes/class-wc-google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public function __construct() {

// utm_nooverride parameter for Google AdWords
add_filter( 'woocommerce_get_return_url', array( $this, 'utm_nooverride' ) );

// Dequeue the WooCommerce Blocks Google Analytics integration
add_action( 'wp_enqueue_scripts', function() {
wp_dequeue_script( 'wc-blocks-google-analytics' );
});

}

/**
Expand Down
36 changes: 27 additions & 9 deletions includes/class-wc-google-gtag-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class WC_Google_Gtag_JS extends WC_Abstract_Google_Analytics_JS {
/** @var string $script_handle Handle for the front end JavaScript file */
public $script_handle = 'woocommerce-google-analytics-integration';

/** @var string $script_handle Handle for the event data inline script */
public $data_script_handle = 'woocommerce-google-analytics-integration-data';

/** @var string $script_data Data required for frontend event tracking */
private $script_data = array();

Expand Down Expand Up @@ -43,7 +46,7 @@ public function __construct( $options = array() ) {
$this->map_actions();

// Setup frontend scripts
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ), 5 );
add_action( 'wp_footer', array( $this, 'inline_script_data' ) );
}

Expand Down Expand Up @@ -71,22 +74,37 @@ public function register_scripts(): void {
Plugin::get_instance()->get_js_asset_version( 'main' ),
true
);

wp_add_inline_script(
$this->script_handle,
sprintf(
'const wcgaiTracker = %s;',
wp_json_encode( $this->load_analytics_config() )
),
'before'
);
}

/**
* Add inline script data to the front end
* Add the event data inline script late to ensure it's added at the bottom of the page
*
* @return void
*/
public function inline_script_data(): void {
wp_register_script(
$this->data_script_handle,
''
);

wp_add_inline_script(
$this->script_handle,
$this->data_script_handle,
sprintf(
'const wcgaiData = %s;',
$this->get_script_data()
),
'before'
)
);

wp_enqueue_script( $this->data_script_handle );
}

/**
Expand Down Expand Up @@ -156,12 +174,12 @@ public static function tracker_function_name(): string {
}

/**
* Add Google Analytics configuration data to the script data
* Get Google Analytics configuration data
*
* @return void
* @return array
*/
public function load_analytics_config(): void {
$this->script_data['config'] = array(
public function load_analytics_config(): array {
return array(
'developer_id' => self::DEVELOPER_ID,
'gtag_id' => self::get( 'ga_id' ),
'tracker_function_name' => self::tracker_function_name(),
Expand Down
Loading