Skip to content

Upgrading to use new Marketing API contacts list #391

Open

Description

@justinkchen

Issue Summary

I'm wondering what the timeline is expected to be for the sendgrid-ruby gem to support the Marketing Campaign v2?

Steps to Reproduce

N/A

Technical details:

  • sendgrid-ruby Version: master (latest commit: [commit number])
  • Ruby Version: 2.5.3

Activity

  1. tannerhallman commented on Feb 6, 2020

    @tannerhallman

    Any updates on this? There's no documentation on the new Marketing campaigns and I'm pulling my hair out. Bout to rage quit and rewrite app using Mailchimp. It's been 6 months...

    Update: I did find this https://sendgrid.com/docs/API_Reference/api_v3.html contacts which doesn't appear to be documented very well. I still can't get much to work with it though.

    Another Update: I finally, after guessing a lot, got the contacts uploading to a list using the new Marketing.

    See my example for those poor souls after me:

    sg = SendGrid::API.new(api_key: ENV["SENDGRID_API_KEY"])
    users = [{first_name: 'Tanner', last_name: 'Hallman', email: 't@h.com}]
    list_ids = ["2984y3u32-2334932-34344"] # some crazy long UUID of the list you're trying to send these contacts to (you can grab it from the URL bar on the dashboard) (not the old Integer IDs)
    body = {list_ids: list_ids, contacts: users}
    response = sg.client.marketing.contacts.put(request_body: body)
    Rails.logger.debug response.status_code
    Rails.logger.debug response.body
    Rails.logger.debug response.headers
  2. ooochie commented on Feb 6, 2020

    @ooochie

    @tannerhallman I, too, have been waiting for updates here. I ended up moving forward using this gem as-is, but writing an intermediate layer in my own app to access the new endpoints. The way this gem is written allows you to chain the methods together to access the endpoint you need, so you can just initialize your client and go from there, using the API docs you referenced under "NEW MARKETING CAMPAIGNS." Here's what I mean:

    #initialize your client
    @client = SendGrid::API.new(api_key: YOUR_API_KEY).client
    
    #Get a single contact by its ID
    #GET /marketing/contacts/{id}
    @client.marketing.contacts.send(id).get
    
    #delete a contact
    #DELETE /marketing/contacts
    @client.marketing.contacts.delete({query_params: {ids: id}})
    
    #search for a contact
    #POST /marketing/contacts/search
    @client.marketing.contacts.search.post(request_body: {"query"=>"email = '#{email}'"})
    
    #ETC
    

    This API still leaves a lot to be desired and seems like it's not quite finished, but this is working for me in the interim.

  3. thinkingserious commented on Mar 19, 2020

    @thinkingserious
    Contributor

    Hello @justinkchen, @tannerhallman and @ooochie,

    First, BIG THANKS to @tannerhallman and @ooochie for posting solutions!!

    Secondly, to answer @justinkchen, pull requests to add this feature are welcome and will be reviewed based on priority, but Twilio SendGrid is not actively building new functionality for the library.

    With best regards,

    Elmer

  4. added
    status: help wanted
    type: community enhancement
    on Mar 19, 2020
  5. tannerhallman commented on Mar 19, 2020

    @tannerhallman

    Wait so you are not going to be building support for the new marketing campaign into this library? Is SendGrid dropping support for Ruby?

  6. childish-sambino commented on Mar 20, 2020

    @childish-sambino
    Contributor

    @tannerhallman No, not dropping support for Ruby or this library. Just that building new functionality here is lower on the priority list at this time.

  7. kjohnston commented on Mar 22, 2020

    @kjohnston

    I'm looking to come back to SendGrid for the marketing contacts, lists, segments and automation as it's something I can't get from Mailgun, Postmark or MailChimp. It looks like the API documentation for these endpoints is coming together. I'm happy to see a Ruby gem and appreciate the company-supported, community-built, approach here - everyone has to prioritize. But, I have to voice that without some more company support you're forcing busy people to decide between contributing to this project vs. hand-rolling only what they need in isolation. Please consider allocating some more Twilio SendGrid resource to the project to reduce the barrier to (re-)entry.

  8. rickychilcott commented on May 6, 2020

    @rickychilcott

    If someone comes along and wants to be able to upload custom fields using @tannerhallman's code, you'll need to do a bit more work:

    $sg = SendGrid::API.new(api_key: ENV["SENDGRID_API_KEY"])
    
    def sendgrid_custom_field_definitions
      @sendgrid_custom_field_definitions ||=
        $sg
          .marketing
          .field_definitions
          .get
          .parsed_body
          .fetch(:custom_fields)
    end
    
    def transform_custom_fields(contact_groups)
      contact_groups.map do |contact|
        transformed_contact = contact.dup
    
        # Convert from "custom_field_name" to sendgrid"s custom field "id"
        transformed_contact[:custom_fields] =
          transformed_contact[:custom_fields]
            .transform_keys do |key|
            field_definition =
              sendgrid_custom_field_definitions.find do |fd|
                fd[:name].to_s == key.to_s
              end
    
            field_definition[:id]
          end
    
        transformed_contact
      end
    end
    
    users_with_custom_fields = [{first_name: "Ricky", last_name: "Chilcott", email: "r@c.com, custom_fields: {"age" => 30, "ice_cream_flavor" => "chocolate"}}]
    list_ids = ["2984y3u32-2334932-34344"] # some crazy long UUID of the list you"re trying to send these contacts to (you can grab it from the URL bar on the dashboard) (not the old Integer IDs)
    
    users_with_transformed_custom_fields = transform_custom_fields(users_with_custom_fields)
    body = {list_ids: list_ids, contacts: users_with_transformed_custom_fields}
    response = sg.client.marketing.contacts.put(request_body: body)
    
    Rails.logger.debug response.status_code
    Rails.logger.debug response.body
    Rails.logger.debug response.headers

    I've made it rely on SendGrid's field_definitions endpoint to lookup the appropriate mappings.

    Hoping this helps someone else. Ideally, sendgrid-ruby would do this transformation for us -- ideally not even relying on us wrapping custom fields in their own sub-object.

  9. netwire88 commented on Jan 12, 2021

    @netwire88

    Any update on the documentation? This https://github.com/sendgrid/sendgrid-ruby/blob/main/USAGE.md#contactdb looks outdated. I get 403 access forbidden error.

  10. JohnLegrandRichards commented on Aug 10, 2021

    @JohnLegrandRichards

    This seems to be just a documentation issue. Chaining the methods through the client is all there. It just seems to not be well documented in the example folders. I will take the time this week to add a new campaigns folder in the documentation as well as a nested contacts folder. I will then include examples so people stop getting confused.

  11. david-mears-2 commented on Oct 21, 2021

    @david-mears-2

    @JohnLegrandRichards I think you are right that this is just a documentation issue. Did you make any progress on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: help wantedtype: community enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions