Пример #1
0
        private VkAdSuccess CreateAd(string accountId, string accessToken, int campaignId, string sex, string ageFrom = "14", string ageUpTo = "80", string status = "0", string groups = "",
                                     string excludedGroupds = "", string interestCategories     = "", string country = "", string cities = "", string excludedCities = "",
                                     string retargetGroups  = "", string excludedRetargetGroups = "")
        {
            var reqString = _vkUrlService.CreateAd(accountId: accountId, campaignId: campaignId, accessToken: accessToken, name: "EvilMarketing_Affinity_Ad",
                                                   sex: sex, ageFrom: Convert.ToInt32(ageFrom), ageUpTo: Convert.ToInt32(ageUpTo), status: status, groups: groups, excludedGroups: excludedGroupds, interestCategories: interestCategories,
                                                   country: country, cities: cities, excludedCities: excludedCities, retargetGroups: retargetGroups, excludedRetargetGroups: excludedRetargetGroups);

            var ad     = _vkBaseService.GetJsonFromResponse(_vkAdsRequestService.RequestJs(reqString));
            var result = JsonConvert.DeserializeObject <List <VkAdSuccess> >(ad).FirstOrDefault();

            return(result);
        }
Пример #2
0
        public IEnumerable <VkAnalyseSalesResultModel> CreateRetargets(List <CohortAnalysisModel> posts, string accountId, string clientId, string excludeTargetGroupdId, string accessToken)
        {
            var results = new List <VkAnalyseSalesResultModel>();

            //Создаём рекламную кампанию
            var campaignJson = VkAdsRequestService.RequestJs(VkUrlService.CreateCampaign(accountId, clientId, string.Format("EM-{0}", accountId), accessToken));
            var campaign     = JsonConvert.DeserializeObject <List <VkCampaignSuccess> >(JObject.Parse(campaignJson)["response"].ToString());

            for (var i = 0; i < posts.Count(); i = i + DefaultPostsCountValue)
            {
                var items = posts.Skip(i).Take(DefaultPostsCountValue).ToList();


                int iterationCount = 0;

                foreach (var item in items)
                {
                    try
                    {
                        int fCount = 0;
                        int uCount = 0;

                        //TODO: добавлять номер поста
                        // Создание группы ретаргета
                        var retargetGroupJson = VkAdsRequestService.RequestJs(VkUrlService.CreateRetargetGroup(accountId, clientId, accessToken, item.PostId));

                        var retargetGroup = JsonConvert.DeserializeObject <VkTargetGroupSuccess>(JObject.Parse(retargetGroupJson)["response"].ToString());
                        if (retargetGroup.Id == 0 || retargetGroup.ErrorCode != 0)
                        {
                            continue;
                        }

                        //Если при создании группы ретаргетинга в ней меньше 100 человек, мы добавляем совершенно левых, чтобы удовлетворять требуемым условиям
                        //if (item.LikedIds.Count() < 100)
                        //{
                        //    var needToAdd = 300;
                        //    item.LikedIds.AddRange(GetRandomUsers(300, accessToken));
                        //}

                        item.LikedIds.AddRange(GetRandomUsers(300, accessToken));


                        var contacts = item.LikedIds.Aggregate(string.Empty, (current, id) => current + string.Format("{0},", id));

                        //Добавляем пользователей в группу ретаргета
                        var updatedGroup = VkAdsRequestService.RequestJs(VkUrlService.ImportRetargetContacts(accountId, clientId, retargetGroup.Id.ToString(), contacts, accessToken));

                        //Создаём рекламное объявление
                        var adsJson = VkAdsRequestService.RequestJs(VkUrlService.CreateAd(accountId, campaign.FirstOrDefault().Id.ToString(), retargetGroup.Id.ToString(), string.Format("EM_Ad_{0}", item.PostId), accessToken));
                        var newAds  = JsonConvert.DeserializeObject <List <VkAdSuccess> >(JObject.Parse(adsJson)["response"].ToString());

                        var newAd = newAds.FirstOrDefault();
                        if (newAd.Id == 0 || newAd.ErrorCode != 0)
                        {
                            continue;
                        }

                        //Получаем охват текущей рекламной кампании
                        var adTargetInfoJson = VkAdsRequestService.RequestJs(VkUrlService.GetAdsTargeting(accountId, clientId, string.Format("[{0}]", newAd.Id), accessToken));
                        var adTargeting      = JsonConvert.DeserializeObject <List <VkAdTargeting> >(JObject.Parse(adTargetInfoJson)["response"].ToString());

                        var adChk = adTargeting.FirstOrDefault();
                        if (adChk.Id == 0 || adChk.ErrorCode != 0)
                        {
                            continue;
                        }
                        fCount = adChk.Count;

                        //Добавляем в исключение выбранную пользователем группу ретаргета
                        var updatedInfoJson = VkAdsRequestService.RequestJs(VkUrlService.UpdateAd(accountId, newAd.Id.ToString(), excludeTargetGroupdId, accessToken));
                        var updatedInfo     = JsonConvert.DeserializeObject <List <VkAdSuccess> >(JObject.Parse(updatedInfoJson)["response"].ToString());

                        var result = new VkAnalyseSalesResultModel
                        {
                            PostId = item.PostId,
                            Date   = item.PostDate
                        };

                        if (updatedInfo.Any(x => x.ErrorCode != 0 || !string.IsNullOrEmpty(x.ErrorDesc)))
                        {
                            //Плохая группа ретаргета и невозможно провести обновление
                            result.Result = "<100";
                        }
                        else
                        {
                            var updatedAdsInfoJson = VkAdsRequestService.RequestJs(VkUrlService.GetAdsTargeting(accountId, clientId, string.Format("[{0}]", updatedInfo.FirstOrDefault().Id), accessToken));
                            var updatedAdsInfo     = JsonConvert.DeserializeObject <List <VkAdTargeting> >(JObject.Parse(updatedAdsInfoJson)["response"].ToString());

                            if (updatedAdsInfo.Any(x => x.ErrorCode != 0 || !string.IsNullOrEmpty(x.ErrorDesc)))
                            {
                                continue;
                            }
                            uCount        = updatedAdsInfo.FirstOrDefault().Count;
                            result.Result = (Math.Abs(Convert.ToInt32(fCount) - Convert.ToInt32(uCount))).ToString();
                        }

                        //var deleteAdsResult = VkAdsRequestService.RequestJs(VkUrlService.DeleteAds(accountId, new[] {chk.Id}, accessToken));
                        //var deleteResult = VkAdsRequestService.RequestJs(VkUrlService.DeleteRetargetGroup(accountId, clientId, retargetGroup.Id.ToString(), accessToken));

                        results.Add(result);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error("Error during foreach in CreateRetargets:");
                        _logger.Error(ex);
                        _logger.Error("Inner Exception:");
                        _logger.Error(ex.InnerException);
                    }

                    ++iterationCount;
                }
            }

            return(results);
        }