Craft\InstantAnalyticsPlugin::init PHP Method

init() public method

public init ( ) : mixed
return mixed
    public function init()
    {
        require_once __DIR__ . '/vendor/autoload.php';
        Craft::import('plugins.instantanalytics.src.IAnalytics');
        /* -- This is the hook that triggers a PageView to be sent */
        craft()->templates->hook('iaSendPageView', function (&$context) {
            if (craft()->request->isSiteRequest() && !craft()->isConsole()) {
                if (isset($context['instantAnalytics'])) {
                    /* -- Get the Analytics object from the Twig context */
                    $analytics = $context['instantAnalytics'];
                    /* -- If SEOmatic is installed, set the page title from it */
                    $seomatic = craft()->plugins->getPlugin('Seomatic');
                    if ($seomatic && $seomatic->isInstalled && $seomatic->isEnabled && isset($context['seomaticMeta'])) {
                        $seomaticMeta = $context['seomaticMeta'];
                        $analytics->setDocumentTitle($seomaticMeta['seoTitle']);
                    }
                    /* -- Send the page view */
                    $analytics->sendPageView();
                }
            }
            return "";
        });
        /* -- Only install these listeners if Craft Commerce is installed */
        $settings = $this->getSettings();
        $commerce = craft()->plugins->getPlugin('Commerce');
        if ($commerce && $commerce->isInstalled && $commerce->isEnabled) {
            /* -- Listen for completed Craft Commerce orders */
            if ($settings['autoSendPurchaseComplete']) {
                craft()->on('commerce_orders.onOrderComplete', function (Event $e) {
                    $orderModel = null;
                    if (isset($e->params['order'])) {
                        $orderModel = $e->params['order'];
                    }
                    craft()->instantAnalytics->orderComplete($orderModel);
                });
            }
            /* -- Listen for items added to the Craft Commerce cart */
            if ($settings['autoSendAddToCart']) {
                craft()->on('commerce_cart.onAddToCart', function (Event $e) {
                    $orderModel = null;
                    $lineItem = null;
                    if (isset($e->params['cart'])) {
                        $orderModel = $e->params['cart'];
                    }
                    if (isset($e->params['lineItem'])) {
                        $lineItem = $e->params['lineItem'];
                    }
                    craft()->instantAnalytics->addToCart($orderModel, $lineItem);
                });
            }
            /* -- Listen for items removed from the Craft Commerce cart */
            if ($settings['autoSendRemoveFromCart'] && craft()->commerce_cart->hasEvent('onBeforeRemoveFromCart')) {
                craft()->on('commerce_cart.onBeforeRemoveFromCart', function (Event $e) {
                    $orderModel = null;
                    $lineItem = null;
                    if (isset($e->params['cart'])) {
                        $orderModel = $e->params['cart'];
                    }
                    if (isset($e->params['lineItem'])) {
                        $lineItem = $e->params['lineItem'];
                    }
                    craft()->instantAnalytics->removeFromCart($orderModel, $lineItem);
                });
            } else {
                InstantAnalyticsPlugin::log("commerce_cart.onBeforeRemoveFromCart doesn't exist", LogLevel::Info, false);
            }
        }
    }