Skip to content

Commit

Permalink
Enable custom order states for purchase only when server-side tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Oct 22, 2024
1 parent ae359c1 commit 285ca71
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 24 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.9.9] - 22 October 2024
### Fixed
- Add new order state to `order_states_for_purchase_event` default
- New setting `googletagmanager2/settings/serverside_enabled`
- Only custom order states for `purchase` when Server-Side Tracking is enabled

## [3.9.8] - 12 September 2024
### Fixed
- Fix Uncaught TypeError: products.forEach is not a function #246 @nahall
Expand Down
44 changes: 30 additions & 14 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,6 @@ public function isEnabled(): bool
return true;
}

/**
*
* Get the Google tag manager url. Defaults to googletagmanager.com. when field is filled return that url.
*
* @return string
*/
public function getGoogleTagmanagerUrl(): string
{
return $this->getModuleConfigValue(
'serverside_gtm_url',
'https://www.googletagmanager.com'
);
}

/**
* Check whether the module is in debugging mode
*
Expand Down Expand Up @@ -207,8 +193,38 @@ public function getProductListValueOnCategory(): string
return (string)$this->getModuleConfigValue('product_list_value_on_category');
}

public function hasServerSideTracking(): bool
{
return (bool)$this->getModuleConfigValue(
'serverside_enabled',
0
);
}

/**
*
* Get the Google tag manager url. Defaults to googletagmanager.com. when field is filled return that url.
*
* @return string
*/
public function getGoogleTagmanagerUrl(): string
{
if (false === $this->hasServerSideTracking()) {
return '';
}

return $this->getModuleConfigValue(
'serverside_gtm_url',
'https://www.googletagmanager.com'
);
}

public function getOrderStatesForPurchaseEvent(): array
{
if (false === $this->hasServerSideTracking()) {
return [];
}

return explode(',', (string)$this->getModuleConfigValue('order_states_for_purchase_event'));
}

Expand Down
2 changes: 1 addition & 1 deletion DataLayer/Event/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function get(): array
return [];
}

if (false === in_array($order->getState(), $this->getOrderStates())) {
if ($this->config->hasServerSideTracking() && false === in_array($order->getState(), $this->getOrderStates())) {
return [];
}

Expand Down
5 changes: 5 additions & 0 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Yes, this extension ships with native JavaScript code that works with Hyvä. Add

Currently, Luma-based checkouts work without an issue, but for using the React-based checkout or the MageWire-based checkout, more work is needed.

# This extension is not generating any `purchase` event
First of all, make sure to upgrade to the latest version of this extension, before reporting any issue.

When making use of Server-Side Tracking, make sure to customize the setting **Orders states for purchase event** (`order_states_for_purchase_event`) and to manually trigger the `purchase` event where needed.

# Does this extension work under PHP 8.2?
Yes, version 3 does. Version 2 is no longer maintained, but you could use the following composer patch with `vaimo/composer-patches`:
```bash
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yireo/magento2-googletagmanager2",
"version": "3.9.8",
"version": "3.9.9",
"license": "OSL-3.0",
"type": "magento2-module",
"homepage": "https://www.yireo.com/software/magento-extensions/googletagmanager2",
Expand Down
25 changes: 17 additions & 8 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
<field id="enabled">1</field>
</depends>
</field>
<field id="serverside_gtm_url" type="text" translate="label" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Container URL</label>
<comment><![CDATA[<span>When a value is present in this field, data will send to this url instead of https://www.googletagmanager.com.
Default output: https://gtm-test123.uc.r.appspot.com. Create a cname in the dns records for a name like marketing.shopname.com</span><br/>
<b>Keep in mind</b>; enter a full url (with https://). ]]></comment>
</field>
<field id="debug" type="select" translate="label" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Debug</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down Expand Up @@ -114,12 +108,27 @@
</depends>
<comment>Generate the view_cart event only when expanding the minicart</comment>
</field>
<field id="order_states_for_purchase_event" type="multiselect" translate="label" sortOrder="16" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="serverside_enabled" type="select" translate="label" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Server-Side Tracking</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Only enable this, if you are using some kind of Server-Side Tracking extension already. Note that this extension does not enable Server-Side Tracking.</comment>
</field>
<field id="serverside_gtm_url" type="text" translate="label" sortOrder="31" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Container URL</label>
<comment><![CDATA[<span>When a value is present in this field, data will send to this url instead of https://www.googletagmanager.com.
Default output: https://gtm-test123.uc.r.appspot.com. Create a cname in the dns records for a name like marketing.shopname.com</span><br/>
<b>Keep in mind</b>; enter a full url (with https://). ]]></comment>
<depends>
<field id="serverside_enabled">1</field>
</depends>
</field>
<field id="order_states_for_purchase_event" type="multiselect" translate="label" sortOrder="32" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Orders states for purchase event</label>
<source_model>Yireo\GoogleTagManager2\Config\Source\OrderStateOptions</source_model>
<comment>Order states at which to trigger the purchase event</comment>
<comment>Order states at which to trigger the purchase event. Note that this only makes sense when using Server-Side Tracking, because otherwise no purchase event will be triggered ever. When Server-Side Tracking is disabled in this extension, the default of this setting will be to trigger at any state.</comment>
<depends>
<field id="enabled">1</field>
<field id="serverside_enabled">1</field>
</depends>
</field>
</group>
Expand Down
2 changes: 2 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<view_cart_occurances>everywhere</view_cart_occurances>
<view_cart_on_mini_cart_expand_only>0</view_cart_on_mini_cart_expand_only>
<product_list_value_on_category>product_first_category</product_list_value_on_category>
<serverside_enabled>0</serverside_enabled>
<serverside_gtm_url/>
<order_states_for_purchase_event>new,payment_review,pending_payment,holded,processing,complete</order_states_for_purchase_event>
</settings>
</googletagmanager2>
Expand Down

0 comments on commit 285ca71

Please sign in to comment.