示例#1
0
        /// <summary>
        /// Uploads a creative asset and associates it with the specified advertiser.
        /// </summary>
        /// <param name="assetFile">The path to the asset file to be uploaded</param>
        /// <param name="assetType">The CreativeAssetId type of the asset</param>
        /// <returns>
        /// A CreativeAssetMetadata object populated with the name of the asset after insert.
        /// </returns>
        public CreativeAssetMetadata uploadAsset(String assetFile, String assetType)
        {
            // Prepare an input stream.
            FileStream assetContent = new FileStream(assetFile, FileMode.Open, FileAccess.Read);

            // Create the creative asset ID and Metadata.
            CreativeAssetId assetId = new CreativeAssetId();

            assetId.Name = Path.GetFileName(assetFile);
            assetId.Type = assetType;

            CreativeAssetMetadata metaData = new CreativeAssetMetadata();

            metaData.AssetIdentifier = assetId;

            // Insert the creative.
            String mimeType = determineMimeType(assetFile, assetType);

            CreativeAssetsResource.InsertMediaUpload request =
                Service.CreativeAssets.Insert(metaData, ProfileId, AdvertiserId, assetContent, mimeType);

            IUploadProgress progress = request.Upload();

            if (UploadStatus.Failed.Equals(progress.Status))
            {
                throw progress.Exception;
            }

            // Display the new asset name.
            Console.WriteLine("Creative asset was saved with name \"{0}\".",
                              request.ResponseBody.AssetIdentifier.Name);
            return(request.ResponseBody);
        }
        /// <summary>
        /// Uploads a creative asset and associates it with the specified advertiser.
        /// </summary>
        /// <param name="assetFile">The path to the asset file to be uploaded</param>
        /// <param name="assetType">The CreativeAssetId type of the asset</param>
        /// <returns>
        /// A CreativeAssetId object populated with the name of the asset after insert.
        /// </returns>
        public CreativeAssetId uploadAsset(String assetFile, String assetType)
        {
            // Prepare an input stream.
              FileStream assetContent = new FileStream(assetFile, FileMode.Open, FileAccess.Read);

              // Create the creative asset ID and Metadata.
              CreativeAssetId assetId = new CreativeAssetId();
              assetId.Name = Path.GetFileName(assetFile);
              assetId.Type = assetType;

              CreativeAssetMetadata metaData = new CreativeAssetMetadata();
              metaData.AssetIdentifier = assetId;

              // Insert the creative.
              String mimeType = determineMimeType(assetFile, assetType);
              CreativeAssetsResource.InsertMediaUpload request =
              Service.CreativeAssets.Insert(metaData, ProfileId, AdvertiserId, assetContent, mimeType);
              request.Upload();

              // Display the new asset name.
              Console.WriteLine("Creative asset was saved with name \"{0}\".",
            request.ResponseBody.AssetIdentifier.Name);

              return request.ResponseBody.AssetIdentifier;
        }
示例#3
0
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="service">An initialized Dfa Reporting service object
        /// </param>
        public override void Run(DfareportingService service)
        {
            long instreamVideoCreativeId = long.Parse(_T("INSERT_INSTREAM_VIDEO_CREATIVE_ID_HERE"));
            long targetingTemplateId     = long.Parse(_T("INSERT_TARGETING_TEMPLATE_ID_HERE"));
            long profileId = long.Parse(_T("INSERT_USER_PROFILE_ID_HERE"));

            string videoAssetName       = _T("INSERT_VIDEO_ASSET_NAME_HERE");
            string pathToVideoAssetFile = _T("INSERT_PATH_TO_VIDEO_ASSET_FILE_HERE");

            // Retrieve the specified creative.
            Creative creative = service.Creatives.Get(profileId, instreamVideoCreativeId).Execute();

            if (creative == null || !"INSTREAM_VIDEO".Equals(creative.Type))
            {
                Console.Error.WriteLine("Invalid creative specified.");
                return;
            }

            CreativeAssetSelection selection = creative.CreativeAssetSelection;

            if (!creative.DynamicAssetSelection.Value)
            {
                // Locate an existing video asset to use as a default.
                // This example uses the first PARENT_VIDEO asset found.
                CreativeAsset defaultAsset =
                    creative.CreativeAssets.First(asset => "PARENT_VIDEO".Equals(asset.Role));
                if (defaultAsset == null)
                {
                    Console.Error.WriteLine("Default video asset could not be found.");
                    return;
                }

                // Create a new selection using the existing asset as a default.
                selection = new CreativeAssetSelection();
                selection.DefaultAssetId = defaultAsset.Id;
                selection.Rules          = new List <Rule>();

                // Enable dynamic asset selection for the creative.
                creative.DynamicAssetSelection  = true;
                creative.CreativeAssetSelection = selection;
            }

            // Upload the new video asset and add it to the creative.
            CreativeAssetUtils assetUtils = new CreativeAssetUtils(service, profileId,
                                                                   creative.AdvertiserId.Value);
            CreativeAssetMetadata videoMetadata = assetUtils.uploadAsset(pathToVideoAssetFile, "VIDEO");

            creative.CreativeAssets.Add(new CreativeAsset()
            {
                AssetIdentifier = videoMetadata.AssetIdentifier,
                Role            = "PARENT_VIDEO"
            });

            // Create a rule targeting the new video asset and add it to the selection.
            Rule rule = new Rule();

            rule.AssetId             = videoMetadata.Id;
            rule.Name                = "Test rule for asset " + videoMetadata.Id;
            rule.TargetingTemplateId = targetingTemplateId;
            selection.Rules.Add(rule);

            // Update the creative.
            Creative result = service.Creatives.Update(creative, profileId).Execute();

            Console.WriteLine("Dynamic asset selection enabled for creative with ID {0}.", result.Id);
        }
        /// <summary>
        /// Inserts a new creative asset.
        /// Documentation https://developers.google.com/dfareporting/v2.7/reference/creativeAssets/insert
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Dfareporting service.</param>
        /// <param name="profileId">User profile ID associated with this request.</param>
        /// <param name="advertiserId">Advertiser ID of this creative. This is a required field.</param>
        /// <param name="body">A valid Dfareporting v2.7 body.</param>
        /// <returns>CreativeAssetMetadataResponse</returns>
        public static CreativeAssetMetadata Insert(DfareportingService service, string profileId, string advertiserId, CreativeAssetMetadata body)
        {
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }
                if (body == null)
                {
                    throw new ArgumentNullException("body");
                }
                if (profileId == null)
                {
                    throw new ArgumentNullException(profileId);
                }
                if (advertiserId == null)
                {
                    throw new ArgumentNullException(advertiserId);
                }

                // Make the request.
                return(service.CreativeAssets.Insert(body, profileId, advertiserId).Execute());
            }
            catch (Exception ex)
            {
                throw new Exception("Request CreativeAssets.Insert failed.", ex);
            }
        }