Skip to content

Commit

Permalink
Resolved merge conflict in .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
posthogseb committed Aug 15, 2024
2 parents 955594f + 2d9bb48 commit 29bf326
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
.env
instance
2d9bb48c9eb65f691a8395edc479ca1150874260
Binary file removed instance/hogflix.sqlite
Binary file not shown.
223 changes: 223 additions & 0 deletions scripts/create_posthog_artifacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import requests
from argparse import ArgumentParser

parser = ArgumentParser()
parser.add_argument("-k", "--personal_api_key",
help="PostHog Personal API Key", required=True)
parser.add_argument("-p", "--posthog_api_base_url",
help="PostHog API Host", required=True)
args = parser.parse_args()

# The URL to which you want to send the POST request
url = args.posthog_api_base_url

# The Bearer token for authentication
token = args.personal_api_key

# Headers including the Bearer token for authorization
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}

# The JSON payload to be sent in the POST request
actions_ids = {}
actions_endpoint = '/actions/'
actions_data = [
{
"name": "Subscription Cancelled (Test)",
"steps": [
{
"event": "plan_changed",
"properties": [
{
"key": "new_plan",
"type": "event",
"value": [
"Free"
],
"operator": "exact"
}
]
}
]
},
{
"name": "Paid Plan Purchase (Test)",
"steps": [
{
"event": "plan_changed",
"properties": [
{
"key": "new_plan",
"type": "event",
"value": [
"Premium",
"Max-imal"
],
"operator": "exact"
}
]
}
]
},
{
"name": "Watched a movie (Test)",
"description": "Watched a movie",
"steps": [
{
"event": "$pageview",
"url": "movie/",
"url_matching": "contains"
}
]
}
]

insights_endpoint = '/insights/'

movie_views_trend_data = {
"name":"Movie Views by Plan Type 2.0",
"query": {
"kind": "InsightVizNode",
"source": {
"kind": "TrendsQuery",
"properties": {
"type": "AND",
"values": [
{
"type": "AND",
"values": [
{
"key": "plan",
"type": "person",
"value": "is_set",
"operator": "is_set"
}
]
}
]
},
"dateRange": {
"date_to": None,
"date_from": "-30d"
},
"series": [
{
"kind": "ActionsNode",
"math": "total"
}
],
"interval": "week",
"breakdownFilter": {
"breakdowns": [
{
"type": "person",
"property": "plan"
}
]
},
"trendsFilter": {
"display": "ActionsLineGraph"
}
},
"full": True
},
"saved": True
}

purchase_funnel_data = {
"name": "Paid plan purchase funnel 2.0",
"query": {
"kind": "InsightVizNode",
"source": {
"kind": "FunnelsQuery",
"dateRange": {
"date_from": "-30d"
},
"series": [
{
"kind": "EventsNode",
"event": "$pageview",
"name": "$pageview",
"custom_name": "Home Page",
"properties": [
{
"key": "$pathname",
"type": "event",
"value": [
"/"
],
"operator": "exact"
}
]
},
{
"kind": "EventsNode",
"event": "$pageview",
"name": "$pageview",
"custom_name": "Plans Page",
"properties": [
{
"key": "$pathname",
"type": "event",
"value": [
"/plans"
],
"operator": "exact"
}
]
},
{
"kind": "EventsNode",
"event": "$pageview",
"name": "$pageview",
"custom_name": "Signup Page",
"properties": [
{
"key": "$pathname",
"type": "event",
"value": [
"/signup"
],
"operator": "exact"
}
]
},
{
"kind": "ActionsNode",
"name": "Paid Plan Purchase"
}
],
"funnelsFilter": {
"funnelVizType": "steps"
}
},
"full": True
},
"saved": True
}

def make_posthog_api_request(endpoint, data):
response = requests.post(url + endpoint, headers=headers, json=data)
# Checking the status code and response
if response.status_code == 201:
print("Request was successful!")
print("Response:", response.json()) # Assuming the response is JSON
else:
print(f"Request failed with status code {response.status_code}")
print("Response:", response.text)
return response.json()

# Making the POST request

for data in actions_data:
response = make_posthog_api_request(actions_endpoint, data)
actions_ids[data['name']] = response['id']

movie_views_trend_data['query']['source']['series'][0]['id'] = actions_ids.get('Watched a movie (Test)')
response = make_posthog_api_request(insights_endpoint, movie_views_trend_data)

purchase_funnel_data['query']['source']['series'][3]['id'] = actions_ids.get('Paid Plan Purchase (Test)')
response = make_posthog_api_request(insights_endpoint, purchase_funnel_data)

91 changes: 64 additions & 27 deletions scripts/seed_demo_data.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@

from posthog import Posthog
from datetime import datetime,timedelta
from faker import Faker
import json
import os
import gzip
from argparse import ArgumentParser
import random
import uuid

from argparse import ArgumentParser

days_to_generate = 30
number_of_iterations = 100

parser = ArgumentParser()
parser.add_argument("-d", "--number_of_days",
help="Number of days before today to generate data from",default=30, required=False)
parser.add_argument("-i", "--number_of_iterations",
help="Number of iterations of the data generator",default=100, required=False)
parser.add_argument("-k", "--posthog_api_key",
help="PostHog Project API Key", required=True)
parser.add_argument("-p", "--posthog_host",
help="PostHog Host", required=True)
args = parser.parse_args()

# PostHog Python Client
posthog = Posthog(os.getenv('PH_PROJECT_KEY'),
host=os.getenv('PH_HOST'),
posthog = Posthog(args.posthog_api_key,
host=args.posthog_host,
debug=True,
historical_migration=True,
disable_geoip=False
)
Expand Down Expand Up @@ -59,14 +70,15 @@
"$device_type": "Mobile"
}]

def get_random_time(last_number_of_days = 7):
random_seconds = random.randint(0,last_number_of_days * 86400)
plans = ['Free', 'Premium', 'Max-imal']

def get_random_time():
random_seconds = random.randint(0,args.number_of_days * 86400)

random_timestamp = datetime.now() - timedelta(seconds = random_seconds)

return(random_timestamp)

#plan_changed
def capture_pageview(url, timestamp, client_properties, distinct_id):
properties = {
"$current_url": url,
Expand Down Expand Up @@ -101,31 +113,43 @@ def get_client_properties():
properties= {
**random.choice(device_properties),
"$ip": fake.ipv4_public(),
"$session_id": fake.uuid4()
"$session_id": fake.uuid4(),
"active_feature_flags": ["action_mode_on"],
"$feature/action_mode_on": random.choice([True,False])
}
return properties

# Get Amplitude data from folder, unzip it, and use the capture function
def browse_and_watch_movie(number = 1):
client_properties = get_client_properties()
client_properties = { **client_properties,
'$set': {
'plan': random.choice(plans)
}}
distinct_id = fake.ascii_email()

print(distinct_id)

for i in range(random.randint(1, number)):
print(i)
start_timestamp = get_random_time(last_number_of_days=7)
timestamp = get_random_time()
client_properties["$session_id"]=fake.uuid4()

capture_event(event='user_logged_in', extra_properties=client_properties, timestamp=timestamp, distinct_id=distinct_id)

capture_pageview(url='https://hogflix.net/', client_properties = client_properties,timestamp=start_timestamp, distinct_id = distinct_id)
timestamp = timestamp + timedelta(minutes=random.randint(1,5))

capture_pageview(url='https://hogflix.net/', client_properties = client_properties,timestamp=timestamp, distinct_id = distinct_id)

movie_id = random.randint(1,3)

next_timestamp = start_timestamp + timedelta(minutes=random.randint(1,15))
timestamp = timestamp + timedelta(minutes=random.randint(1,15))

capture_pageview(url=f'https://hogflix.net/movies/{movie_id}', client_properties = client_properties, timestamp=next_timestamp, distinct_id = distinct_id)
capture_pageview(url=f'https://hogflix.net/movie/{movie_id}', client_properties = client_properties, timestamp=timestamp, distinct_id = distinct_id)

def anon_browse_homepage_and_plans():
client_properties = get_client_properties()
distinct_id = fake.uuid4()
timestamp = get_random_time(last_number_of_days=7)
print(distinct_id)

timestamp = get_random_time()

capture_pageview(url='https://hogflix.net/', client_properties = client_properties,timestamp=timestamp, distinct_id = distinct_id)

Expand All @@ -143,8 +167,8 @@ def anon_browse_homepage_and_plans():
def browse_plans_and_signup():
client_properties = get_client_properties()
distinct_id = fake.ascii_email()
timestamp = get_random_time(last_number_of_days=7)

timestamp = get_random_time()
print(distinct_id)
capture_pageview(url='https://hogflix.net/', client_properties = client_properties,timestamp=timestamp, distinct_id = distinct_id)

timestamp = timestamp + timedelta(minutes=random.randint(1,10))
Expand All @@ -157,8 +181,21 @@ def browse_plans_and_signup():

timestamp = timestamp + timedelta(minutes=random.randint(1,10))

capture_event(event='plan purchased', extra_properties=client_properties, timestamp=timestamp, distinct_id=distinct_id)

browse_and_watch_movie(number = 10)
anon_browse_homepage_and_plans()
browse_plans_and_signup()
selected_plans = random.sample(plans,2)
previous_plan = selected_plans[0]
new_plan = selected_plans[1]
client_properties = { **client_properties,
"previous_plan": previous_plan,
"new_plan": new_plan,
"$set": {
"plan": new_plan
}}
print(client_properties)
capture_event(event='plan_changed', extra_properties=client_properties, timestamp=timestamp, distinct_id=distinct_id)

for i in range(args.number_of_iterations):
print(args)
browse_and_watch_movie(number = 10)
anon_browse_homepage_and_plans()
browse_plans_and_signup()
posthog.flush()

0 comments on commit 29bf326

Please sign in to comment.