Skip to content

Commit

Permalink
Merge branch 'release/4.0.0-beta.2' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Mar 9, 2022
2 parents f5c177e + 77a0642 commit 854a9e7
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 40 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Instant Analytics Changelog

## 4.0.0-beta.2 - 2022.03.04

### Fixed

* Updated types for Craft CMS `4.0.0-alpha.1` via Rector

## 4.0.0-beta.1 - 2022.02.26

### Added
Expand Down
4 changes: 2 additions & 2 deletions README.md
@@ -1,6 +1,6 @@
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-instantanalytics/badges/quality-score.png?b=v1)](https://scrutinizer-ci.com/g/nystudio107/craft-instantanalytics/?branch=v1) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-instantanalytics/badges/coverage.png?b=v1)](https://scrutinizer-ci.com/g/nystudio107/craft-instantanalytics/?branch=v1) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-instantanalytics/badges/build.png?b=v1)](https://scrutinizer-ci.com/g/nystudio107/craft-instantanalytics/build-status/v1) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-instantanalytics/badges/code-intelligence.svg?b=v1)](https://scrutinizer-ci.com/code-intelligence)

# Instant Analytics plugin for Craft CMS 3.x
# Instant Analytics plugin for Craft CMS 4.x

Instant Analytics brings full Google Analytics support to your Twig templates and automatic Craft Commerce integration with Google Enhanced Ecommerce.

Expand All @@ -12,7 +12,7 @@ Related: [Instant Analytics for Craft 2.x](https://github.com/nystudio107/instan

## Requirements

This plugin requires Craft CMS 3.0.0 or later. Commerce 2 is required for Google Analytics Enhanced eCommerce support.
This plugin requires Craft CMS 4.0.0 or later. Commerce 4 is required for Google Analytics Enhanced eCommerce support.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -2,7 +2,7 @@
"name": "nystudio107/craft-instantanalytics",
"description": "Instant Analytics brings full Google Analytics support to your Twig templates and automatic Craft Commerce integration with Google Enhanced Ecommerce",
"type": "craft-plugin",
"version": "4.0.0-beta.1",
"version": "4.0.0-beta.2",
"keywords": [
"craft",
"cms",
Expand Down
63 changes: 32 additions & 31 deletions src/InstantAnalytics.php
Expand Up @@ -53,8 +53,14 @@ class InstantAnalytics extends Plugin
{
// Constants
// =========================================================================

/**
* @var string
*/
protected const COMMERCE_PLUGIN_HANDLE = 'commerce';

/**
* @var string
*/
protected const SEOMATIC_PLUGIN_HANDLE = 'seomatic';

// Static Properties
Expand All @@ -63,22 +69,22 @@ class InstantAnalytics extends Plugin
/**
* @var null|InstantAnalytics
*/
public static ?InstantAnalytics $plugin;
public static ?InstantAnalytics $plugin = null;

/**
* @var null|Settings
*/
public static ?Settings $settings;
public static ?Settings $settings = null;

/**
* @var null|Commerce
*/
public static ?Commerce $commercePlugin;
public static ?Commerce $commercePlugin = null;

/**
* @var null|Seomatic
*/
public static ?Seomatic $seomaticPlugin;
public static ?Seomatic $seomaticPlugin = null;

/**
* @var string
Expand All @@ -102,6 +108,7 @@ class InstantAnalytics extends Plugin
* @var bool
*/
public bool $hasCpSection = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -165,11 +172,11 @@ public function init(): void
/**
* @inheritdoc
*/
public function settingsHtml(): ?string
protected function settingsHtml(): ?string
{
$commerceFields = [];

if (self::$commercePlugin) {
if (self::$commercePlugin !== null) {
$productTypes = self::$commercePlugin->getProductTypes()->getAllProductTypes();

foreach ($productTypes as $productType) {
Expand All @@ -193,8 +200,8 @@ public function settingsHtml(): ?string
'commerceFields' => $commerceFields,
]
);
} catch (Exception $e) {
Craft::error($e->getMessage(), __METHOD__);
} catch (Exception $exception) {
Craft::error($exception->getMessage(), __METHOD__);
}

return '';
Expand All @@ -203,9 +210,7 @@ public function settingsHtml(): ?string
/**
* Handle the `{% hook iaSendPageView %}`
*
* @param array &$context
*
* @return string
*/
public function iaSendPageView(/** @noinspection PhpUnusedParameterInspection */ array &$context): string
{
Expand All @@ -226,12 +231,12 @@ protected function addComponents(): void
// Add in our Twig extensions
$view->registerTwigExtension(new InstantAnalyticsTwigExtension());
// Install our template hook
$view->hook('iaSendPageView', [$this, 'iaSendPageView']);
$view->hook('iaSendPageView', fn(array $context): string => $this->iaSendPageView($context));
// Register our variables
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $event) {
function (Event $event): void {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('instantAnalytics', [
Expand All @@ -251,7 +256,7 @@ protected function installEventListeners(): void
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
function (PluginEvent $event) {
function (PluginEvent $event): void {
if ($event->plugin === $this) {
$request = Craft::$app->getRequest();
if ($request->isCpRequest) {
Expand All @@ -265,6 +270,7 @@ function (PluginEvent $event) {
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
$this->installSiteEventListeners();
}

// Install only for non-console Control Panel requests
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
$this->installCpEventListeners();
Expand All @@ -280,7 +286,7 @@ protected function installSiteEventListeners(): void
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
function (RegisterUrlRulesEvent $event) {
function (RegisterUrlRulesEvent $event): void {
Craft::debug(
'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
__METHOD__
Expand All @@ -296,30 +302,30 @@ function (RegisterUrlRulesEvent $event) {
Event::on(
View::class,
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
static function (TemplateEvent $event) {
static function (TemplateEvent $event): void {
self::$currentTemplate = $event->template;
}
);
// Remember the name of the currently rendering template
Event::on(
View::class,
View::EVENT_AFTER_RENDER_PAGE_TEMPLATE,
function (TemplateEvent $event) {
function (TemplateEvent $event): void {
if (self::$settings->autoSendPageView) {
$this->sendPageView();
}
}
);
// Commerce-specific hooks
if (self::$commercePlugin) {
Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function (Event $e) {
if (self::$commercePlugin !== null) {
Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function (Event $e): void {
$order = $e->sender;
if (self::$settings->autoSendPurchaseComplete) {
$this->commerce->orderComplete($order);
}
});

Event::on(Order::class, Order::EVENT_AFTER_ADD_LINE_ITEM, function (LineItemEvent $e) {
Event::on(Order::class, Order::EVENT_AFTER_ADD_LINE_ITEM, function (LineItemEvent $e): void {
$lineItem = $e->lineItem;
if (self::$settings->autoSendAddToCart) {
$this->commerce->addToCart($lineItem->order, $lineItem);
Expand All @@ -328,7 +334,7 @@ function (TemplateEvent $event) {

// Check to make sure Order::EVENT_AFTER_REMOVE_LINE_ITEM is defined
if (defined(Order::class . '::EVENT_AFTER_REMOVE_LINE_ITEM')) {
Event::on(Order::class, Order::EVENT_AFTER_REMOVE_LINE_ITEM, function (LineItemEvent $e) {
Event::on(Order::class, Order::EVENT_AFTER_REMOVE_LINE_ITEM, function (LineItemEvent $e): void {
$lineItem = $e->lineItem;
if (self::$settings->autoSendRemoveFromCart) {
$this->commerce->removeFromCart($lineItem->order, $lineItem);
Expand All @@ -348,7 +354,7 @@ protected function installCpEventListeners(): void
/**
* Return the custom frontend routes
*
* @return array
* @return array<string, string>
*/
protected function customFrontendRoutes(): array
{
Expand Down Expand Up @@ -379,12 +385,8 @@ private function sendPageView(): void
$request = Craft::$app->getRequest();
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest() && !self::$pageViewSent) {
self::$pageViewSent = true;
/** @var IAnalytics $analytics */
$analytics = self::$plugin->ia->getGlobals(self::$currentTemplate);
// Bail if we have no analytics object
if ($analytics === null) {
return;
}

// If SEOmatic is installed, set the page title from it
$this->setTitleFromSeomatic($analytics);
// Send the page view
Expand Down Expand Up @@ -414,14 +416,12 @@ private function sendPageView(): void

/**
* If SEOmatic is installed, set the page title from it
*
* @param IAnalytics $analytics
*/
private function setTitleFromSeomatic(IAnalytics $analytics): void
{
if (self::$seomaticPlugin && Seomatic::$settings->renderEnabled) {
$titleTag = Seomatic::$plugin->title->get('title');
if ($titleTag) {
if ($titleTag !== null) {
$titleArray = $titleTag->renderAttributes();
if (!empty($titleArray['title'])) {
$analytics->setDocumentTitle($titleArray['title']);
Expand All @@ -433,14 +433,15 @@ private function setTitleFromSeomatic(IAnalytics $analytics): void
/**
* @param $layoutId
*
* @return array
* @return mixed[]|array<string, string>
*/
private function getPullFieldsFromLayoutId($layoutId): array
{
$result = ['' => 'none'];
if ($layoutId === null) {
return $result;
}

$fieldLayout = Craft::$app->getFields()->getLayoutById($layoutId);
if ($fieldLayout) {
$result = FieldHelper::fieldsOfTypeFromLayout(FieldHelper::TEXT_FIELD_CLASS_KEY, $fieldLayout, false);
Expand Down
6 changes: 2 additions & 4 deletions src/controllers/ManifestController.php
Expand Up @@ -29,11 +29,9 @@ class ManifestController extends Controller
// =========================================================================

/**
* @var bool|array Allows anonymous access to this controller's actions.
* The actions must be in 'kebab-case'
* @access protected
* @inerhitdoc
*/
protected $allowAnonymous = [
protected array|bool|int $allowAnonymous = [
'resource'
];

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/TrackController.php
Expand Up @@ -31,7 +31,7 @@ class TrackController extends Controller
* The actions must be in 'kebab-case'
* @access protected
*/
protected $allowAnonymous = [
protected array|bool|int $allowAnonymous = [
'track-page-view-url',
'track-event-url'
];
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/Field.php
Expand Up @@ -79,7 +79,7 @@ public static function fieldsOfTypeFromLayout(
$foundFields = [];
if (!empty(self::FIELD_CLASSES[$fieldClassKey])) {
$fieldClasses = self::FIELD_CLASSES[$fieldClassKey];
$fields = $layout->getFields();
$fields = $layout->getCustomFields();
/** @var $field BaseField */
foreach ($fields as $field) {
/** @var array $fieldClasses */
Expand Down

0 comments on commit 854a9e7

Please sign in to comment.