简体   繁体   中英

Google Analytics and Yii2 pages requiring login

I am designing a website using Yii2 framework and want to include a Google Analytics tracking code. I guess the best place to include it such that it gets generated in all the is the main.php layout file.

My concern is what will happen if it gets generated in a page that requires a user to log in and may contain data confidential to them. Does it matter, since it is not search indexing, or is there any other check for authorized pages that I should do before generating the script? How?

Regards,

Shahid

If that is the case you just need to check it in your layout may be main.php in your case if user is authenticated or not using this. if you are setting username for login

$privateUrl = ['user/create','user/view'];
$currentUrl = Yii::$app->controller->id . '/' . Yii::$app->controller->action->id;
if (in_array($currentUrl, $privateUrl)) {
    if (Yii::$app->user->identity->username) {
    // GA for authenticated users only
    }
} else {
    // GA for guest
}

We can have one or more controllers to process and show all protected pages and have the following code in main.php.

<?php 
    $gaBlackList = ['protected'];
    if (!in_array(Yii::$app->controller->id, $gaBlackList)) {
        include_once('/config/tracking.php');
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM