Example #1
0
func main() {
	flag.Parse()
	config, err := gads.NewCredentialsFromFile(*configJson)
	if err != nil {
		log.Fatal(err)
	}

	// User List Service
	auls := gads.NewAdwordsUserListService(&config.Auth)

	crmList := gads.NewCrmBasedUserList("Test List", "Just a list to test with", 0, "http://mytest.com/optout")

	ops := gads.UserListOperations{
		Operations: []gads.Operation{
			gads.Operation{
				Operator: "ADD",
				Operand:  crmList,
			},
		},
	}

	resp, err := auls.Mutate(ops)

	if err != nil {
		panic(err)
	}

	fmt.Println(resp[0].Id)

	mmo := gads.NewMutateMembersOperand()
	mmo.UserListId = resp[0].Id

	var members []string
	members = append(members, "*****@*****.**")
	members = append(members, "*****@*****.**")

	mmo.Members = members

	mutateMembersOperations := gads.MutateMembersOperations{
		Operations: []gads.Operation{
			gads.Operation{
				Operator: "ADD",
				Operand:  mmo,
			},
		},
	}

	lists, err := auls.MutateMembers(mutateMembersOperations)

	if err != nil {
		panic(err)
	}

	fmt.Println(lists)
}
Example #2
0
func main() {
	flag.Parse()
	config, err := gads.NewCredentialsFromFile(*configJson)
	if err != nil {
		log.Fatal(err)
	}

	config.Auth.PartialFailure = true

	service := gads.NewAdGroupService(&config.Auth)

	adGroupList := []gads.AdGroupLabel{
		gads.AdGroupLabel{
			AdGroupId: 1234567890,
			LabelId:   1234567,
		},
		gads.AdGroupLabel{
			AdGroupId: 1234567891,
			LabelId:   1234567,
		},
		gads.AdGroupLabel{
			AdGroupId: 1234567892,
			LabelId:   1234567,
		},
	}

	adGroupLabels, err := service.MutateLabel(
		gads.AdGroupLabelOperations{
			"ADD": adGroupList,
		},
	)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(adGroupLabels)

}
Example #3
0
func main() {
	flag.Parse()
	config, err := gads.NewCredentialsFromFile(*configJson)
	if err != nil {
		log.Fatal(err)
	}

	service := gads.NewDataService(&config.Auth)

	// Test GetAdGroupBidLandscape

	adGroupBidLandscape, _, err := service.GetAdGroupBidLandscape(
		gads.Selector{
			Fields: []string{
				"AdGroupId",
				"Bid",
				"CampaignId",
				"LocalClicks",
				"LocalCost",
				"LocalImpressions",
				"PromotedImpressions",
				"StartDate",
				"EndDate",
			},
			Predicates: []gads.Predicate{
				{"AdGroupId", "EQUALS", []string{adGroupId}},
			},
		},
	)

	if err != nil {
		log.Fatal(err)
	}

	for _, bidLandscape := range adGroupBidLandscape {
		landscapeJSON, _ := json.MarshalIndent(bidLandscape, "", "  ")
		fmt.Printf("%s\n", landscapeJSON)
	}

	// END Test GetAdGroupBidLandscape

	// Test QueryAdGroupBidLandscape

	foundBidLandscape, _, err := service.QueryAdGroupBidLandscape(fmt.Sprintf("SELECT AdGroupId, Bid, CampaignId, LocalClicks, LocalCost, LocalImpressions WHERE AdGroupId = '%v'", adGroupId))

	if err != nil {
		log.Fatal(err)
	}

	for _, bidLandscape := range foundBidLandscape {
		landscapeJSON, _ := json.MarshalIndent(bidLandscape, "", "  ")
		fmt.Printf("%s\n", landscapeJSON)
	}

	// END Test QueryAdGroupBidLandscape

	// Test GetCriterionBidLandscape

	criterionBidLandscape, _, err := service.GetCriterionBidLandscape(
		gads.Selector{
			Fields: []string{
				"AdGroupId",
				"Bid",
				"CampaignId",
				"CriterionId",
				"LocalClicks",
				"LocalCost",
				"LocalImpressions",
				"PromotedImpressions",
				"StartDate",
				"EndDate",
			},
			Predicates: []gads.Predicate{
				{"CriterionId", "EQUALS", []string{criterionId}},
				{"AdGroupId", "EQUALS", []string{adGroupId}},
			},
		},
	)

	if err != nil {
		log.Fatal(err)
	}

	for _, bidLandscape := range criterionBidLandscape {
		landscapeJSON, _ := json.MarshalIndent(bidLandscape, "", "  ")
		fmt.Printf("%s\n", landscapeJSON)
	}

	// END Test GetCriterionBidLandscape

	// Test QueryCriterionBidLandscape

	queryCriterionBidLandscape, _, err := service.QueryCriterionBidLandscape(fmt.Sprintf("SELECT AdGroupId, Bid, CampaignId, CriterionId, LocalClicks, LocalCost, LocalImpressions WHERE AdGroupId = '%v' AND CriterionId = '%v'", adGroupId, criterionId))

	if err != nil {
		log.Fatal(err)
	}

	for _, bidLandscape := range queryCriterionBidLandscape {
		landscapeJSON, _ := json.MarshalIndent(bidLandscape, "", "  ")
		fmt.Printf("%s\n", landscapeJSON)
	}

	// END Test QueryCriterionBidLandscape

}
Example #4
0
func main() {
	flag.Parse()
	config, err := gads.NewCredentialsFromFile(*configJson)
	if err != nil {
		log.Fatal(err)
	}
	// Bulk Mutate
	ms := gads.NewMutateJobService(&config.Auth)
	policy := new(gads.BulkMutateJobPolicy)

	// If you need to add prerequisites
	//policy.PrerequisiteJobIds = append(policy.PrerequisiteJobIds, 123456)

	// Creating AdGroups
	/*ago := gads.AdGroupOperations{
		"ADD": {
			gads.AdGroup{
				Name:       "test ad group " + rand_str(10),
				Status:     "PAUSED",
				CampaignId: campaignId,
			},
			gads.AdGroup{
				Name:       "test ad group " + rand_str(10),
				Status:     "PAUSED",
				CampaignId: campaignId,
			},
		},
	}*/

	// Updating AdGroups
	/*ago := gads.AdGroupOperations{
		"SET": {
			gads.AdGroup{
				Id: 1234567890,
				CampaignId: campaignId,
				BiddingStrategyConfiguration: []gads.BiddingStrategyConfiguration{
					gads.BiddingStrategyConfiguration{
						StrategyType: "MANUAL_CPC",
						Bids: []gads.Bid{
							gads.Bid{
								Type:   "CpcBid",
								Amount: 2000000,
							},
						},
					},
				},
			},
			gads.AdGroup{
				Id: 1234567890,
				CampaignId: campaignId,
				BiddingStrategyConfiguration: []gads.BiddingStrategyConfiguration{
					gads.BiddingStrategyConfiguration{
						StrategyType: "MANUAL_CPC",
						Bids: []gads.Bid{
							gads.Bid{
								Type:   "CpcBid",
								Amount: 2000000,
							},
						},
					},
				},
			},
		},
	}*/

	ago := gads.AdGroupCriterionOperations{
		"SET": {
			gads.BiddableAdGroupCriterion{
				AdGroupId: 1234567890,
				Criterion: gads.KeywordCriterion{
					Id: 1234567890,
				},
				BiddingStrategyConfiguration: &gads.BiddingStrategyConfiguration{
					Bids: []gads.Bid{
						gads.Bid{
							Type:   "CpcBid",
							Amount: 3000000,
						},
					},
				},
			},
			gads.BiddableAdGroupCriterion{
				AdGroupId: 1234567890,
				Criterion: gads.KeywordCriterion{
					Id: 1234567890,
				},
				BiddingStrategyConfiguration: &gads.BiddingStrategyConfiguration{
					Bids: []gads.Bid{
						gads.Bid{
							Type:   "CpcBid",
							Amount: 2000000,
						},
					},
				},
			},
		},
	}

	if resp, err := ms.Mutate(ago, policy); err == nil {
		jobId := resp.Id

		// loop
		for {
			// recheck every 5 seconds
			time.Sleep(5 * time.Second)

			jobSelector := gads.BulkMutateJobSelector{JobIds: []int64{jobId}}

			result, err := ms.Get(jobSelector)

			if err != nil {
				panic(err)
			}

			if result.Status == "COMPLETED" {
				break
			}

			if result.Status == "FAILED" {
				// probably do something else here
				panic("Job result failed")
			}
		}

		jobSelector := gads.BulkMutateJobSelector{JobIds: []int64{jobId}}
		result, err := ms.GetResult(jobSelector)

		if err != nil {
			panic(err)
		}
		jsonResult, _ := json.Marshal(result)
		fmt.Println(result)
		fmt.Println(string(jsonResult))

	} else {
		// handle err
		panic(err)
	}

}
Example #5
0
func main() {
	flag.Parse()
	config, err := gads.NewCredentialsFromFile(*configJson)
	if err != nil {
		log.Fatal(err)
	}

	// Batch Job
	bs := gads.NewBatchJobService(&config.Auth)

	/*ago := gads.AdGroupCriterionOperations{
	"SET":	gads.AdGroupCriterions {
				gads.BiddableAdGroupCriterion{
					AdGroupId: 11400713462,
					Criterion: gads.KeywordCriterion{
						Id: 47652524966,
					},
					BiddingStrategyConfiguration: &gads.BiddingStrategyConfiguration{
						Bids: []gads.Bid{
							gads.Bid{
								Type:   "CpcBid",
								Amount: 5372200,
							},
						},
					},
					UrlCustomParameters: gads.CustomParameters{
						CustomParameters: []gads.CustomParameter{
							gads.CustomParameter{
								Key:      "foo",
								Value:    "bar",
								IsRemove: false,
							},
						},
						DoReplace: false,
					},
				},
				gads.BiddableAdGroupCriterion{
					AdGroupId: 11400713462,
					Criterion: gads.KeywordCriterion{
						Id: 47652524366,
					},
					BiddingStrategyConfiguration: &gads.BiddingStrategyConfiguration{
						Bids: []gads.Bid{
							gads.Bid{
								Type:   "CpcBid",
								Amount: 5372200,
							},
						},
					},
					UrlCustomParameters: gads.CustomParameters{
						CustomParameters: []gads.CustomParameter{
							gads.CustomParameter{
								Key:      "foo",
								Value:    "bar",
								IsRemove: false,
							},
						},
						DoReplace: false,
					},
				},
			},
	}*/
	// Creating AdGroups
	ago := gads.AdGroupOperations{
		"ADD": {
			gads.AdGroup{
				Name:       "test ad group " + rand_str(10),
				Status:     "PAUSED",
				CampaignId: campaignId,
			},
			gads.AdGroup{
				Name:       "test ad group " + rand_str(10),
				Status:     "PAUSED",
				CampaignId: campaignId,
			},
		},
	}

	var operations []interface{}
	operations = append(operations, ago)

	bjo := gads.BatchJobOperations{
		BatchJobOperations: []gads.BatchJobOperation{
			gads.BatchJobOperation{
				Operator: "ADD",
				Operand:  gads.BatchJob{},
			},
		},
	}

	if resp, err := bs.Mutate(bjo); err == nil {

		bjh := gads.NewBatchJobHelper(&config.Auth)
		err = bjh.UploadBatchJobOperations(operations, *resp[0].UploadUrl)

		if err != nil {
			panic(err)
		}

		jobId := resp[0].Id
		batchJobs := gads.BatchJobPage{}

		// loop
		for {
			// recheck every 5 seconds
			time.Sleep(5 * time.Second)
			selector := gads.Selector{
				Fields: []string{
					"Id",
					"Status",
					"DownloadUrl",
					"ProcessingErrors",
					"ProgressStats",
				},
				Predicates: []gads.Predicate{
					{"Id", "EQUALS", []string{strconv.FormatInt(jobId, 10)}},
				},
			}

			// more than likely you'll want to have some logic to loop through these if you have multiple batch jobs, but since only one we just want to grab the first one
			batchJobs, err = bs.Get(selector)

			if err != nil {
				panic(err)
			}

			if batchJobs.BatchJobs[0].Status == "DONE" {
				break
			} else if batchJobs.BatchJobs[0].Status == "CANCELED" {
				panic("Job was canceled")
			}
		}

		if batchJobs.BatchJobs[0].DownloadUrl.Url != "" {
			// get the job
			mutateResult, err := bjh.DownloadBatchJob(*batchJobs.BatchJobs[0].DownloadUrl)

			if err != nil {
				panic(err)
			}

			fmt.Println(mutateResult)
			jsonResult, _ := json.Marshal(mutateResult)

			fmt.Println(string(jsonResult))
		}
	} else {
		// handle err
		panic(err)
	}

}