public function createLocationAction(Request $request) { $selectedIds = $request->get('google-analytics-profile-id', []); if (empty($selectedIds)) { $this->addFlash('warning', 'Please select at least one Google Analytics property'); return $this->redirectToRoute('campaignchain_channel_google_analytics_list_properties'); } /** @var Token $token */ $token = $request->getSession()->get('token'); $em = $this->getDoctrine()->getManager(); $token = $em->merge($token); $websiteChannelModule = $em->getRepository('CampaignChainCoreBundle:ChannelModule')->findOneBy(['identifier' => 'campaignchain-website']); $googleAnalyticsChannelModule = $em->getRepository('CampaignChainCoreBundle:ChannelModule')->findOneBy(['identifier' => 'campaignchain-google-analytics']); foreach ($selectedIds as $analyticsId) { list($accountId, $propertyId, $profileId) = explode('|', $analyticsId); $analyticsClient = $this->get('campaignchain_report_google_analytics.service_client')->getService($token); /** @var \Google_Service_Analytics_Profile $profile */ $profile = $analyticsClient->management_profiles->get($accountId, $propertyId, $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'); $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->getAccountId() . 'w' . $profile->getInternalWebPropertyId() . 'p' . $profile->getId()); $em->persist($location); try { $em->flush(); } catch (UniqueConstraintViolationException $e) { //This GA endpoint is already connected $this->addFlash('warning', 'The Google Analytics Property <a href="#">' . $profile->getName() . '</a> is already connected.'); continue; } $wizard->addLocation($location->getIdentifier(), $location); $wizard->persist(); $wizard->end(); //Check if the if the belonging website location exists, if not create a new website location $website = $em->getRepository('CampaignChainCoreBundle:Location')->findOneByUrl($profile->getWebsiteUrl()); //Create website location that belongs to the GA location if (!$website) { $websiteLocationModule = $locationService->getLocationModule('campaignchain/location-website', 'campaignchain-website'); $websiteLocationName = ParserUtil::getHTMLTitle($profile->getWebsiteUrl()); $websiteLocation = new Location(); $websiteLocation->setLocationModule($websiteLocationModule); $websiteLocation->setName($websiteLocationName); $websiteLocation->setUrl($profile->getWebsiteUrl()); $em->persist($websiteLocation); $wizard->start(new Channel(), $websiteChannelModule); $wizard->setName($websiteLocationName); $wizard->addLocation($profile->getWebsiteUrl(), $websiteLocation); $wizard->persist(); $website = $websiteLocation; } $entityToken = clone $token; $entityToken->setLocation($location); $entityToken->setApplication($token->getApplication()); $em->persist($entityToken); $analyticsProfile = new Profile(); $analyticsProfile->setAccountId($profile->getAccountId()); $analyticsProfile->setPropertyId($profile->getWebPropertyId()); $analyticsProfile->setProfileId($profile->getId()); $analyticsProfile->setIdentifier($profile->getId()); $analyticsProfile->setDisplayName($profile->getName()); $analyticsProfile->setLocation($location); $analyticsProfile->setBelongingLocation($website); $em->persist($analyticsProfile); $em->flush(); $this->addFlash('success', 'The Google Analytics Property <a href="#">' . $profile->getName() . '</a> was connected successfully.'); } return $this->redirectToRoute('campaignchain_core_location'); }
public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('metrics', 'choice', array('label' => 'Metrics', 'choices' => Profile::getMetricsArray(), 'expanded' => true, 'multiple' => true, 'constraints' => array(new Assert\NotBlank(array('message' => 'Please choose at least one metric'))))); $builder->add('segment', 'choice', array('label' => 'Segments', 'choices' => Profile::getSegmentsArray(), 'expanded' => true, 'multiple' => false)); }