1

I'm trying to take Campaign Monitor Open events and pipe the data to Segment.com via POST API using Python code Action on Zapier.

I keep getting the following error:

Bargle. We hit an error creating a run python. :-( Error: Your code had an error! Traceback (most recent call last): SyntaxError: invalid >syntax (usercode.py, line 9)

Here is the existing setup screenshot (masking the auth code): Zapier Zap Setup for Code

The Python code returning the error is:

url = 'https://api.segment.io/v1/track/'
payload =
{
  'userId': input_data['email'],
  'event': 'Email Opened',
  'properties': {
    'listid': input_data['listid'],
    'open_date': input_data['date'],
    'cm_id': input_data['cm_id'],
    'open_city': input_data['city'],
    'open_region': input_data['region'],
    'open_country': input_data['country'],
    'open_lat': input_data['lat'],
    'open_long': input_data['long'],
    'open_country_code': input_data['country_code']
  },
  'context': {
    'ip': input_data['ip']
  }
}

headers = {
    'content-type': 'application/json',
    'Authorization': 'Basic BASE64ENCODEDWRITEKEY'
}

response = requests.post(url, data=json.dumps(payload), headers=headers)
response.raise_for_status()
return response.json()

Any advice on what the error may be referencing? Any advice overall on how to better achieve this?

2 Answers2

0

Doing this:

payload =
{}

Is improper syntax. Try:

payload = {}

I also recommend using a linter - maybe http://infoheap.com/python-lint-online/ would be helpful to you!

Bryan Helmig
  • 636
  • 4
  • 4
0

Thanks to @Bryan Helmig. That syntax, in addition to import json fixed the issue. For those interested, this works...

import json
import requests
url = 'https://api.segment.io/v1/track/'
payload = {
  'userId': input_data['email'],
  'event': 'Email Opened',
  'properties': {
    'listid': input_data['listid'],
    'open_date': input_data['date'],
    'cm_id': input_data['cm_id'],
    'open_city': input_data['city'],
    'open_region': input_data['region'],
    'open_country': input_data['country'],
    'open_lat': input_data['lat'],
    'open_long': input_data['long'],
    'open_country_code': input_data['country_code']
  },
  'context': {
    'ip': input_data['ip']
  }
       }

headers = {
    'content-type': 'application/json',
    'Authorization': 'Basic WRITEKEYHERE'
}

response = requests.post(url, data=json.dumps(payload), headers=headers)
response.raise_for_status()