<commit_before><?php
/*
 * This file is part of the CampaignChain package.
 *
 * (c) CampaignChain Inc. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CampaignChain\Channel\GoogleAnalyticsBundle\Controller;

use CampaignChain\CoreBundle\Entity\Channel;
use CampaignChain\CoreBundle\Entity\Location;
use CampaignChain\Location\GoogleAnalyticsBundle\Entity\Profile;
use CampaignChain\Security\Authentication\Client\OAuthBundle\Entity\Token;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use CampaignChain\CoreBundle\Util\ParserUtil;

class GoogleAnalyticsController extends Controller
{
    const RESOURCE_OWNER = 'Google';

    private $applicationInfo = array(
        'key_labels' => array('id', 'App Key'),
        'secret_labels' => array('secret', 'App Secret'),
        'config_url' => 'https://code.google.com',
        'parameters' => array(
            "approval_prompt" => 'force',
            "access_type" => "offline",
            "scope" => 'https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/analytics.edit https://www.googleapis.com/auth/analytics https://www.googleapis.com/auth/userinfo.profile'
        ),
    );


    public function createAction()
    {
        $oauthApp = $this->get('campaignchain.security.authentication.client.oauth.application');

        $application = $oauthApp->getApplication(self::RESOURCE_OWNER);

        if(!$application){
            return $oauthApp->newApplicationTpl(self::RESOURCE_OWNER, $this->applicationInfo);
        }
        else {
            return $this->render(
                'CampaignChainChannelGoogleAnalyticsBundle::index.html.twig',
                array(
                    'page_title' => 'Connect with Google Analytics',
                    'app_id' => $application->getKey(),
                )
            );
        }
    }
    public function loginAction(Request $request){
        $oauth = $this->get('campaignchain.security.authentication.client.oauth.authentication');
        $status = $oauth->authenticate(self::RESOURCE_OWNER, $this->applicationInfo);
        $profile = $oauth->getProfile();
        if($status){
            try {
                $request->getSession()->set('token', $oauth->getToken());
            } catch (\Exception $e) {
                throw $e;
            }
        }

        return $this->render(
            'CampaignChainChannelTwitterBundle:Create:login.html.twig',
            array(
                'redirect' => $this->generateUrl('campaignchain_channel_google_analytics_list_properties')
            )
        );
    }

    public function listPropertiesAction(Request $request)
    {
        $token = $request->getSession()->get('token');
        $analyticsClient = $this->get('campaignchain_report_google_analytics.service_client')->getService($token);

        $allProfiles = [];
        foreach ($analyticsClient->management_accounts->listManagementAccounts() as $account) {
            $profiles = $analyticsClient->management_profiles
                ->listManagementProfiles($account->getId(), '~all');
            foreach ($profiles as $profile) {
                $allProfiles[] = $profile;
            }
        }
        $profileIds = $this->getDoctrine()->getRepository('CampaignChainLocationGoogleAnalyticsBundle:Profile')->getGoogleIds();


        return $this->render(
            '@CampaignChainChannelGoogleAnalytics/list_properties.html.twig',
            array(
                'page_title' => 'Connect with Google Analytics',
                'profiles' => $allProfiles,
                'dbProfileIds' => $profileIds,
            )
        );
    }

    public function createLocationAction(Request $request)
    {

        /** @var Token $token */
        $token = $request->getSession()->get('token');

        $websiteChannelModule = $this->getDoctrine()->getRepository('CampaignChainCoreBundle:ChannelModule')->findOneBy([
            'identifier' => 'campaignchain-website'
        ]);

        $googleAnalyticsChannelModule = $this->getDoctrine()->getRepository('CampaignChainCoreBundle:ChannelModule')->findOneBy([
            'identifier' => 'campaignchain-google-analytics'
        ]);

        foreach ($request->get('google-analytics-property-id') as $analyticsId) {
            list($accountId, $profileId) = explode('|', $analyticsId);
            $analyticsClient = $this->get('campaignchain_report_google_analytics.service_client')->getService($token);
            $profile = $analyticsClient->management_webproperties->get($accountId, $profileId);

            $wizard = $this->get('campaignchain.core.channel.wizard');
            $wizard->start(new Channel(), $googleAnalyticsChannelModule);
            $wizard->setName($profile->getName());
            // Get the location module.
            $locationService = $this->get('campaignchain.core.location');
            $locationModule = $locationService->getLocationModule('campaignchain/location-google-analytics', 'campaignchain-google-analytics');

            $oauthApp = $this->get('campaignchain.security.authentication.client.oauth.application');

            $application = $oauthApp
                ->getApplication(self::RESOURCE_OWNER);

            $em = $this->getDoctrine()->getManager();

            $location = new Location();
            $location->setIdentifier($profile->getId());
            $location->setName($profile->getName());
            $location->setLocationModule($locationModule);
            $google_base_url = 'https://www.google.com/analytics/web/?authuser=1#report/visitors-overview/';
            $location->setUrl($google_base_url . 'a' . $profile->accountId . 'w' . $profile->internalWebPropertyId . 'p' . $profile->defaultProfileId);
            $em->persist($location);<commit_msg>Changed Google Analytics URL Pattern<commit_after><?php
/*
 * This file is part of the CampaignChain package.
 *
 * (c) CampaignChain Inc. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CampaignChain\Channel\GoogleAnalyticsBundle\Controller;

use CampaignChain\CoreBundle\Entity\Channel;
use CampaignChain\CoreBundle\Entity\Location;
use CampaignChain\Location\GoogleAnalyticsBundle\Entity\Profile;
use CampaignChain\Security\Authentication\Client\OAuthBundle\Entity\Token;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use CampaignChain\CoreBundle\Util\ParserUtil;

class GoogleAnalyticsController extends Controller
{
    const RESOURCE_OWNER = 'Google';

    private $applicationInfo = array(
        'key_labels' => array('id', 'App Key'),
        'secret_labels' => array('secret', 'App Secret'),
        'config_url' => 'https://code.google.com',
        'parameters' => array(
            "approval_prompt" => 'force',
            "access_type" => "offline",
            "scope" => 'https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/analytics.edit https://www.googleapis.com/auth/analytics https://www.googleapis.com/auth/userinfo.profile'
        ),
    );


    public function createAction()
    {
        $oauthApp = $this->get('campaignchain.security.authentication.client.oauth.application');

        $application = $oauthApp->getApplication(self::RESOURCE_OWNER);

        if(!$application){
            return $oauthApp->newApplicationTpl(self::RESOURCE_OWNER, $this->applicationInfo);
        }
        else {
            return $this->render(
                'CampaignChainChannelGoogleAnalyticsBundle::index.html.twig',
                array(
                    'page_title' => 'Connect with Google Analytics',
                    'app_id' => $application->getKey(),
                )
            );
        }
    }
    public function loginAction(Request $request){
        $oauth = $this->get('campaignchain.security.authentication.client.oauth.authentication');
        $status = $oauth->authenticate(self::RESOURCE_OWNER, $this->applicationInfo);
        $profile = $oauth->getProfile();
        if($status){
            try {
                $request->getSession()->set('token', $oauth->getToken());
            } catch (\Exception $e) {
                throw $e;
            }
        }

        return $this->render(
            'CampaignChainChannelTwitterBundle:Create:login.html.twig',
            array(
                'redirect' => $this->generateUrl('campaignchain_channel_google_analytics_list_properties')
            )
        );
    }

    public function listPropertiesAction(Request $request)
    {
        $token = $request->getSession()->get('token');
        $analyticsClient = $this->get('campaignchain_report_google_analytics.service_client')->getService($token);

        $allProfiles = [];
        foreach ($analyticsClient->management_accounts->listManagementAccounts() as $account) {
            $profiles = $analyticsClient->management_profiles
                ->listManagementProfiles($account->getId(), '~all');
            foreach ($profiles as $profile) {
                $allProfiles[] = $profile;
            }
        }
        $profileIds = $this->getDoctrine()->getRepository('CampaignChainLocationGoogleAnalyticsBundle:Profile')->getGoogleIds();


        return $this->render(
            '@CampaignChainChannelGoogleAnalytics/list_properties.html.twig',
            array(
                'page_title' => 'Connect with Google Analytics',
                'profiles' => $allProfiles,
                'dbProfileIds' => $profileIds,
            )
        );
    }

    public function createLocationAction(Request $request)
    {

        /** @var Token $token */
        $token = $request->getSession()->get('token');

        $websiteChannelModule = $this->getDoctrine()->getRepository('CampaignChainCoreBundle:ChannelModule')->findOneBy([
            'identifier' => 'campaignchain-website'
        ]);

        $googleAnalyticsChannelModule = $this->getDoctrine()->getRepository('CampaignChainCoreBundle:ChannelModule')->findOneBy([
            'identifier' => 'campaignchain-google-analytics'
        ]);

        foreach ($request->get('google-analytics-property-id') as $analyticsId) {
            list($accountId, $profileId) = explode('|', $analyticsId);
            $analyticsClient = $this->get('campaignchain_report_google_analytics.service_client')->getService($token);
            $profile = $analyticsClient->management_webproperties->get($accountId, $profileId);

            $wizard = $this->get('campaignchain.core.channel.wizard');
            $wizard->start(new Channel(), $googleAnalyticsChannelModule);
            $wizard->setName($profile->getName());
            // Get the location module.
            $locationService = $this->get('campaignchain.core.location');
            $locationModule = $locationService->getLocationModule('campaignchain/location-google-analytics', 'campaignchain-google-analytics');

            $oauthApp = $this->get('campaignchain.security.authentication.client.oauth.application');

            $application = $oauthApp
                ->getApplication(self::RESOURCE_OWNER);

            $em = $this->getDoctrine()->getManager();

            $location = new Location();
            $location->setIdentifier($profile->getId());
            $location->setName($profile->getName());
            $location->setLocationModule($locationModule);
            $google_base_url = 'https://www.google.com/analytics/web/#report/visitors-overview/';
            $location->setUrl($google_base_url . 'a' . $profile->accountId . 'w' . $profile->internalWebPropertyId . 'p' . $profile->defaultProfileId);
            $em->persist($location);
{'fraction_non_alphanumeric': 0.13704138046573966, 'fraction_numerical': 8.376612497905847e-05, 'mean_word_length': 2.815596037072547, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 159, 'https://': 12, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5203400', 'n_tokens_mistral': 3216, 'n_tokens_neox': 3097, 'n_words': 475}