Krajee

Google Analytics Widget

Thankful to Krajee! BUY A COFFEEor to get more out of us.
This widget allows you to embed GoogleAnalytics plugin into your website.

Tip

Not seeing the updated content on this page! Hard refresh your browser to clean cache for this page (e.g. SHIFT-F5 on Windows Chrome)

The GoogleAnalytics widget supports these parameters.
  • id: string the Google Analytics Tracking ID

  • domain: string the domain name of your website where the tracking code will be displayed

  • objectName: string the global object name. Defaults to __gaTracker

  • testMode: boolean whether to enable test mode to automatically set `cookieDomain` to `none` for localhost. The test mode will auto default to YII_DEBUG definition.

  • anonymizeIp: int the settings to anonymize the IP address of the hit (http request) sent to Google Analytics. One of the following values are supported:

    • 0 or GoogleAnalytics::HIT_NONE: The IP will not be anonymized.

    • 1 or GoogleAnalytics::HIT_ALL: Anonymize the IP addresses for all the hits sent from a page (the lifetime of the tracker object).

    • 2 or GoogleAnalytics::HIT_EACH: Anonymize the IP addresses of an individual hit.

  • trackerConfig: array|string the tracker object configuration. Set it as a string (to be used as is) or an associative array in $key => $value format. Check the GoogleAnalytics tracker object create documentation for further details.

  • sendConfig: array|string the configuration for sending data. Set it as a string (to be used as is) or an associative array in $key => $value format. Check the GoogleAnalytics tracker object create documentation for further details.

  • jsBeforeSend: string any additional javascript to embed before the script that sends data.

  • jsAfterSend: string any additional javascript to embed after the script that sends data.

  • noscript: string|boolean text to be displayed if browser does not support javascript. If set to false will not displayed. Defaults to Please enable JavaScript to view the comments powered by google-analytics.

  • noscriptOptions: array the HTML attributes for the noscript message container. Defaults to ['class' => 'alert alert-danger']

NOTE You can configure the GoogleAnalytics settings once at the social module configuration level, for defaulting it across your application, and skip it in your widget calls. For example.
// your module config
'modules' => [
    'social' => [
        // the module class
        'class' => 'kartik\social\Module',

        // the global settings for the google-analytics widget
        'google-analytics' => [
            'id' => 'TRACKING_ID',
            'domain' => 'TRACKING_DOMAIN'
        ],
]

// your call in your view/layout file
echo GoogleAnalytics::widget([]); // will use the google-analytics settings configured in module
You can setup the GoogleAnalytics widget in your view layout file or your view like below:
use kartik\social\GoogleAnalytics;

// If any parameters are not passed, the widget will use settings 
// from the social module wherever possible
echo GoogleAnalytics::widget();

// Basic widget usage
echo GoogleAnalytics::widget([
    'id' => 'TRACKING_ID', 
    'domain' => 'TRACKING_DOMAIN',
]);

// Advanced widget usage
echo GoogleAnalytics::widget([
    'id' => 'TRACKING_ID', 
    'domain' => 'TRACKING_DOMAIN',
    'noscript' => 'Analytics cannot be run on this browser since Javascript is not enabled.'
]);

yii2-social is released under the BSD-3-Clause License. See the bundled LICENSE.md for details.

Note

You can now visit the Krajee Webtips Q & A forum for searching OR asking questions OR helping programmers with answers on these extensions and plugins. For asking a question click here. Select the appropriate question category (i.e. Krajee Plugins) and choose this current page plugin in the question related to field.

The comments and discussion section below are intended for generic discussions or feedback for this plugin. Developers may not be able to search or lookup here specific questions or tips on usage for this plugin.

 
visitors to Krajee Yii2 Demos since 22-May-2017