Posts tagged with php

I implemented a custom script in tankyou page in my woocommerce store to track google ads conversions. This is my implementation:

add_action( "woocommerce_thankyou", "pixel_analytics_conversion_track_script", 20 ); if ( ! function_exists( 'pixel_analytics_conversion_track_script' ) ) {     function pixel_analytics_conversion_track_script($order_id){         if ( $order_id > 0 ) {             $order = wc_get_order( $order_id );             if ( $order instanceof WC_Order ) {                 $order_id               = $order->get_id(); // order id                 $order_key              = $order->get_order_key(); // order key                 $order_total            = $order->get_total(); // order total                 $order_currency         = $order->get_currency(); // order currency                 $order_payment_method   = $order->get_payment_method(); // order payment method                 $order_shipping_country = $order->get_shipping_country(); // order shipping country                 $order_billing_country  = $order->get_billing_country(); // order billing country                 $order_status           = $order->get_status(); // order status                 ?>                 <script type="text/javascript">                     jQuery(document).ready(function( $ ){                         console.log('PURCHACE EVENT');                         /* Track conversion on facebook Pixel */                         fbq('track', 'Purchase',                         {                             value: <?php echo $order_total ?>,                             currency: "<?php echo $order_currency ?>"                         });                                              /* Track conversion on Google Ads */                         gtag('event', 'conversion',                          {                              'send_to': 'AW-693771414/0MhwCMa9rLYBEJa56MoC',                              'value': <?php echo $order_total ?>,                              'currency': "<?php echo $order_currency ?>",                              'transaction_id': "<?php echo $order_id ?>"                         });                     });                 </script>                 <?php             }         }     } } 

The code works very well but some data is not precise and I think that probably the code above duplicates conversion if user goes to thank you page twice. We have an order confirmation email that has a link to the thankyou page of woocommerce.

As you can see Im sending the transaction_id parameter, so my question:

If the user loads twice or N times the thank you page the conversion will appear dupplicated in Google ads even if you send the transaction_id parameter?

Has anyone put a Google ad conversion tag on a WooCommerce order confirmation page?

We have our website on Wordpress, use WooCommerce for our ordering and use a Divi Theme.

I tried doing this with one webmaster and we weren't able to get it to work. We tried putting the tag on the order confirmation page and couldn't get it to work.

Now, I have a new webmaster. I set up the conversion tag in Google ads and emailed it to her. She said she used a plug in to set it up on the page. She said she viewed the page, but I'm still seeing it labeled as inactive in Google ads.

Don't know why we're having such problems? What's the trick?

I'm not very familiar with Wordpress. Seems like this should be worked out or other people should also be having this problem.

PHP was suggested as a tag for this question, so I thought I would add it.

Thanks,

I need to fetch:

  1. Name of the campaign.
  2. Impressions.
  3. Cost.
  4. Conversation Rate.
  5. Conversions.
  6. Date.
  7. Budget.
  8. Image/Video of this ad.

Can I fetch it all from campaign entity? How?

$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); // Creates a query that retrieves all campaigns. $query = 'SELECT [MYLIST]  FROM campaign ORDER BY campaign.name'; // Issues a search stream request. /** @var GoogleAdsServerStreamDecorator $stream */ $stream = $googleAdsServiceClient->searchStream($customerId, $query); // Iterates over all rows in all messages and prints the requested field values for // the campaign in each row. foreach ($stream->iterateAllElements() as $googleAdsRow) {     /** @var GoogleAdsRow $googleAdsRow */     printf(         "Campaign with ID %d and name '%s' was found.%s",         $googleAdsRow->getCampaign()->getId(),         $googleAdsRow->getCampaign()->getName(),         AND SOMETHING HERE         PHP_EOL     ); } 

I am trying to get the data from table in Campaign Target CPA but doesn't work...

I tried two methods:

... $dataService = $adWordsServices->get($session, DataService::class); $selector = new Selector(); $selector->setFields(     [         'BidModifier',         'CampaignId',         'CriterionId',         'StartDate',         'EndDate',         'LocalClicks',         'LocalCost',         'LocalImpressions',         'TotalLocalClicks',         'TotalLocalCost',         'TotalLocalImpressions',         'RequiredBudget'     ] ); $selector->setPredicates(     [new Predicate('CampaignId', PredicateOperator::IN, [12334567])] ); $result = $dataService->getCampaignCriterionBidLandscape($selector); ... 

and

... $query = sprintf( 'SELECT campaign_criterion_simulation.criterion_id, ' . 'campaign_criterion_simulation.start_date, ' . 'campaign_criterion_simulation.end_date, ' . 'campaign_criterion_simulation.bid_modifier_point_list.points ' . 'FROM campaign_criterion_simulation ' . 'WHERE campaign_criterion_simulation.campaign_id = %d', 1233456 ); $response = $googleAdsServiceClient->search($customerId, $query); $result = $response->iterateAllElements(); ... 

I get blank data.. Please help me!

I want to link and view the analytics account linked with Google Adwords.

Procedure used:

  1. Authenticating google account with scopes "Ananlytics and Adwords" with following url https://www.googleapis.com/auth/adwords https://www.googleapis.com/auth/analytics
  2. After getting the authentication response creating Google analytics service object.
  3. Google ads link API throwing error "Insufficient Premissions" screenshot attached

Script :

<?php //function to authenticate google account and create analytics service object function googleAuth(){         if (!empty($code)) {                         $postFields = 'client_id=' . Configure::read('GOOGLE_OAUTH_CLIENT_ID') . '&client_secret=' . Configure::read('GOOGLE_OAUTH_CLIENT_SECRET') . '&code=' . $code . '&grant_type=authorization_code&redirect_uri=' . Configure::read('GOOGLE_OAUTH_REDIRECT_URI');                         $ch = curl_init();                         curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');                         curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);                         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);                         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                         $Rec_Data = curl_exec($ch);                         if (curl_exec($ch) === false) {                                 return $Rec_Data;                         }                         $Rec_Data = json_decode($Rec_Data, true);                         if (isset($Rec_Data['refresh_token'])) {                                                                  try {                                         $credentials = array('client_id' => Configure::read('GOOGLE_OAUTH_CLIENT_ID'), 'client_secret' => Configure::read('GOOGLE_OAUTH_CLIENT_SECRET'), 'redirect_uris' => array(Configure::read('GOOGLE_OAUTH_REDIRECT_URI')));                                         $client = new \Google_Client($credentials);                                                                                                  $client->addScope(\Google_Service_Analytics::ANALYTICS_READONLY);                                                 $client->setAccessToken($Rec_Data['access_token']);                                                 // Create an authorized analytics service object.                                                 $analytics = new \Google_Service_Analytics($client);                                                                          } catch (Exception $e) {                                         echo 'Caught exception: ', $e->getMessage(), "\n";                                         die();                                                                          }                         }                 } else {                         if (!empty($id)) {                                 header("Location:https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=" . Configure::read('GOOGLE_OAUTH_CLIENT_ID') . "&redirect_uri=" . Configure::read('GOOGLE_OAUTH_REDIRECT_URI') . "&access_type=offline&approval_prompt=force&state=" . $id . "&scope=https://www.googleapis.com/auth/adwords https://www.googleapis.com/auth/analytics");                                 exit;                         }                 } } //function to fetch linked account list function adwordsLinkAnalytics($analyticsAuth){                 $this->autoRender = false;                          try {                         $adWordsLinks = $analyticsAuth->management_webPropertyAdWordsLinks                                 ->listManagementwebPropertyAdWordsLinks('123456', 'UA-123456-1');                 } catch (apiServiceException $e) {                         print 'There was an Analytics API service error '                         . $e->getCode() . ':+' . $e->getMessage();                         exit;                 } catch (apiException $e) {                         print 'There was a general API error '                         . $e->getCode() . ':-' . $e->getMessage();                         exit;                 }                 pr($adWordsLinks);                 exit;                  } 

Required result: List of the analytics account linked with adwords account.